]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/config/picochip/picochip.c
4756cb78b724a6f330ff56496a9f6ffee633f0c5
[thirdparty/gcc.git] / gcc / config / picochip / picochip.c
1 /* Subroutines used for code generation on picoChip processors.
2 Copyright (C) 2001-2013 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)
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
13
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not, see
21 <http://www.gnu.org/licenses/>. */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "rtl.h"
28 #include "regs.h"
29 #include "hard-reg-set.h"
30 #include "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 "calls.h"
38 #include "stor-layout.h"
39 #include "stringpool.h"
40 #include "varasm.h"
41 #include "expr.h"
42 #include "optabs.h"
43 #include "except.h"
44 #include "function.h"
45 #include "output.h"
46 #include "basic-block.h"
47 #include "diagnostic-core.h"
48 #include "ggc.h"
49 #include "hashtab.h"
50 #include "tm_p.h"
51 #include "target.h"
52 #include "target-def.h"
53 #include "langhooks.h"
54 #include "reload.h"
55 #include "params.h"
56
57 #include "picochip-protos.h"
58
59 #include "insn-attr.h" /* For DFA state_t. */
60 #include "insn-config.h" /* Required by recog.h */
61 #include "insn-codes.h" /* For CODE_FOR_? */
62 #include "optabs.h" /* For GEN_FCN */
63 #include "basic-block.h" /* UPDATE_LIFE_GLOBAL* for picochip_reorg. */
64 #include "timevar.h" /* For TV_SCHED2, in picochip_reorg. */
65 #include "libfuncs.h" /* For memcpy_libfuncs, etc. */
66 #include "df.h" /* For df_regs_ever_live_df_regs_ever_live_pp, etc. */
67 #include "dbxout.h"
68 \f
69
70 /* Target AE ISA information. */
71 enum picochip_dfa_type picochip_schedule_type;
72
73 bool picochip_has_mul_unit = false;
74 bool picochip_has_mac_unit = false;
75
76 /* targetm hook function prototypes. */
77
78 void picochip_asm_file_start (void);
79 void picochip_asm_file_end (void);
80
81 void picochip_init_libfuncs (void);
82 void picochip_reorg (void);
83
84 int picochip_arg_partial_bytes (cumulative_args_t p_cum,
85 enum machine_mode mode,
86 tree type, bool named);
87 rtx picochip_function_arg (cumulative_args_t p_cum,
88 enum machine_mode mode,
89 const_tree type, bool named);
90 rtx picochip_incoming_function_arg (cumulative_args_t p_cum,
91 enum machine_mode mode,
92 const_tree type, bool named);
93 void picochip_arg_advance (cumulative_args_t p_cum, enum machine_mode mode,
94 const_tree type, bool named);
95 unsigned int picochip_function_arg_boundary (enum machine_mode mode,
96 const_tree type);
97
98 int picochip_sched_lookahead (void);
99 int picochip_sched_issue_rate (void);
100 int picochip_sched_adjust_cost (rtx insn, rtx link,
101 rtx dep_insn, int cost);
102 int picochip_sched_reorder (FILE * file, int verbose, rtx * ready,
103 int *n_readyp, int clock);
104
105 void picochip_init_builtins (void);
106 rtx picochip_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
107
108 bool picochip_rtx_costs (rtx x, int code, int outer_code, int opno,
109 int* total, bool speed);
110 bool picochip_return_in_memory(const_tree type,
111 const_tree fntype ATTRIBUTE_UNUSED);
112 bool picochip_legitimate_address_p (enum machine_mode, rtx, bool);
113 rtx picochip_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
114 enum machine_mode mode);
115 int picochip_legitimize_reload_address (rtx *x, enum machine_mode mode,
116 int opnum, int type, int ind_levels);
117
118 rtx picochip_struct_value_rtx(tree fntype ATTRIBUTE_UNUSED, int incoming ATTRIBUTE_UNUSED);
119 rtx picochip_function_value (const_tree valtype, const_tree func ATTRIBUTE_UNUSED,
120 bool outgoing ATTRIBUTE_UNUSED);
121 static reg_class_t
122 picochip_secondary_reload (bool in_p,
123 rtx x ATTRIBUTE_UNUSED,
124 reg_class_t cla ATTRIBUTE_UNUSED,
125 enum machine_mode mode,
126 secondary_reload_info *sri);
127 void
128 picochip_asm_named_section (const char *name,
129 unsigned int flags ATTRIBUTE_UNUSED,
130 tree decl ATTRIBUTE_UNUSED);
131
132 static rtx picochip_static_chain (const_tree, bool);
133
134 static void picochip_option_override (void);
135
136 /* Lookup table mapping a register number to the earliest containing
137 class. Used by REGNO_REG_CLASS. */
138 const enum reg_class picochip_regno_reg_class[FIRST_PSEUDO_REGISTER] =
139 {
140 TWIN_REGS, TWIN_REGS, TWIN_REGS, TWIN_REGS,
141 TWIN_REGS, TWIN_REGS, TWIN_REGS, TWIN_REGS,
142 TWIN_REGS, TWIN_REGS, TWIN_REGS, TWIN_REGS,
143 GR_REGS, FRAME_REGS, PTR_REGS, CONST_REGS,
144 ACC_REGS, CC_REGS, GR_REGS, GR_REGS
145 };
146
147 /* picoChip register names. */
148 const char *picochip_regnames[] = REGISTER_NAMES;
149
150 /* Define the maximum number of registers which may be used to pass
151 * parameters to functions. */
152 #define MAX_CALL_PARAMETER_REGS 6
153 \f
154
155 /* Target scheduling information. */
156
157 /* This flag indicates whether the next instruction to be output is a
158 VLIW continuation instruction. It is used to communicate between
159 final_prescan_insn and asm_output_opcode. */
160 static int picochip_vliw_continuation = 0;
161
162 /* This variable is used to communicate the current instruction
163 between final_prescan_insn and functions such as asm_output_opcode,
164 and picochip_get_vliw_alu_id (which are otherwise unable to determine the
165 current instruction. */
166 static rtx picochip_current_prescan_insn;
167
168 static bool picochip_is_delay_slot_pending = 0;
169
170 /* When final_prescan_insn is called, it computes information about
171 the current VLIW packet, and stores it in this structure. When
172 instructions are output, this state is used to make sure that the
173 instructions are output in the correct way (e.g., which ALU to use,
174 whether a macro branch was ever previously a real branch, etc.). */
175 struct vliw_state
176 {
177 int contains_pico_alu_insn;
178 int contains_non_cc_alu_insn;
179 int num_alu_insns_so_far;
180
181 /* Record how many instructions are contained in the packet. */
182 int num_insns_in_packet;
183
184 /* There was a case for this to be more than 1 */
185 int num_cfi_labels_deferred;
186 char cfi_label_name[2][256]; /* Used to record the name of a CFI label
187 emitted inside a VLIW packet. */
188 char lm_label_name[256]; /* Used to record the name of an LM label. */
189 };
190
191 struct vliw_state picochip_current_vliw_state;
192
193 /* Save/restore recog_data. */
194 static int picochip_saved_which_alternative;
195 static struct recog_data_d picochip_saved_recog_data;
196
197 /* Determine which ALU to use for the instruction in
198 picochip_current_prescan_insn. */
199 static char picochip_get_vliw_alu_id (void);
200 \f
201 /* Initialize the GCC target structure. */
202
203 #undef TARGET_ASM_FUNCTION_PROLOGUE
204 #define TARGET_ASM_FUNCTION_PROLOGUE picochip_function_prologue
205
206 #undef TARGET_ASM_FUNCTION_EPILOGUE
207 #define TARGET_ASM_FUNCTION_EPILOGUE picochip_function_epilogue
208
209 #undef TARGET_ASM_INTERNAL_LABEL
210 #define TARGET_ASM_INTERNAL_LABEL picochip_output_internal_label
211
212 #undef TARGET_ASM_GLOBALIZE_LABEL
213 #define TARGET_ASM_GLOBALIZE_LABEL picochip_output_global
214
215 #undef TARGET_ASM_BYTE_OP
216 #define TARGET_ASM_BYTE_OP ".initByte "
217 #undef TARGET_ASM_ALIGNED_HI_OP
218 #define TARGET_ASM_ALIGNED_HI_OP ".initWord "
219 #undef TARGET_ASM_UNALIGNED_HI_OP
220 #define TARGET_ASM_UNALIGNED_HI_OP ".unalignedInitWord "
221 #undef TARGET_ASM_ALIGNED_SI_OP
222 #define TARGET_ASM_ALIGNED_SI_OP ".initLong "
223 #undef TARGET_ASM_UNALIGNED_SI_OP
224 #define TARGET_ASM_UNALIGNED_SI_OP ".unalignedInitLong "
225
226 #undef TARGET_INIT_BUILTINS
227 #define TARGET_INIT_BUILTINS picochip_init_builtins
228
229 #undef TARGET_EXPAND_BUILTIN
230 #define TARGET_EXPAND_BUILTIN picochip_expand_builtin
231
232 #undef TARGET_RTX_COSTS
233 #define TARGET_RTX_COSTS picochip_rtx_costs
234
235 #undef TARGET_SCHED_ISSUE_RATE
236 #define TARGET_SCHED_ISSUE_RATE picochip_sched_issue_rate
237
238 #undef TARGET_SCHED_REORDER
239 #define TARGET_SCHED_REORDER picochip_sched_reorder
240
241 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
242 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD \
243 picochip_sched_lookahead
244
245 #undef TARGET_SCHED_ADJUST_COST
246 #define TARGET_SCHED_ADJUST_COST picochip_sched_adjust_cost
247
248 #undef TARGET_ASM_NAMED_SECTION
249 #define TARGET_ASM_NAMED_SECTION picochip_asm_named_section
250
251 #undef TARGET_HAVE_SWITCHABLE_BSS_SECTIONS
252 #define TARGET_HAVE_SWITCHABLE_BSS_SECTIONS 1
253
254 #undef TARGET_INIT_LIBFUNCS
255 #define TARGET_INIT_LIBFUNCS picochip_init_libfuncs
256
257 #undef TARGET_ASM_FILE_START
258 #define TARGET_ASM_FILE_START picochip_asm_file_start
259
260 #undef TARGET_ASM_FILE_END
261 #define TARGET_ASM_FILE_END picochip_asm_file_end
262
263 #undef TARGET_MACHINE_DEPENDENT_REORG
264 #define TARGET_MACHINE_DEPENDENT_REORG picochip_reorg
265
266 #undef TARGET_ARG_PARTIAL_BYTES
267 #define TARGET_ARG_PARTIAL_BYTES picochip_arg_partial_bytes
268
269 #undef TARGET_FUNCTION_ARG
270 #define TARGET_FUNCTION_ARG picochip_function_arg
271
272 #undef TARGET_FUNCTION_INCOMING_ARG
273 #define TARGET_FUNCTION_INCOMING_ARG picochip_incoming_function_arg
274
275 #undef TARGET_FUNCTION_ARG_ADVANCE
276 #define TARGET_FUNCTION_ARG_ADVANCE picochip_arg_advance
277
278 #undef TARGET_FUNCTION_ARG_BOUNDARY
279 #define TARGET_FUNCTION_ARG_BOUNDARY picochip_function_arg_boundary
280
281 #undef TARGET_PROMOTE_FUNCTION_MODE
282 #define TARGET_PROMOTE_FUNCTION_MODE default_promote_function_mode_always_promote
283 #undef TARGET_PROMOTE_PROTOTYPES
284 #define TARGET_PROMOTE_PROTOTYPES hook_bool_const_tree_true
285
286 /* Target support for Anchored Addresses optimization */
287 #undef TARGET_MIN_ANCHOR_OFFSET
288 #define TARGET_MIN_ANCHOR_OFFSET 0
289 #undef TARGET_MAX_ANCHOR_OFFSET
290 #define TARGET_MAX_ANCHOR_OFFSET 7
291 #undef TARGET_ASM_OUTPUT_ANCHOR
292 #define TARGET_ASM_OUTPUT_ANCHOR picochip_asm_output_anchor
293
294 #undef TARGET_FUNCTION_VALUE
295 #define TARGET_FUNCTION_VALUE picochip_function_value
296 /*
297 #undef TARGET_LIBGCC_CMP_RETURN_MODE
298 #define TARGET_LIBGCC_CMP_RETURN_MODE picochip_libgcc_cmp_return_mode
299 */
300
301 #undef TARGET_LEGITIMATE_ADDRESS_P
302 #define TARGET_LEGITIMATE_ADDRESS_P picochip_legitimate_address_p
303
304 #undef TARGET_LEGITIMIZE_ADDRESS
305 #define TARGET_LEGITIMIZE_ADDRESS picochip_legitimize_address
306
307 /* Loading and storing QImode values to and from memory
308 usually requires a scratch register. */
309 #undef TARGET_SECONDARY_RELOAD
310 #define TARGET_SECONDARY_RELOAD picochip_secondary_reload
311
312 /* How Large Values are Returned */
313
314 #undef TARGET_RETURN_IN_MEMORY
315 #define TARGET_RETURN_IN_MEMORY picochip_return_in_memory
316
317 #undef TARGET_STATIC_CHAIN
318 #define TARGET_STATIC_CHAIN picochip_static_chain
319
320 #undef TARGET_OPTION_OVERRIDE
321 #define TARGET_OPTION_OVERRIDE picochip_option_override
322
323 #undef TARGET_OVERRIDE_OPTIONS_AFTER_CHANGE
324 #define TARGET_OVERRIDE_OPTIONS_AFTER_CHANGE picochip_option_override
325
326 /* The 2nd scheduling pass option is switched off, and a machine
327 dependent reorganisation ensures that it is run later on, after the
328 second jump optimisation. */
329 #undef TARGET_DELAY_SCHED2
330 #define TARGET_DELAY_SCHED2 true
331
332 /* Variable tracking should be run after all optimizations which
333 change order of insns. It also needs a valid CFG. */
334 #undef TARGET_DELAY_VARTRACK
335 #define TARGET_DELAY_VARTRACK true
336
337 struct gcc_target targetm = TARGET_INITIALIZER;
338 \f
339
340 /* Only return a value in memory if it is greater than 4 bytes.
341 int_size_in_bytes returns -1 for variable size objects, which go in
342 memory always. The cast to unsigned makes -1 > 8. */
343
344 bool
345 picochip_return_in_memory(const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
346 {
347 return ((unsigned HOST_WIDE_INT) int_size_in_bytes (type) > 4);
348 }
349
350 /* Allow some options to be overriden. */
351
352 static void
353 picochip_option_override (void)
354 {
355 /* If we are optimizing for stack, dont let inliner to inline functions
356 that could potentially increase stack size.*/
357 if (flag_conserve_stack)
358 {
359 maybe_set_param_value (PARAM_LARGE_STACK_FRAME, 0,
360 global_options.x_param_values,
361 global_options_set.x_param_values);
362 maybe_set_param_value (PARAM_STACK_FRAME_GROWTH, 0,
363 global_options.x_param_values,
364 global_options_set.x_param_values);
365 }
366
367 /* Turn off the elimination of unused types. The elaborator
368 generates various interesting types to represent constants,
369 generics, and so on, and it is useful to retain this information
370 in the debug output. The increased size of the debug information
371 is not really an issue for us. */
372 flag_eliminate_unused_debug_types = 0;
373
374 /* Even if the user specifies a -fno-omit-frame-pointer on the
375 command line, we still want to go ahead and omit frame pointer
376 usages, since we dont really have a frame pointer register.
377 So, all accesses to FP need to be converted to accesses off
378 stack pointer.*/
379 flag_omit_frame_pointer = 1;
380
381 /* Turning on anchored addresses by default. This is an optimization
382 that could decrease the code size by placing anchors in data and
383 accessing offsets from the anchor for file local data variables.*/
384 if (optimize >= 1)
385 flag_section_anchors = 1;
386
387 /* The second scheduling pass runs within picochip_reorg, to avoid
388 having the second jump optimisation trash the instruction modes
389 (e.g., instructions are changed to TImode to mark the beginning
390 of cycles). Two types of DFA scheduling are possible: space and
391 speed. In both cases, instructions are reordered to avoid stalls
392 (e.g., memory loads stall for one cycle). Speed scheduling will
393 also enable VLIW instruction packing. VLIW instructions use more
394 code space, so VLIW scheduling is disabled when scheduling for
395 size. */
396 if (flag_schedule_insns_after_reload)
397 {
398 if (optimize_size)
399 picochip_schedule_type = DFA_TYPE_SPACE;
400 else
401 {
402 picochip_schedule_type = DFA_TYPE_SPEED;
403 flag_delayed_branch = 0;
404 }
405 }
406 else
407 picochip_schedule_type = DFA_TYPE_NONE;
408
409 /* Ensure that the debug level is always at least -g2. The flow
410 analyser works at its best if it always has debug
411 information. DWARF is non-intrusive, so it makes no difference to
412 code quality if debug is always enabled. */
413 if (debug_info_level < DINFO_LEVEL_NORMAL)
414 {
415 debug_info_level = DINFO_LEVEL_NORMAL;
416 write_symbols = DWARF2_DEBUG;
417 }
418
419 /* Options of the form -mae=mac, and so on will be substituted by
420 the compiler driver for the appropriate byte access and multiply
421 unit ISA options. Any unrecognised AE types will end up being
422 passed to the compiler, which should reject them as invalid. */
423 if (picochip_ae_type_string != NULL)
424 error ("invalid AE type specified (%s)", picochip_ae_type_string);
425
426 /* Override any specific capabilities of the instruction set. These
427 take precedence over any capabilities inferred from the AE type,
428 regardless of where the options appear on the command line. */
429 if (picochip_mul_type_string == NULL)
430 {
431 /* Default to MEM-type multiply, for historical compatibility. */
432 picochip_has_mac_unit = false;
433 picochip_has_mul_unit = true;
434 }
435 else
436 {
437 picochip_has_mac_unit = false;
438 picochip_has_mul_unit = false;
439
440 if (strcmp (picochip_mul_type_string, "mul") == 0)
441 picochip_has_mul_unit = true;
442 else if (strcmp (picochip_mul_type_string, "mac") == 0)
443 picochip_has_mac_unit = true;
444 else if (strcmp (picochip_mul_type_string, "none") == 0)
445 { /* Do nothing. Unit types already set to false. */ }
446 else
447 error ("invalid mul type specified (%s) - expected mac, mul or none",
448 picochip_mul_type_string);
449 }
450 }
451 \f
452
453 /* Initialise the library functions to handle arithmetic on some of
454 the larger modes. */
455 void
456 picochip_init_libfuncs (void)
457 {
458 /* 64-bit shifts */
459 set_optab_libfunc (ashr_optab, DImode, "__ashrdi3");
460 set_optab_libfunc (ashl_optab, DImode, "__ashldi3");
461 set_optab_libfunc (lshr_optab, DImode, "__lshrdi3");
462
463 /* 64-bit signed multiplication. */
464 set_optab_libfunc (smul_optab, DImode, "__muldi3");
465
466 /* Signed division */
467 set_optab_libfunc (sdiv_optab, HImode, "__divhi3");
468 set_optab_libfunc (sdiv_optab, DImode, "__divdi3");
469
470 /* Signed modulus */
471 set_optab_libfunc (smod_optab, HImode, "__modhi3");
472 set_optab_libfunc (smod_optab, DImode, "__moddi3");
473
474 /* 32-bit count leading Zeros*/
475 set_optab_libfunc (clz_optab, SImode, "_clzsi2");
476
477 /* 64-bit comparison */
478 set_optab_libfunc (ucmp_optab, DImode, "__ucmpdi2");
479 set_optab_libfunc (cmp_optab, DImode, "__cmpdi2");
480
481 /* 64-bit addition and subtraction*/
482 set_optab_libfunc (add_optab, DImode, "_adddi3");
483 set_optab_libfunc (sub_optab, DImode, "_subdi3");
484 }
485
486 /* Memcpy function */
487 int
488 picochip_expand_movmemhi (rtx *operands)
489 {
490 rtx src_addr_reg, dst_addr_reg, count_reg, src_mem, dst_mem, tmp_reg;
491 rtx start_label;
492 int align, size;
493 src_addr_reg = gen_reg_rtx(HImode);
494 dst_addr_reg = gen_reg_rtx(HImode);
495 count_reg = gen_reg_rtx(HImode);
496 emit_insn (gen_movhi (count_reg, operands[2]));
497 emit_insn (gen_movqi (src_addr_reg, XEXP(operands[1], 0)));
498 emit_insn (gen_movqi (dst_addr_reg, XEXP(operands[0], 0)));
499 gcc_assert (GET_CODE(count_reg) == REG);
500 start_label = gen_label_rtx ();
501 emit_label (start_label);
502
503 /* We can specialise the code for different alignments */
504 align = INTVAL(operands[3]);
505 size = INTVAL(operands[2]);
506 gcc_assert(align >= 0 && size >= 0);
507 if (size != 0)
508 {
509 if (size % 4 == 0 && align % 4 == 0)
510 {
511 src_mem = gen_rtx_MEM(SImode, src_addr_reg);
512 dst_mem = gen_rtx_MEM(SImode, dst_addr_reg);
513 tmp_reg = gen_reg_rtx(SImode);
514 emit_insn (gen_movsi (tmp_reg, src_mem));
515 emit_insn (gen_movsi (dst_mem, tmp_reg));
516 emit_insn (gen_addhi3 (dst_addr_reg, dst_addr_reg, GEN_INT(4)));
517 emit_insn (gen_addhi3 (src_addr_reg, src_addr_reg, GEN_INT(4)));
518 emit_insn (gen_addhi3 (count_reg, count_reg, GEN_INT(-4)));
519 /* The sub instruction above generates cc, but we cannot just emit the branch.*/
520 emit_cmp_and_jump_insns (count_reg, const0_rtx, GT, 0, HImode, 0, start_label);
521 }
522 else if (size % 2 == 0 && align % 2 == 0)
523 {
524 src_mem = gen_rtx_MEM(HImode, src_addr_reg);
525 dst_mem = gen_rtx_MEM(HImode, dst_addr_reg);
526 tmp_reg = gen_reg_rtx(HImode);
527 emit_insn (gen_movhi (tmp_reg, src_mem));
528 emit_insn (gen_movhi (dst_mem, tmp_reg));
529 emit_insn (gen_addhi3 (dst_addr_reg, dst_addr_reg, const2_rtx));
530 emit_insn (gen_addhi3 (src_addr_reg, src_addr_reg, const2_rtx));
531 emit_insn (gen_addhi3 (count_reg, count_reg, GEN_INT(-2)));
532 /* The sub instruction above generates cc, but we cannot just emit the branch.*/
533 emit_cmp_and_jump_insns (count_reg, const0_rtx, GT, 0, HImode, 0, start_label);
534 }
535 else
536 {
537 src_mem = gen_rtx_MEM(QImode, src_addr_reg);
538 dst_mem = gen_rtx_MEM(QImode, dst_addr_reg);
539 tmp_reg = gen_reg_rtx(QImode);
540 emit_insn (gen_movqi (tmp_reg, src_mem));
541 emit_insn (gen_movqi (dst_mem, tmp_reg));
542 emit_insn (gen_addhi3 (dst_addr_reg, dst_addr_reg, const1_rtx));
543 emit_insn (gen_addhi3 (src_addr_reg, src_addr_reg, const1_rtx));
544 emit_insn (gen_addhi3 (count_reg, count_reg, GEN_INT(-1)));
545 /* The sub instruction above generates cc, but we cannot just emit the branch.*/
546 emit_cmp_and_jump_insns (count_reg, const0_rtx, GT, 0, HImode, 0, start_label);
547 }
548 }
549 return 1;
550 }
551
552 \f
553 /* Return the register class for letter C. */
554 enum reg_class
555 picochip_reg_class_from_letter (unsigned c)
556 {
557 switch (c)
558 {
559 case 'k':
560 return FRAME_REGS;
561 case 'f':
562 return PTR_REGS;
563 case 't':
564 return TWIN_REGS;
565 case 'r':
566 return GR_REGS;
567 default:
568 return NO_REGS;
569 }
570 }
571
572 static const int
573 pico_leaf_reg_alloc_order[] = LEAF_REG_ALLOC_ORDER;
574 static const int
575 pico_nonleaf_reg_alloc_order[] = REG_ALLOC_ORDER;
576
577 void
578 picochip_order_regs_for_local_alloc (void)
579 {
580 /* We change the order for leaf functions alone. We put r12 at
581 the end since using it will prevent us to combine stw/ldws to
582 stl/ldl and it gives no benefit. In non-leaf functions, we
583 would anyway saveup/restore r12, so it makes sense to use it.*/
584
585 if (leaf_function_p())
586 {
587 memcpy ((char *)reg_alloc_order, (const char *) pico_leaf_reg_alloc_order,
588 FIRST_PSEUDO_REGISTER * sizeof (int));
589 }
590 else
591 {
592 memcpy ((char *)reg_alloc_order, (const char *) pico_nonleaf_reg_alloc_order,
593 FIRST_PSEUDO_REGISTER * sizeof (int));
594 }
595 }
596
597 /* Check that VALUE (an INT_CST) is ok as a constant of type C. */
598 int
599 picochip_const_ok_for_letter_p (unsigned HOST_WIDE_INT value, unsigned c)
600 {
601
602 switch (c)
603 {
604 case 'I': /* 4 bits signed. */
605 return value + 8 < 16;
606 case 'J': /* 4 bits unsigned. */
607 return value < 16;
608 case 'K': /* 8 bits signed. */
609 return value + 128 < 256;
610 case 'M': /* 4-bit magnitude. */
611 return abs (value) < 16;
612 case 'N': /* 10 bits signed. */
613 return value + 512 > 1024;
614 case 'O': /* 16 bits signed. */
615 return value + 32768 < 65536;
616 default: /* Unknown letter. */
617 return 0;
618 }
619 }
620 \f
621 /* Stack utility functions. */
622 rtx
623 picochip_return_addr_rtx(int count, rtx frameaddr ATTRIBUTE_UNUSED)
624 {
625 if (count==0)
626 return gen_rtx_REG (Pmode, LINK_REGNUM);
627 else
628 return NULL_RTX;
629 }
630
631
632 /* Emit a set of parallel register expressions used to store
633 blockmode values to pass to functions. */
634 static rtx
635 picochip_emit_register_parallel (int size_in_units, int offset)
636 {
637 int num_regs = 0;
638 rtx result;
639 rtx vector[MAX_CALL_PARAMETER_REGS];
640 int base_reg = 0;
641 int i = 0;
642
643 /* Compute the base register, and number of required registers. */
644 base_reg = offset / 2;
645 num_regs = size_in_units / 2;
646 if (size_in_units % 2 == 1)
647 num_regs++;
648
649 /* Emit a register for each part of the block mode value to be
650 passed in a register. */
651 for (i = 0; i < num_regs; i++)
652 vector[i] = gen_rtx_EXPR_LIST (VOIDmode,
653 gen_rtx_REG (HImode, base_reg + i),
654 GEN_INT (i * 2));
655 result = gen_rtx_PARALLEL (BLKmode, gen_rtvec_v (num_regs, vector));
656
657 return result;
658
659 }
660
661 /* Emit an instruction to allocate a suitable amount of space on the
662 stack, by decrementing the stack pointer. */
663 static void
664 picochip_emit_stack_allocate (int adjustment)
665 {
666 rtx insn;
667 rtx stack_pointer_reg = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
668
669 /* Use an addition of a negative value. */
670 insn = emit_insn (gen_addhi3 (stack_pointer_reg, stack_pointer_reg,
671 GEN_INT (-adjustment)));
672
673 /* Make the instruction frame related. Also add an expression note,
674 so that the correct Dwarf information is generated (see documention
675 for RTX_FRAME_RELATED_P for more details). */
676 RTX_FRAME_RELATED_P (insn) = 1;
677 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
678 gen_rtx_SET (VOIDmode, stack_pointer_reg,
679 gen_rtx_PLUS (Pmode, stack_pointer_reg,
680 GEN_INT (-adjustment))));
681
682 }
683
684 /* Emit an instruction to save a register of the given mode. The
685 offset at which to save the register is given relative to the stack
686 pointer. */
687 static void
688 picochip_emit_save_register (rtx reg, int offset)
689 {
690 rtx stack_pointer, address, mem, insn;
691
692 stack_pointer = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
693
694 address = gen_rtx_PLUS (Pmode, stack_pointer, GEN_INT (offset));
695
696 mem = gen_rtx_MEM (GET_MODE (reg), address);
697
698 insn = emit_move_insn (mem, reg);
699 RTX_FRAME_RELATED_P (insn) = 1;
700
701 /* For modes other than HImode, create a note explaining that
702 multiple registers have been saved. This allows the correct DWARF
703 call frame information to be generated. */
704 switch (GET_MODE (reg))
705 {
706 case HImode:
707 /* The RTL is sufficient to explain HImode register saves. */
708 break;
709
710 case SImode:
711 /* SImode must be broken down into parallel HImode register saves. */
712 {
713 rtvec p;
714 p = rtvec_alloc (2);
715
716 RTVEC_ELT (p, 0) =
717 gen_rtx_SET (HImode,
718 gen_rtx_MEM (HImode,
719 gen_rtx_PLUS (Pmode, stack_pointer,
720 GEN_INT (offset))),
721 gen_rtx_REG (HImode, REGNO (reg)));
722 RTX_FRAME_RELATED_P (RTVEC_ELT (p, 0)) = 1;
723
724 RTVEC_ELT (p, 1) =
725 gen_rtx_SET (HImode, gen_rtx_MEM (HImode,
726 gen_rtx_PLUS (Pmode,
727 stack_pointer,
728 GEN_INT (offset +
729 2))),
730 gen_rtx_REG (HImode, REGNO (reg) + 1));
731 RTX_FRAME_RELATED_P (RTVEC_ELT (p, 1)) = 1;
732
733 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
734 gen_rtx_PARALLEL (VOIDmode, p));
735
736 }
737 break;
738
739 default:
740 internal_error
741 ("unexpected mode %s encountered in picochip_emit_save_register",
742 GET_MODE_NAME (GET_MODE (reg)));
743 }
744
745 }
746
747 /* Emit an instruction to restore a register of the given mode. The
748 offset from which to restore the register is given relative to the
749 stack pointer. */
750 static void
751 picochip_emit_restore_register (rtx reg, int offset)
752 {
753 rtx stack_pointer, address, mem;
754
755 stack_pointer = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
756
757 address = gen_rtx_PLUS (Pmode, stack_pointer, GEN_INT (offset));
758
759 mem = gen_rtx_MEM (GET_MODE (reg), address);
760
761 emit_move_insn (reg, mem);
762
763 }
764
765 /* Check that the given byte offset is aligned to the given number of
766 bits. */
767 static int
768 picochip_is_aligned (int byte_offset, int bit_alignment)
769 {
770 int byte_alignment = bit_alignment / BITS_PER_UNIT;
771 return (byte_offset % byte_alignment) == 0;
772 }
773 \f
774 /*****************************************************************************
775 * Stack layout.
776 *
777 * The following section contains code which controls how the stack is
778 * laid out.
779 *
780 * The stack is laid out as follows (high addresses first):
781 *
782 * Incoming arguments
783 * Pretend arguments (ARG PTR)
784 * Special registers
785 * General registers
786 * Frame (FP)
787 * Outgoing arguments (SP)
788 *
789 * The (constant) offsets of the different areas must be calculated
790 * relative to the stack area immediately below, and aligned
791 * appropriately. For example, the frame offset is computed by
792 * determining the offset of the special register area, adding the
793 * size of the special register area, and then aligning the resulting
794 * offset correctly. In turn, the special register offset is computed
795 * from the general register offset, and so on. This enables the
796 * different offsets to change size and alignment, without requiring
797 * the code for other offset calculations to be rewritten.
798 *
799 * The argument pointer, and the frame pointer are eliminated wherever
800 * possible, by replacing them with a constant offset from the stack
801 * pointer. In the rare cases where constant offsets from the stack
802 * pointer cannot be computed, another register will be allocated to
803 * serve as the argument pointer, or the frame pointer.
804 *
805 * The save registers are stored at small offsets from the caller, to
806 * enable the more efficient SP-based ISA instructions to be used.
807 *
808 ****************************************************************************/
809
810 /* Compute the size of an argument in units. */
811 static int
812 picochip_compute_arg_size (const_tree type, enum machine_mode mode)
813 {
814 int type_size_in_units = 0;
815
816 if (type)
817 type_size_in_units = tree_to_uhwi (TYPE_SIZE_UNIT (type));
818 else
819 type_size_in_units = GET_MODE_SIZE (mode);
820
821 return type_size_in_units;
822
823 }
824
825 /* Determine where the next outgoing arg should be placed. */
826 rtx
827 picochip_function_arg (cumulative_args_t cum_v, enum machine_mode mode,
828 const_tree type, bool named ATTRIBUTE_UNUSED)
829 {
830 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
831 int reg = 0;
832 int type_align_in_units = 0;
833 int type_size_in_units;
834 int new_offset = 0;
835 int offset_overflow = 0;
836
837 /* VOIDmode is passed when computing the second argument to a `call'
838 pattern. This can be ignored. */
839 if (mode == VOIDmode)
840 return 0;
841
842 /* Compute the alignment and size of the parameter. */
843 type_align_in_units =
844 picochip_function_arg_boundary (mode, type) / BITS_PER_UNIT;
845 type_size_in_units = picochip_compute_arg_size (type, mode);
846
847 /* Compute the correct offset (i.e., ensure that the offset meets
848 the alignment requirements). */
849 offset_overflow = *cum % type_align_in_units;
850 if (offset_overflow == 0)
851 new_offset = *cum;
852 else
853 new_offset = (*cum - offset_overflow) + type_align_in_units;
854
855 if (TARGET_DEBUG)
856 {
857 printf ("Function arg:\n");
858 printf (" Type valid: %s\n", (type ? "yes" : "no"));
859 printf (" Cumulative Value: %d\n", *cum);
860 printf (" Mode: %s\n", GET_MODE_NAME (mode));
861 printf (" Type size: %i units\n", type_size_in_units);
862 printf (" Alignment: %i units\n", type_align_in_units);
863 printf (" New offset: %i\n", new_offset);
864 printf ("\n");
865 }
866
867 /* If the new offset is outside the register space, return. */
868 if (new_offset >= MAX_CALL_PARAMETER_REGS * 2)
869 return 0;
870
871 /* If the end of the argument is outside the register space, then
872 the argument must overlap the register space. Return the first
873 available register. */
874 if ((new_offset + type_size_in_units) > (MAX_CALL_PARAMETER_REGS * 2))
875 return gen_rtx_REG (HImode, new_offset / 2);
876
877 /* Create a register of the required mode to hold the parameter. */
878 reg = new_offset / 2;
879 switch (mode)
880 {
881 case QImode:
882 case HImode:
883 case SImode:
884 case SFmode:
885 case DImode:
886 case DFmode:
887 case SDmode:
888 case DDmode:
889 case CHImode:
890 case CSImode:
891 case SCmode:
892 case CQImode:
893 return gen_rtx_REG (mode, reg);
894
895 case BLKmode:
896 {
897 /* Empty blockmode values can be passed as arguments (e.g.,
898 * empty structs). These require no registers
899 * whatsoever. Non-empty blockmode values are passed in a set
900 * of parallel registers. */
901 if (type_size_in_units == 0)
902 return 0;
903 else
904 return picochip_emit_register_parallel (type_size_in_units, new_offset);
905 }
906
907 default:
908 warning
909 (0, "defaulting to stack for %s register creation",
910 GET_MODE_NAME (mode));
911 break;
912 }
913
914 return 0;
915
916 }
917
918 /* Determine where the next incoming function argument will
919 appear. Normally, this works in exactly the same way as
920 picochip_function_arg, except when the function in question is a
921 varadic function. In this case, the incoming arguments all appear
922 to be passed on the stack (actually, some of the arguments are
923 passed in registers, which are then pushed onto the stack by the
924 function prologue). */
925 rtx
926 picochip_incoming_function_arg (cumulative_args_t cum,
927 enum machine_mode mode,
928 const_tree type, bool named)
929 {
930
931 if (cfun->stdarg)
932 return 0;
933 else
934 return picochip_function_arg (cum, mode, type, named);
935
936 }
937
938 /* Gives the alignment boundary, in bits, of an argument with the
939 specified mode. */
940 unsigned int
941 picochip_function_arg_boundary (enum machine_mode mode,
942 const_tree type ATTRIBUTE_UNUSED)
943 {
944 int align;
945
946 if (mode == BLKmode)
947 align = STACK_BOUNDARY;
948 else
949 align = GET_MODE_ALIGNMENT (mode);
950
951 if (align < PARM_BOUNDARY)
952 align = PARM_BOUNDARY;
953
954 return align;
955
956 }
957
958 /* Compute partial registers. */
959 int
960 picochip_arg_partial_bytes (cumulative_args_t p_cum, enum machine_mode mode,
961 tree type, bool named ATTRIBUTE_UNUSED)
962 {
963 int type_align_in_units = 0;
964 int type_size_in_units;
965 int new_offset = 0;
966 int offset_overflow = 0;
967
968 unsigned cum = *get_cumulative_args (p_cum);
969
970 /* VOIDmode is passed when computing the second argument to a `call'
971 pattern. This can be ignored. */
972 if (mode == VOIDmode)
973 return 0;
974
975 /* Compute the alignment and size of the parameter. */
976 type_align_in_units =
977 picochip_function_arg_boundary (mode, type) / BITS_PER_UNIT;
978 type_size_in_units = picochip_compute_arg_size (type, mode);
979
980 /* Compute the correct offset (i.e., ensure that the offset meets
981 the alignment requirements). */
982 offset_overflow = cum % type_align_in_units;
983 if (offset_overflow == 0)
984 new_offset = cum;
985 else
986 new_offset = (cum - offset_overflow) + type_align_in_units;
987
988 if (TARGET_DEBUG)
989 {
990 printf ("Partial function arg nregs:\n");
991 printf (" Type valid: %s\n", (type ? "yes" : "no"));
992 printf (" Cumulative Value: %d\n", cum);
993 printf (" Mode: %s\n", GET_MODE_NAME (mode));
994 printf (" Type size: %i units\n", type_size_in_units);
995 printf (" Alignment: %i units\n", type_align_in_units);
996 printf (" New offset: %i\n", new_offset);
997 printf ("\n");
998 }
999
1000 /* If the new offset is outside the register space, return. */
1001 if (new_offset >= (MAX_CALL_PARAMETER_REGS * 2))
1002 return 0;
1003
1004 /* If the end of the argument is outside the register space, then
1005 the argument must overlap the register space. Return the number
1006 of bytes which are passed in registers. */
1007 if ((new_offset + type_size_in_units) > (MAX_CALL_PARAMETER_REGS * 2))
1008 return ((MAX_CALL_PARAMETER_REGS * 2) - new_offset);
1009
1010 return 0;
1011
1012 }
1013
1014 /* Advance the cumulative args counter CUM. */
1015 void
1016 picochip_arg_advance (cumulative_args_t cum_v, enum machine_mode mode,
1017 const_tree type, bool named ATTRIBUTE_UNUSED)
1018 {
1019 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
1020 int type_align_in_units = 0;
1021 int type_size_in_units;
1022 int new_offset = 0;
1023 int offset_overflow = 0;
1024
1025 /* VOIDmode is passed when computing the second argument to a `call'
1026 pattern. This can be ignored. */
1027 if (mode == VOIDmode)
1028 return;
1029
1030 /* Compute the alignment and size of the parameter. */
1031 type_align_in_units =
1032 picochip_function_arg_boundary (mode, type) / BITS_PER_UNIT;
1033 type_size_in_units = picochip_compute_arg_size (type, mode);
1034
1035 /* Compute the correct offset (i.e., ensure that the offset meets
1036 the alignment requirements). */
1037 offset_overflow = *cum % type_align_in_units;
1038 if (offset_overflow == 0)
1039 new_offset = *cum;
1040 else
1041 new_offset = (*cum - offset_overflow) + type_align_in_units;
1042
1043 /* Advance past the last argument. */
1044 new_offset += type_size_in_units;
1045
1046 *cum = new_offset;
1047 }
1048
1049 /* Determine whether a register needs saving/restoring. It does if it
1050 is live in a function, and isn't a call-used register. */
1051 static int
1052 picochip_reg_needs_saving (int reg_num)
1053 {
1054 return df_regs_ever_live_p(reg_num) && !call_used_regs[reg_num];
1055 }
1056
1057 /* Compute and return offset of the main frame. */
1058 static int
1059 picochip_frame_byte_offset (void)
1060 {
1061 gcc_assert(picochip_is_aligned
1062 (crtl->outgoing_args_size, BITS_PER_WORD));
1063
1064 return crtl->outgoing_args_size;
1065 }
1066
1067 /* Return the size of the main frame. */
1068 static int
1069 picochip_frame_size_in_bytes (void)
1070 {
1071 int frame_size = get_frame_size();
1072 int stack_align = STACK_BOUNDARY/BITS_PER_UNIT;
1073 if (!picochip_is_aligned (frame_size, STACK_BOUNDARY))
1074 frame_size = frame_size + (stack_align - frame_size%stack_align);
1075 gcc_assert(picochip_is_aligned (frame_size, STACK_BOUNDARY));
1076 return frame_size;
1077 }
1078
1079 /* Compute and return the size (in bytes) of the register save/restore
1080 area for the current function. This only includes the general
1081 purpose registers - the special purpose stack pointer and link
1082 registers are not included in this area. */
1083 static int
1084 picochip_save_area_size_in_bytes (void)
1085 {
1086 int num_regs_to_save = 0;
1087 int i = 0;
1088
1089 /* Read through all the registers, determining which need to be saved. */
1090 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1091 {
1092 if (picochip_reg_needs_saving (i))
1093 num_regs_to_save += 1;
1094 }
1095
1096 return num_regs_to_save * UNITS_PER_WORD;
1097
1098 }
1099
1100 /* Compute and return offset of the save area base. */
1101 static int
1102 picochip_save_area_byte_offset (void)
1103 {
1104 int base_offset = (picochip_frame_byte_offset () +
1105 picochip_frame_size_in_bytes ());
1106
1107 gcc_assert(picochip_is_aligned (base_offset, BITS_PER_WORD));
1108
1109 return base_offset;
1110
1111 }
1112
1113 /* Compute and return offset of the special register save area. This
1114 area can be found immediately above the normal save area. It must
1115 be aligned, to allow the registers to be saved and restored as a
1116 pair. */
1117 static int
1118 picochip_special_save_area_byte_offset (void)
1119 {
1120 int byte_alignment = STACK_BOUNDARY / BITS_PER_UNIT;
1121 int offset = (picochip_save_area_byte_offset () +
1122 picochip_save_area_size_in_bytes ());
1123
1124 if ((offset % byte_alignment) != 0)
1125 offset = ((offset / byte_alignment) + 1) * byte_alignment;
1126
1127 return offset;
1128
1129 }
1130
1131 /* Determine whether the LNK/SP register save/restores can be eliminated. */
1132 static int
1133 picochip_can_eliminate_link_sp_save (void)
1134 {
1135 /* This deserves some reasoning. The df_regs_ever_live_p call keeps
1136 changing during optimizations phases. So, this function returns different
1137 values when called from initial_elimination_offset and then again when it
1138 is called from prologue/epilogue generation. This means that argument
1139 accesses become wrong. This wouldnt happen only if we were not using the
1140 stack at all. The following conditions ensures that.*/
1141
1142 return (crtl->is_leaf &&
1143 !df_regs_ever_live_p(LINK_REGNUM) &&
1144 !df_regs_ever_live_p(STACK_POINTER_REGNUM) &&
1145 (picochip_special_save_area_byte_offset() == 0) &&
1146 (crtl->args.size == 0) &&
1147 (crtl->args.pretend_args_size == 0));
1148 }
1149
1150 /* Compute the size of the special reg save area (SP and LNK). If the
1151 SP/LNK registers don't need to be saved, this area can shrink to
1152 nothing. */
1153 static int
1154 picochip_special_save_area_size_in_bytes (void)
1155 {
1156
1157
1158 if (picochip_can_eliminate_link_sp_save ())
1159 return 0;
1160 else
1161 return 2 * UNITS_PER_WORD;
1162 }
1163
1164 /* Return the number of pretend arguments. If this function is
1165 varadic, all the incoming arguments are effectively passed on the
1166 stack. If this function has real pretend arguments (caused by a
1167 value being passed partially on the stack and partially in
1168 registers), then return the number of registers used. */
1169 static int
1170 picochip_pretend_arg_area_size (void)
1171 {
1172
1173 if (crtl->args.pretend_args_size != 0)
1174 {
1175 gcc_assert(crtl->args.pretend_args_size % 4 == 0);
1176
1177 return crtl->args.pretend_args_size;
1178 }
1179 else if (cfun->stdarg)
1180 return 12;
1181 else
1182 return 0;
1183
1184 }
1185
1186 /* Compute and return the offset of the pretend arguments. The pretend
1187 arguments are contiguous with the incoming arguments, and must be
1188 correctly aligned. */
1189 static int
1190 picochip_pretend_arg_area_byte_offset (void)
1191 {
1192 int base_offset = 0;
1193
1194 base_offset = (picochip_special_save_area_byte_offset () +
1195 picochip_special_save_area_size_in_bytes ());
1196
1197 gcc_assert(picochip_is_aligned (base_offset, STACK_BOUNDARY));
1198 gcc_assert(picochip_is_aligned
1199 (base_offset + picochip_pretend_arg_area_size (), STACK_BOUNDARY));
1200
1201 return base_offset;
1202
1203 }
1204
1205 /* Compute and return the offset of the incoming arguments. If a
1206 static chain is in use, this will be passed just before the other
1207 arguments. This means that the pretend argument mechanism, used in
1208 variadic functions, doesn't work properly. Thus, static chains work
1209 on their own, as do variadic functions, but not the combination of
1210 the two. This isn't really a problem. */
1211 static int
1212 picochip_arg_area_byte_offset (void)
1213 {
1214 int base_offset = (picochip_pretend_arg_area_byte_offset () +
1215 picochip_pretend_arg_area_size ());
1216
1217 /* Add an extra 4 bytes - only an extra 16-bits are required, but
1218 the alignment on a 32-bit boundary must be maintained. */
1219 if (cfun->static_chain_decl != NULL)
1220 {
1221 gcc_assert (!cfun->stdarg);
1222 base_offset += 4;
1223 }
1224
1225 gcc_assert(picochip_is_aligned (base_offset, STACK_BOUNDARY));
1226
1227 return base_offset;
1228
1229 }
1230
1231 int
1232 picochip_regno_nregs (int regno ATTRIBUTE_UNUSED, int mode)
1233 {
1234
1235 /* Special case - only one register needed. */
1236 if (GET_MODE_CLASS (mode) == MODE_CC)
1237 return 1;
1238
1239 /* We actually do not allocate acc0 ever. But, it seems like we need to
1240 make it look like a allocatable register for the dataflow checks to work
1241 properly. Note that hard_regno_mode_ok will always return 0 for acc0*/
1242
1243 if (regno == 16)
1244 return 1;
1245
1246 /* General case - compute how much space in terms of units. */
1247 return ((GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD);
1248
1249 }
1250
1251 int
1252 picochip_class_max_nregs (int reg_class, int mode)
1253 {
1254 int size = ((GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD);
1255
1256 if (reg_class == ACC_REGS)
1257 return 1;
1258
1259 if (GET_MODE_CLASS (mode) == MODE_CC)
1260 return 1;
1261 else
1262 return size;
1263
1264 }
1265
1266 /* Eliminate a register that addresses the stack (e.g., frame pointer,
1267 argument pointer) by replacing it with a constant offset from the
1268 main stack register. */
1269 int
1270 initial_elimination_offset (int from, int to)
1271 {
1272 int offset_from_sp = 0;
1273
1274 if (FRAME_POINTER_REGNUM == from && STACK_POINTER_REGNUM == to)
1275 offset_from_sp = picochip_frame_byte_offset ();
1276 else if (ARG_POINTER_REGNUM == from && STACK_POINTER_REGNUM == to)
1277 offset_from_sp = picochip_pretend_arg_area_byte_offset ();
1278 else
1279 gcc_unreachable();
1280
1281 return offset_from_sp;
1282
1283 }
1284
1285 /* Compute and return the size of the incoming argument area. */
1286 static int
1287 picochip_arg_area_size_in_bytes (void)
1288 {
1289 return crtl->args.size;
1290 }
1291 \f
1292 /* Determine whether the given register is valid. When the strict mode
1293 is used, only hard registers are valid, otherwise any register is
1294 valid. */
1295 static int
1296 picochip_legitimate_address_register (rtx x, unsigned strict)
1297 {
1298
1299 /* Sanity check - non-registers shouldn't make it here, but... */
1300 if (REG != GET_CODE (x))
1301 return 0;
1302
1303 if (strict)
1304 return REGNO (x) < FIRST_NONHARD_REGISTER;
1305 else
1306 return 1;
1307
1308 }
1309 \f
1310 /* Determine whether the given constant is in the range required for
1311 the given base register. */
1312 static int
1313 picochip_const_ok_for_base (enum machine_mode mode, int regno, int offset)
1314 {
1315 HOST_WIDE_INT corrected_offset;
1316
1317 if (GET_MODE_SIZE (mode) != 0)
1318 {
1319 if (GET_MODE_SIZE(mode) <= 4)
1320 {
1321 /* We used to allow incorrect offsets if strict is 0. But, this would
1322 then rely on reload doing the right thing. We have had problems
1323 there before, and on > 4.3 compiler, there are no benefits. */
1324 if (offset % GET_MODE_SIZE (mode) != 0)
1325 return 0;
1326 corrected_offset = offset / GET_MODE_SIZE (mode);
1327 }
1328 else
1329 {
1330 if (offset % 4 != 0)
1331 return 0;
1332 corrected_offset = offset / 4;
1333 }
1334 }
1335 else
1336 {
1337 /* Default to the byte offset as supplied. */
1338 corrected_offset = offset;
1339 }
1340
1341 /* The offset from the base register can be different depending upon
1342 the base register. The stack/frame/argument pointer offsets can
1343 all be greater than a simple register-based offset. Note that the
1344 frame/argument pointer registers are actually eliminations of the
1345 stack pointer, so a value which is valid for an offset to, for
1346 example, the frame pointer, might be invalid for the stack
1347 pointer once the elimination has occurred. However, there is no
1348 need to handle this special case here, as the stack offset is
1349 always checked after elimination anyway, and the generated code
1350 seems to have identical performance. */
1351 if (regno == STACK_POINTER_REGNUM ||
1352 regno == FRAME_POINTER_REGNUM || regno == ARG_POINTER_REGNUM)
1353 return picochip_const_ok_for_letter_p (corrected_offset, 'K');
1354 else
1355 return picochip_const_ok_for_letter_p (corrected_offset, 'J');
1356
1357 }
1358 \f
1359 /* Determine whether a given rtx is a legitimate address for machine_mode
1360 MODE. STRICT is non-zero if we're being strict - any pseudo that
1361 is not a hard register must be a memory reference. */
1362 bool
1363 picochip_legitimate_address_p (enum machine_mode mode, rtx x, bool strict)
1364 {
1365 int valid = 0;
1366
1367 switch (GET_CODE (x))
1368 {
1369 case REG:
1370 valid = picochip_legitimate_address_register (x, strict);
1371 break;
1372
1373 case PLUS:
1374 {
1375 rtx base = XEXP (x, 0);
1376 rtx offset = XEXP (x, 1);
1377 if (strict && !REGNO_OK_FOR_BASE_P (REGNO(base)))
1378 {
1379 valid = 0;
1380 break;
1381 }
1382
1383 valid = (REG == GET_CODE (base) &&
1384 picochip_legitimate_address_register (base, strict) &&
1385 CONST_INT == GET_CODE (offset) &&
1386 picochip_const_ok_for_base (mode, REGNO (base),
1387 INTVAL (offset)));
1388 break;
1389 }
1390
1391 case SYMBOL_REF:
1392 /* The user can select whether a symbol can be used as a memory
1393 address. Typically, this will decrease execution time (no
1394 register load is required first), but will increase code size
1395 (because the symbol will be used several times, rather than
1396 loaded once into a register.*/
1397 valid = TARGET_SYMBOL_AS_ADDRESS;
1398 break;
1399
1400 case CONST:
1401 {
1402 /* A constant memory address must be a (plus (symbol_ref)
1403 (const_int)), and is only allowed when the symbols are
1404 permitted addresses. */
1405 rtx inner = XEXP (x, 0);
1406
1407 valid = (TARGET_SYMBOL_AS_ADDRESS &&
1408 PLUS == GET_CODE (inner) &&
1409 SYMBOL_REF == GET_CODE (XEXP (inner, 0)) &&
1410 CONST_INT == GET_CODE (XEXP (inner, 1)));
1411
1412 break;
1413
1414 }
1415
1416 default:
1417 valid = 0;
1418 }
1419
1420 return valid;
1421
1422 }
1423
1424 /* For all memory operations, picochip allows a uconst4 offset value. It
1425 is hence beneficial to turn an
1426 addr = <reg + long_const>
1427 ld/st addr
1428
1429 into
1430
1431 X = reg + long_const & FFF0
1432 diff = long_const - (long_const & FFF0)
1433 ld/st <X + diff>
1434
1435 X can be reused in subsequent memory operations.
1436 */
1437 rtx
1438 picochip_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
1439 enum machine_mode mode)
1440 {
1441 unsigned mask_val;
1442
1443 if (!optimize)
1444 return x;
1445
1446 /* Depending on mode, the offsets allowed are either 16/32/64.*/
1447 switch (mode)
1448 {
1449 case QImode:
1450 mask_val = 0xFFF0;
1451 break;
1452 case HImode:
1453 mask_val = 0xFFE0;
1454 break;
1455 case SImode:
1456 mask_val = 0xFFC0;
1457 break;
1458 default:
1459 return x;
1460 }
1461
1462 if (GET_CODE (x) == PLUS
1463 && GET_CODE (XEXP (x, 0)) == REG
1464 && GET_CODE (XEXP (x, 1)) == CONST_INT)
1465 {
1466 int high_val, low_val, offset;
1467 offset = INTVAL (XEXP (x, 1));
1468 /* Ignore cases with negative offsets. */
1469 if (offset < 0)
1470 return x;
1471 high_val = offset & mask_val;
1472 low_val = offset - high_val;
1473 if (high_val != 0)
1474 {
1475 rtx temp_reg = force_reg (Pmode, gen_rtx_PLUS (Pmode, XEXP (x, 0), GEN_INT(high_val)));
1476 x = gen_rtx_PLUS (Pmode, temp_reg, GEN_INT(low_val));
1477 return x;
1478 }
1479 }
1480 return x;
1481 }
1482
1483 /* For all memory operations, picochip allows a uconst4 offset value. It
1484 is hence beneficial to turn an
1485 addr = <reg + long_const>
1486 ld/st addr
1487
1488 into
1489
1490 X = reg + long_const & FFF0
1491 diff = long_const - (long_const & FFF0)
1492 ld/st <X + diff>
1493
1494 X can be reused in subsequent memory operations.
1495 */
1496 int
1497 picochip_legitimize_reload_address (rtx *x,
1498 enum machine_mode mode,
1499 int opnum, int type,
1500 int ind_levels ATTRIBUTE_UNUSED)
1501 {
1502 unsigned mask_val;
1503
1504 if (picochip_symbol_offset(*x))
1505 {
1506 *x = gen_rtx_CONST(mode, *x);
1507 return 0;
1508 }
1509 if (!optimize)
1510 return 0;
1511
1512 /* We should recognise addresses that we created.*/
1513 if (GET_CODE (*x) == PLUS
1514 && GET_CODE (XEXP (*x, 0)) == PLUS
1515 && GET_CODE (XEXP (XEXP (*x, 0), 0)) == REG
1516 && GET_CODE (XEXP (XEXP (*x, 0), 1)) == CONST_INT
1517 && GET_CODE (XEXP (*x, 1)) == CONST_INT)
1518 {
1519 push_reload (XEXP (*x, 0), NULL_RTX, &XEXP (*x, 0), NULL,
1520 BASE_REG_CLASS, GET_MODE (*x), VOIDmode, 0, 0,
1521 opnum, (enum reload_type)type);
1522 return 1;
1523 }
1524
1525 /* Depending on mode, the offsets allowed are either 16/32/64. */
1526 switch (mode)
1527 {
1528 case QImode:
1529 mask_val = 0xFFF0;
1530 break;
1531 case HImode:
1532 mask_val = 0xFFE0;
1533 break;
1534 case SImode:
1535 mask_val = 0xFFC0;
1536 break;
1537 default:
1538 return 0;
1539 }
1540
1541 if (GET_CODE (*x) == PLUS
1542 && GET_CODE (XEXP (*x, 0)) == REG
1543 && GET_CODE (XEXP (*x, 1)) == CONST_INT)
1544 {
1545 int high_val, low_val, offset;
1546 offset = INTVAL (XEXP (*x, 1));
1547 /* Ignore cases with negative offsets. */
1548 if (offset < 0)
1549 return 0;
1550 high_val = offset & mask_val;
1551 low_val = offset - high_val;
1552 if (high_val != 0)
1553 {
1554 rtx temp_reg = gen_rtx_PLUS (Pmode, XEXP (*x, 0), GEN_INT(high_val));
1555 *x = gen_rtx_PLUS (Pmode, temp_reg, GEN_INT(low_val));
1556 push_reload (XEXP (*x, 0), NULL_RTX, &XEXP (*x, 0), NULL,
1557 BASE_REG_CLASS, GET_MODE (*x), VOIDmode, 0, 0,
1558 opnum, (enum reload_type)type);
1559 return 1;
1560 }
1561 }
1562
1563 return 0;
1564 }
1565
1566 /* Detect an rtx which matches (plus (symbol_ref) (const_int)). */
1567 int
1568 picochip_symbol_offset (rtx operand)
1569 {
1570
1571 return (PLUS == GET_CODE (operand) &&
1572 SYMBOL_REF == GET_CODE (XEXP (operand, 0)) &&
1573 CONST_INT == GET_CODE (XEXP (operand, 1)));
1574
1575 }
1576 \f
1577 /* Assembly output. */
1578
1579 /* The format here should match the format used in the output of
1580 symbol_ref's elsewhere in this file. */
1581 void
1582 picochip_output_label (FILE * stream, const char name[])
1583 {
1584 int is_cfi_label = (strncmp (name, "picoMark_LCFI", 13) == 0);
1585
1586 /* If VLIW scheduling is in use, any Call Frame Information labels
1587 generated inside a packet must have their output deferred until
1588 the end of the packet. */
1589 if (picochip_schedule_type == DFA_TYPE_SPEED &&
1590 is_cfi_label && picochip_vliw_continuation)
1591 {
1592 if (picochip_current_vliw_state.num_cfi_labels_deferred == 2)
1593 {
1594 internal_error ("LCFI labels have already been deferred");
1595 }
1596 strcpy (picochip_current_vliw_state.cfi_label_name[
1597 picochip_current_vliw_state.num_cfi_labels_deferred], name);
1598 picochip_current_vliw_state.num_cfi_labels_deferred++;
1599 }
1600 else
1601 {
1602 assemble_name (stream, name);
1603
1604 if (strncmp (name, "picoMark_", 9) == 0)
1605 fprintf (stream, "=\n");
1606 else
1607 fprintf (stream, ":\n");
1608
1609 }
1610
1611 }
1612
1613 /* The format here should match the format used in the output of
1614 symbol_ref's elsewhere in this file. */
1615 void
1616 picochip_output_labelref (FILE * stream, const char name[])
1617 {
1618 fprintf (stream, "_%s", name);
1619 }
1620
1621 void
1622 picochip_weaken_label (FILE * stream, const char name[])
1623 {
1624 fprintf (stream, ".weak ");
1625 assemble_name (stream, name);
1626 fprintf (stream, "\n");
1627 }
1628
1629 /* Return true if the given label (or label prefix) denotes a marker
1630 label which should be emitted in the form LABEL= */
1631 static int
1632 picochip_is_marker_prefix (const char *prefix)
1633 {
1634 return (strcmp (prefix, "L") != 0 && strcmp (prefix, "LC") != 0
1635 && strcmp (prefix, "LP") != 0);
1636 }
1637
1638 void
1639 picochip_output_internal_label (FILE * stream, const char *prefix,
1640 unsigned long num)
1641 {
1642
1643 /* Emit different types of label, based upon their prefix. They
1644 are handled differently to allow the assembler to ensure that
1645 branch target labels are properly aligned, while other labels
1646 will only serve as code markers, not branch targets. Aligning
1647 labels unnecessarily can result in much code wastage. */
1648 if (picochip_is_marker_prefix (prefix))
1649 {
1650 /* Special label marker. If it appears in the middle of a VLIW
1651 packet, defer it until the end of the packet. There has
1652 never been a need to handle more than one lm label at a time. */
1653 if (picochip_schedule_type == DFA_TYPE_SPEED &&
1654 (strcmp (prefix, "LM")) == 0 && picochip_vliw_continuation)
1655 {
1656 if (strlen (picochip_current_vliw_state.lm_label_name) != 0)
1657 internal_error ("LM label has already been deferred");
1658
1659 sprintf (picochip_current_vliw_state.lm_label_name,
1660 "picoMark_%s%ld", prefix, num);
1661 }
1662 else if (picochip_schedule_type == DFA_TYPE_SPEED &&
1663 (strcmp (prefix, "LCFI")) == 0 && picochip_vliw_continuation)
1664 {
1665 if (picochip_current_vliw_state.num_cfi_labels_deferred == 2)
1666 {
1667 internal_error ("LCFI labels have already been deferred.");
1668 }
1669 sprintf(picochip_current_vliw_state.cfi_label_name[
1670 picochip_current_vliw_state.num_cfi_labels_deferred],
1671 "picoMark_%s%ld", prefix, num);
1672 picochip_current_vliw_state.num_cfi_labels_deferred++;
1673 }
1674 else
1675 {
1676 /* Marker label. */
1677 fprintf (stream, "_picoMark_%s%ld=\n", prefix, num);
1678 }
1679
1680 }
1681 else
1682 {
1683 /* Normal label. */
1684 fprintf (stream, "_%s%ld:\n", prefix, num);
1685 }
1686
1687 }
1688
1689 void
1690 picochip_generate_internal_label (char *str, const char *prefix, long num)
1691 {
1692 /* Two types of internal label can be generated: branch target
1693 labels and code marker labels. Branch target labels must always
1694 be aligned (since code will execute at these
1695 points). Differentiate between the two by prepending markers with
1696 a unique prefix, which can later be used in output_label to
1697 figure out which label syntax to use. */
1698 if (picochip_is_marker_prefix (prefix))
1699 sprintf (str, "picoMark_%s%ld", prefix, num);
1700 else
1701 sprintf (str, "%s%ld", prefix, num);
1702
1703 }
1704
1705 void
1706 picochip_asm_output_anchor (rtx symbol)
1707 {
1708 fprintf (asm_out_file, ".offsetData _%s, ",XSTR (symbol, 0));
1709 fprintf (asm_out_file, "+ " HOST_WIDE_INT_PRINT_DEC"\n",SYMBOL_REF_BLOCK_OFFSET(symbol));
1710 }
1711
1712 void
1713 picochip_output_aligned_common (FILE * stream, const char *name,
1714 unsigned size, unsigned alignment)
1715 {
1716
1717 fprintf (stream, ".commonData ");
1718 assemble_name (stream, name);
1719 fprintf (stream, ", %u, %u\n", size, alignment / 8);
1720 picochip_output_global (stream, name);
1721
1722 }
1723
1724 void
1725 picochip_output_aligned_local (FILE * stream, const char *name,
1726 unsigned size, unsigned alignment)
1727 {
1728
1729 fprintf (stream, ".commonData ");
1730 assemble_name (stream, name);
1731 fprintf (stream, ", %u, %u\n", size, alignment / 8);
1732
1733 }
1734
1735 void
1736 picochip_output_global (FILE * stream, const char *name)
1737 {
1738 fprintf (stream, ".global ");
1739 assemble_name (stream, name);
1740 fprintf (stream, "\n");
1741 }
1742
1743 /* Output an assembly language string. Output as a sequence of decimal
1744 numbers, followed by the literal string to make it obvious what the
1745 numbers represent. */
1746 void
1747 picochip_output_ascii (FILE * file, const char *str, int length)
1748 {
1749 int i = 0;
1750
1751 fprintf (file, ".ascii ");
1752
1753 for (i = 0; i < length; ++i)
1754 {
1755 fprintf (file, "16#%x# ", (char) (str[i]));
1756 }
1757
1758 fprintf (file, " ; ");
1759
1760 for (i = 0; i < length; ++i)
1761 {
1762 char c = str[i];
1763
1764 switch (c)
1765 {
1766 case '\n':
1767 fprintf (file, "\\n");
1768 break;
1769 case '\t':
1770 fprintf (file, "\\t");
1771 break;
1772 case '\0':
1773 fprintf (file, "\\0");
1774 break;
1775 default:
1776 fprintf (file, "%c", c);
1777 }
1778
1779 }
1780
1781 fprintf (file, "\n");
1782
1783 }
1784
1785 /* Output the beginning of an ASM file. */
1786 void
1787 picochip_asm_file_start (void)
1788 {
1789 default_file_start ();
1790
1791 fprintf (asm_out_file, "// picoChip ASM file\n");
1792 fprintf (asm_out_file, "//.file \"%s\"\n", main_input_filename);
1793
1794 fprintf (asm_out_file, "// Has byte access: %s\n",
1795 (TARGET_HAS_BYTE_ACCESS ? "Yes" : "No"));
1796
1797 if (TARGET_HAS_MUL_UNIT)
1798 fprintf (asm_out_file, "// Has multiply: Yes (Multiply unit)\n");
1799 else if (TARGET_HAS_MAC_UNIT)
1800 fprintf (asm_out_file, "// Has multiply: Yes (Mac unit)\n");
1801 else
1802 fprintf (asm_out_file, "// Has multiply: No\n");
1803 }
1804
1805 /* Output the end of an ASM file. */
1806 void
1807 picochip_asm_file_end (void)
1808 {
1809 /* Include a segment end to make it easy for PERL scripts to grab
1810 segments. This is now done by assembler*/
1811
1812 fprintf (asm_out_file, "// End of picoChip ASM file\n");
1813
1814 }
1815
1816 /* Output frame debug information to the given stream. */
1817 static void
1818 picochip_output_frame_debug (FILE * file)
1819 {
1820 int i = 0;
1821
1822 if (crtl->is_leaf)
1823 fprintf (file, "\t\t// Leaf function\n");
1824 else
1825 fprintf (file, "\t\t// Non-leaf function\n");
1826
1827 if (picochip_can_eliminate_link_sp_save ())
1828 fprintf (file, "\t\t// Link/fp save/restore can be eliminated\n");
1829
1830 if (cfun->static_chain_decl != NULL)
1831 fprintf (file, "\t\t// Static chain in use\n");
1832
1833 fprintf (file, "\t\t// Incoming argument size: %d bytes\n",
1834 picochip_arg_area_size_in_bytes ());
1835 fprintf (file, "\t\t// Incoming arg offset: %d\n",
1836 picochip_arg_area_byte_offset ());
1837 fprintf (file, "\t\t// Pretend arg size: %d\n",
1838 picochip_pretend_arg_area_size ());
1839 fprintf (file, "\t\t// Pretend arg offset (ARGP): %d\n",
1840 picochip_pretend_arg_area_byte_offset ());
1841 fprintf (file, "\t\t// Special reg area size: %d bytes\n",
1842 picochip_special_save_area_size_in_bytes ());
1843 fprintf (file, "\t\t// Special reg area offset: %d\n",
1844 picochip_special_save_area_byte_offset ());
1845
1846 /* Output which registers are saved. */
1847 fprintf (file, "\t\t// Saved regs: ");
1848 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1849 {
1850 if (picochip_reg_needs_saving (i))
1851 fprintf (file, "%s ", picochip_regnames[i]);
1852 }
1853 fprintf (file, "\t\t\n");
1854
1855 fprintf (file, "\t\t// Save area size: %d bytes\n",
1856 picochip_save_area_size_in_bytes ());
1857 fprintf (file, "\t\t// Save area offset: %d\n",
1858 picochip_save_area_byte_offset ());
1859
1860 fprintf (file, "\t\t// Frame size: %ld bytes\n", get_frame_size ());
1861 fprintf (file, "\t\t// Frame offset (FP): %d\n",
1862 picochip_frame_byte_offset ());
1863
1864 fprintf (file, "\t\t// Outgoing argument area size: %d bytes\n",
1865 crtl->outgoing_args_size);
1866
1867 }
1868
1869 /* Output picoChip function prologue. This contains human-readable
1870 information about the function. */
1871 void
1872 picochip_function_prologue (FILE * file, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
1873 {
1874 /* Get the function's name, as described by its RTL. This may be
1875 different from the DECL_NAME name used in the source file. The
1876 real declaration name must be used, to ensure that the prologue
1877 emits the right information for the linker. */
1878 rtx x;
1879 const char *fnname;
1880 x = DECL_RTL (current_function_decl);
1881 gcc_assert (MEM_P (x));
1882 x = XEXP (x, 0);
1883 gcc_assert (GET_CODE (x) == SYMBOL_REF);
1884 fnname = XSTR (x, 0);
1885
1886 /* Note that the name of the function is given in the &_%s
1887 form. This matches the name of the function as used in labels,
1888 and function calls, and enables processCallGraph to match
1889 function calls to the name of the function, as defined here. */
1890 fprintf (file, "// picoChip Function Prologue : &_%s = %d bytes\n",
1891 fnname, picochip_arg_area_byte_offset ());
1892
1893 picochip_output_frame_debug (file);
1894 fprintf (file, "\n");
1895
1896 }
1897
1898 /* Output picoChip function epilogue. */
1899 void
1900 picochip_function_epilogue (FILE * file, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
1901 {
1902
1903 rtx x;
1904 const char *fnname;
1905 x = DECL_RTL (current_function_decl);
1906 gcc_assert (MEM_P (x));
1907 x = XEXP (x, 0);
1908 gcc_assert (GET_CODE (x) == SYMBOL_REF);
1909 fnname = XSTR (x, 0);
1910 fprintf (file, "\n// picoChip Function Epilogue : %s\n\n",
1911 fnname);
1912 }
1913
1914 /* Manipulate the asm output. Some machines only execute the code when
1915 there is actually a chance of needing it (e.g., FRV doesn't execute
1916 it if the scheduling pass wasn't used). We always execute it,
1917 simple to ensure that it is exercised more often, and bugs are more
1918 likely to be found.
1919
1920 This function's prime reason for existence is to insert the VLIW
1921 separators where appropriate. The separators must be inserted
1922 before any comments which appear at the end of the file.
1923
1924 */
1925 const char *
1926 picochip_asm_output_opcode (FILE * f, const char *ptr)
1927 {
1928 int c;
1929
1930 /* Flag to specify when a VLIW continuation has been inserted onto
1931 the line. Continuations are either inserted before any comments,
1932 or before the end of the line is reached. The flag ensures that
1933 we don't insert continuations twice (i.e., at the comment and the
1934 end of line). */
1935 int continuation_inserted = 0;
1936
1937 /* If the instruction uses multiple lines (i.e., a new line
1938 character appears in the opcode), then ensure that no attempt is
1939 made to pack it into a VLIW. */
1940 if (strchr (ptr, '\n') != NULL && picochip_vliw_continuation)
1941 internal_error
1942 ("picochip_asm_output_opcode - Found multiple lines in VLIW packet %s",
1943 ptr);
1944
1945
1946 /* If a delay slot is pending, output the directive to the assembler
1947 before the instruction. */
1948 if (picochip_is_delay_slot_pending)
1949 {
1950 picochip_is_delay_slot_pending = 0;
1951 fputs ("=->\t", f);
1952 }
1953
1954 /* Keep going for entire opcode. All substitution performed ourselves. */
1955 while (*ptr)
1956 {
1957 c = *ptr++;
1958
1959 /* Determine whether a VLIW continuation must be inserted before
1960 any comments, or the end of the opcode. A flag is set to show
1961 that we have inserted a continuation on this line, so that we
1962 don't try to insert another continuation when the end of the
1963 opcode is reached. The only other case for a continuation
1964 might have been a newline, but these aren't allowed in
1965 conjunction with VLIW continuations (see above code). */
1966 if (picochip_vliw_continuation &&
1967 !continuation_inserted &&
1968 ((c == '/' && (*ptr == '/')) || *ptr == '\0'))
1969 {
1970 fprintf (f, "\\ ");
1971 continuation_inserted = 1;
1972 }
1973
1974 /* Detect an explicit VLIW separator. */
1975 if (c == '%' && (*ptr == '|'))
1976 {
1977 fprintf (f, "\\");
1978 ptr++;
1979 }
1980 /* Detect the need for an ALU id operand. */
1981 else if (c == '%' && (*ptr == '#'))
1982 {
1983 fputc (picochip_get_vliw_alu_id (), f);
1984
1985 if (TARGET_DEBUG)
1986 printf ("Generated ALU char at %s for insn %d\n", ptr,
1987 INSN_UID (picochip_current_prescan_insn));
1988
1989 /* Skip past unwanted # */
1990 ptr++;
1991 }
1992 /* Detect the need for branch delay slot. */
1993 else if (c == '%' && (*ptr == '>'))
1994 {
1995 /* Only emit delay slots (NOP's, or otherwise) when delay
1996 * slot scheduling has actually been enabled, otherwise VLIW
1997 * scheduling and delay slot scheduling output combine to
1998 * produce nasty effects. */
1999 if (flag_delayed_branch)
2000 {
2001 if (dbr_sequence_length () == 0)
2002 fputs ("\n=->\tNOP", f);
2003 else
2004 picochip_is_delay_slot_pending = 1;
2005 }
2006
2007 /* Skip past unwanted > */
2008 ptr++;
2009 }
2010 /* Detect any %digit specifiers. */
2011 else if (c == '%' && (*ptr >= '0' && *ptr <= '9'))
2012 {
2013 c = atoi (ptr);
2014 picochip_print_operand (f, recog_data.operand[c], 0);
2015 while ((c = *ptr) >= '0' && c <= '9')
2016 ptr++;
2017 }
2018 /* Detect any %letterdigit specifiers. */
2019 else if (c == '%' && ((*ptr >= 'a' && *ptr <= 'z')
2020 || (*ptr >= 'A' && *ptr <= 'Z')))
2021 {
2022 int letter = *ptr++;
2023
2024 c = atoi (ptr);
2025
2026 switch (letter)
2027 {
2028 case 'l':
2029 output_asm_label (recog_data.operand[c]);
2030 break;
2031
2032 case 'a':
2033 output_address (recog_data.operand[c]);
2034 break;
2035
2036 default:
2037 picochip_print_operand (f, recog_data.operand[c], letter);
2038 }
2039
2040 while ((c = *ptr) >= '0' && c <= '9')
2041 ptr++;
2042 }
2043 else if (c == '%')
2044 internal_error
2045 ("picochip_asm_output_opcode - can%'t output unknown operator %c",
2046 *ptr);
2047 else
2048 fputc (c, f);
2049 }
2050
2051 /* Reached the end of the packet. If any labels were deferred
2052 during output, emit them now. */
2053 if (!picochip_vliw_continuation)
2054 {
2055 if (picochip_current_vliw_state.num_cfi_labels_deferred != 0)
2056 {
2057 fprintf (f, "\n");
2058 assemble_name (f, picochip_current_vliw_state.cfi_label_name[0]);
2059 fprintf (f, "=");
2060 if (picochip_current_vliw_state.num_cfi_labels_deferred == 2)
2061 {
2062 fprintf (f, "\n");
2063 assemble_name (f, picochip_current_vliw_state.cfi_label_name[1]);
2064 fprintf (f, "=");
2065 }
2066 }
2067
2068 if (strlen (picochip_current_vliw_state.lm_label_name) != 0)
2069 {
2070 fprintf (f, "\n");
2071 assemble_name (f, picochip_current_vliw_state.lm_label_name);
2072 fprintf (f, "=");
2073 }
2074 }
2075
2076 /* Output an end-of-packet marker if requested. */
2077 if (!picochip_vliw_continuation &&
2078 TARGET_DEBUG && picochip_schedule_type == DFA_TYPE_SPEED)
2079 fprintf (f, "\n\t//-------------- End of VLIW packet -----------------");
2080
2081 return ptr;
2082 }
2083 \f
2084 /* Function RTL expansion. */
2085
2086 /* Expand the prologue into RTL. */
2087 void
2088 picochip_expand_prologue (void)
2089 {
2090 int stack_adjustment = 0;
2091 int special_save_offset = 0;
2092 int general_save_offset = 0;
2093 int reg_save_offset = 0;
2094 int i = 0;
2095
2096 stack_adjustment = picochip_arg_area_byte_offset ();
2097 general_save_offset =
2098 -(stack_adjustment - picochip_save_area_byte_offset ());
2099 special_save_offset =
2100 -(stack_adjustment - picochip_special_save_area_byte_offset ());
2101
2102 /* Save the link registers. We could try to save just one register
2103 here. This would reduce the amount of stack space required.
2104 There hasn't been a good reason to do that so far. */
2105 if (!picochip_can_eliminate_link_sp_save ())
2106 picochip_emit_save_register (gen_rtx_REG (SImode, LINK_REGNUM),
2107 special_save_offset);
2108
2109 /* Save callee-save registers. */
2110 reg_save_offset = 0;
2111 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2112 {
2113 if (picochip_reg_needs_saving (i))
2114 {
2115
2116 /* If this register is an even numbered register, and the
2117 next register also needs to be saved, use a SImode save,
2118 which does both in one instruction. Note that a special
2119 check is performed to ensure that the double word aligned
2120 store is valid (e.g., it is possible that r6, r8, r9 need
2121 to be saved, in which case once r6 has been saved, the
2122 stored offset is no longer aligned, and an STL/LDL
2123 instruction becomes invalid). Alternately, we could store all
2124 aligned registers first and then save the single one(s). */
2125 if ((i % 2 == 0) &&
2126 picochip_reg_needs_saving (i + 1) &&
2127 picochip_is_aligned (reg_save_offset, LONG_TYPE_SIZE))
2128 {
2129 picochip_emit_save_register (gen_rtx_REG (SImode, i),
2130 general_save_offset +
2131 reg_save_offset);
2132 reg_save_offset += 2 * UNITS_PER_WORD;
2133 i++;
2134 }
2135 else
2136 {
2137 picochip_emit_save_register (gen_rtx_REG (HImode, i),
2138 general_save_offset +
2139 reg_save_offset);
2140 reg_save_offset += UNITS_PER_WORD;
2141 }
2142 }
2143
2144 }
2145
2146 /* Emit a stack adjustment where required. */
2147 if (stack_adjustment != 0)
2148 picochip_emit_stack_allocate (stack_adjustment);
2149
2150 /* If this function uses varadic arguments, write any unnamed
2151 registers to the stack. */
2152 if (cfun->stdarg)
2153 {
2154 int stdarg_offset = picochip_pretend_arg_area_byte_offset ();
2155
2156 /* Sanity check. The pretend argument offset should be 32-bit aligned. */
2157 gcc_assert(picochip_pretend_arg_area_byte_offset () % 4 == 0);
2158
2159 picochip_emit_save_register (gen_rtx_REG (SImode, 0), stdarg_offset);
2160 picochip_emit_save_register (gen_rtx_REG (SImode, 2),
2161 stdarg_offset + 4);
2162 picochip_emit_save_register (gen_rtx_REG (SImode, 4),
2163 stdarg_offset + 8);
2164
2165 }
2166
2167 }
2168
2169 /* Expand the epilogue into RTL. */
2170 void
2171 picochip_expand_epilogue (int is_sibling_call ATTRIBUTE_UNUSED)
2172 {
2173 int stack_adjustment = 0;
2174 int special_save_offset = 0;
2175 int general_save_offset = 0;
2176 int reg_save_offset = 0;
2177 int i = 0;
2178 int use_link_fp_restore_stack_adjust = 0; /* Default to using an explicit
2179 stack restore. */
2180
2181 stack_adjustment = picochip_arg_area_byte_offset ();
2182 general_save_offset =
2183 -(stack_adjustment - picochip_save_area_byte_offset ());
2184 special_save_offset =
2185 -(stack_adjustment - picochip_special_save_area_byte_offset ());
2186
2187 /* Emit a stack adjustment where required. */
2188 if (stack_adjustment != 0)
2189 {
2190 /* If the link/fp is already being restored, and the offset to
2191 their save location is small enough, don't bother adjusting
2192 the stack explicitly. */
2193 if (picochip_special_save_area_byte_offset () < 512 &&
2194 !picochip_can_eliminate_link_sp_save ())
2195 use_link_fp_restore_stack_adjust = 1;
2196 else
2197 /* Explicitly restore the stack. */
2198 picochip_emit_stack_allocate (-stack_adjustment);
2199 }
2200
2201 /* Restore the Link/FP registers. Only save the link register? */
2202 if (!picochip_can_eliminate_link_sp_save ())
2203 {
2204 if (use_link_fp_restore_stack_adjust)
2205 picochip_emit_restore_register (gen_rtx_REG (SImode, LINK_REGNUM),
2206 picochip_special_save_area_byte_offset
2207 ());
2208 else
2209 picochip_emit_restore_register (gen_rtx_REG (SImode, LINK_REGNUM),
2210 special_save_offset);
2211 }
2212
2213 /* Restore callee-save registers. */
2214 reg_save_offset = 0;
2215 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2216 {
2217 if (picochip_reg_needs_saving (i))
2218 {
2219
2220 /* If this register is an even numbered register, and the
2221 next register also needs to be saved, use a SImode save,
2222 which does both in one instruction. Note that a special
2223 check is performed to ensure that the double word aligned
2224 store is valid (e.g., it is possible that r6, r8, r9 need
2225 to be saved, in which case once r6 has been saved, the
2226 stored offset is no longer aligned, and an STL/LDL
2227 instruction becomes invalid). We could store all aligned
2228 registers first, and then save the single one(s). */
2229 if ((i % 2 == 0) &&
2230 picochip_reg_needs_saving (i + 1) &&
2231 picochip_is_aligned (reg_save_offset, LONG_TYPE_SIZE))
2232 {
2233 picochip_emit_restore_register (gen_rtx_REG (SImode, i),
2234 general_save_offset +
2235 reg_save_offset);
2236 reg_save_offset += 2 * UNITS_PER_WORD;
2237 i++;
2238 }
2239 else
2240 {
2241 picochip_emit_restore_register (gen_rtx_REG (HImode, i),
2242 general_save_offset +
2243 reg_save_offset);
2244 reg_save_offset += UNITS_PER_WORD;
2245 }
2246 }
2247
2248 }
2249
2250 /* Emit a return instruction, which matches a (parallel
2251 [(return) (use r12)]) */
2252 {
2253 rtvec p;
2254 p = rtvec_alloc (2);
2255
2256 RTVEC_ELT (p, 0) = ret_rtx;
2257 RTVEC_ELT (p, 1) = gen_rtx_USE (VOIDmode,
2258 gen_rtx_REG (Pmode, LINK_REGNUM));
2259 emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
2260 }
2261
2262 }
2263 \f
2264 /* Assembly instruction output. */
2265
2266 /* Test whether the given branch instruction is short, or long. Short
2267 * branches are equivalent to real branches, and may be DFA
2268 * scheduled. Long branches expand to a macro which is handled by the
2269 * elaborator, and cannot be scheduled. Occasionally, the branch
2270 * shortening pass, which is run after DFA scheduling, will change the
2271 * code layout and cause the short branch to be reverted into a long
2272 * branch. Instead of having to fix this up by emitting new assembly,
2273 * the short branch is emitted anyway. There is plenty of slack in the
2274 * calculation of long and short branches (10-bit offset, but only
2275 * 9-bits used in computation), so there is enough slack for this to
2276 * be safe. */
2277 static int
2278 picochip_is_short_branch (rtx insn)
2279 {
2280 int isRealShortBranch = (get_attr_length(insn) == SHORT_BRANCH_LENGTH);
2281
2282 return (isRealShortBranch ||
2283 picochip_current_vliw_state.num_insns_in_packet > 1);
2284 }
2285
2286 /* Output a compare-and-branch instruction (matching the cbranch
2287 pattern). */
2288 const char *
2289 picochip_output_cbranch (rtx operands[])
2290 {
2291
2292 if (HImode != GET_MODE (operands[1]) ||
2293 (HImode != GET_MODE (operands[2]) &&
2294 GET_CODE (operands[2]) != CONST_INT))
2295 {
2296 internal_error ("%s: at least one operand can%'t be handled",
2297 __FUNCTION__);
2298 }
2299
2300 /* Use the type of comparison to output the appropriate condition
2301 test. */
2302 switch (GET_CODE (operands[0]))
2303 {
2304 case NE:
2305 return ("// if (%1 != %2) goto %l3\n\tSUB.%# %1,%2,r15\n\tJMPNE %l3");
2306
2307 case EQ:
2308 return ("// if (%1 == %2) goto %l3\n\tSUB.%# %1,%2,r15\n\tJMPEQ %l3");
2309
2310 case LE:
2311 /* Reverse the operand order to be GE */
2312 return ("// if (%1 <= %2) goto %l3\n\tSUB.%# %2,%1,r15\n\tJMPGE %l3");
2313
2314 case LEU:
2315 /* Reverse operand order of GEU. */
2316 return ("// if (%1 <= %2) goto %l3\n\tSUB.%# %2,%1,r15\n\tJMPHS %l3");
2317
2318 case GE:
2319 return ("// if (%1 >= %2) goto %l3\n\tSUB.%# %1,%2,r15\n\tJMPGE %l3");
2320
2321 case GEU:
2322 return ("// if (%1 >= %2) goto %l3\n\tSUB.%# %1,%2,r15\n\tJMPHS %l3");
2323
2324 case LT:
2325 return ("// if (%1 < %2) goto %l3\n\tSUB.%# %1,%2,r15\n\tJMPLT %l3");
2326
2327 case LTU:
2328 return ("// if (%1 <{U} %2) goto %l3\n\tSUB.%# %1,%2,r15\n\tJMPLO %l3");
2329
2330 case GT:
2331 /* Reversed operand version of LT. */
2332 return ("// if (%1 > %2) goto %l3\n\tSUB.%# %2,%1,r15\n\tJMPLT %l3");
2333
2334 case GTU:
2335 /* Reverse an LTU. */
2336 return ("// if (%1 >{U} %2) goto %l3\n\tSUB.%# %2,%1,r15\n\tJMPLO %l3");
2337
2338 default:
2339 gcc_unreachable();
2340 }
2341 }
2342
2343 /* Output a compare-and-branch instruction (matching the cbranch
2344 pattern). This function is current unused since the cbranch
2345 split is disabled. The function is kept around so we can use
2346 it when we understand how to do cbranch split safely. */
2347 const char *
2348 picochip_output_compare (rtx operands[])
2349 {
2350 int code;
2351
2352 if (HImode != GET_MODE (operands[1]) ||
2353 (HImode != GET_MODE (operands[2]) &&
2354 GET_CODE (operands[2]) != CONST_INT))
2355 {
2356 internal_error ("%s: at least one operand can%'t be handled",
2357 __FUNCTION__);
2358 }
2359
2360 code = GET_CODE (operands[0]);
2361 /* Use the type of comparison to output the appropriate condition
2362 test. */
2363 switch (code)
2364 {
2365 case NE:
2366 return ("SUB.%# %1,%2,r15\t// CC := (%0)");
2367
2368 case EQ:
2369 return ("SUB.%# %1,%2,r15\t// CC := (%0)");
2370
2371 case LE:
2372 /* Reverse the operand order to be GE */
2373 return ("SUB.%# %2,%1,r15\t// CC := (%0)");
2374
2375 case LEU:
2376 /* Reverse operand order of GEU. */
2377 return ("SUB.%# %2,%1,r15\t// CC := (%0)");
2378
2379 case GE:
2380 return ("SUB.%# %1,%2,r15\t// CC := (%0)");
2381
2382 case GEU:
2383 return ("SUB.%# %1,%2,r15\t// CC := (%0)");
2384
2385 case LT:
2386 return ("SUB.%# %1,%2,r15\t// CC := (%0)");
2387
2388 case LTU:
2389 return ("SUB.%# %1,%2,r15\t// CC := (%0)");
2390
2391 case GT:
2392 /* Reversed operand version of LT. */
2393 return ("SUB.%# %2,%1,r15\t// CC := (%0)");
2394
2395 case GTU:
2396 /* Reverse an LTU. */
2397 return ("SUB.%# %2,%1,r15\t// CC := (%0)");
2398
2399 default:
2400 gcc_unreachable();
2401 }
2402 }
2403
2404 /* Output the branch insn part of a compare-and-branch split. */
2405 const char *
2406 picochip_output_branch (rtx operands[], rtx insn)
2407 {
2408
2409 int code = GET_CODE(operands[2]);
2410 if (picochip_is_short_branch (insn))
2411 {
2412 /* Short branches can be output directly using the
2413 appropriate instruction. */
2414 switch (code)
2415 {
2416 case NE:
2417 return ("BNE %l0 %>");
2418 case EQ:
2419 return ("BEQ %l0 %>");
2420 case LE:
2421 return ("BGE %l0 %>");
2422 case LEU:
2423 return ("BHS %l0 %>");
2424 case GE:
2425 return ("BGE %l0 %>");
2426 case GEU:
2427 return ("BHS %l0 %>");
2428 case LT:
2429 return ("BLT %l0 %>");
2430 case LTU:
2431 return ("BLO %l0 %>");
2432 case GT:
2433 return ("BLT %l0 %>");
2434 case GTU:
2435 return ("BLO %l0 %>");
2436 default:
2437 internal_error ("unknown short branch in %s (type %d)",
2438 __FUNCTION__, (int) INTVAL (operands[1]));
2439 return "UNKNOWN_BRANCH";
2440 }
2441 }
2442 else
2443 {
2444 /* Long branches result in the emission of a special
2445 instruction, which the assembler expands into a suitable long
2446 branch. */
2447
2448 /* Use the type of comparison to output the appropriate condition
2449 test. */
2450 switch (code)
2451 {
2452 case NE:
2453 return ("JMPNE %l0 %>");
2454 case EQ:
2455 return ("JMPEQ %l0 %>");
2456 case LE:
2457 return ("JMPGE %l0 %>");
2458 case LEU:
2459 return ("JMPHS %l0 %>");
2460 case GE:
2461 return ("JMPGE %l0 %>");
2462 case GEU:
2463 return ("JMPHS %l0 %>");
2464 case LT:
2465 return ("JMPLT %l0 %>");
2466 case LTU:
2467 return ("JMPLO %l0 %>");
2468 case GT:
2469 return ("JMPLT %l0 %>");
2470 case GTU:
2471 return ("JMPLO %l0 %>");
2472
2473 default:
2474 internal_error ("unknown long branch in %s (type %d)",
2475 __FUNCTION__, (int) INTVAL (operands[1]));
2476 return "UNKNOWN_BRANCH";
2477 }
2478
2479 }
2480 }
2481
2482 /* Output a jump instruction. */
2483 const char *
2484 picochip_output_jump (rtx insn)
2485 {
2486 if (picochip_is_short_branch (insn))
2487 return "BRA %l0%>";
2488 else
2489 return "JMPRA %l0%>";
2490 }
2491 \f
2492 const char *
2493 picochip_output_put_array (int alternative, rtx operands[])
2494 {
2495 /* Local output buffer. */
2496 char buf[256];
2497
2498 int portArraySize = INTVAL(operands[1]);
2499 int portBaseIndex = INTVAL(operands[2]);
2500
2501 if (alternative == 0)
2502 {
2503 sprintf (buf, "// Array put\n\tadd.0 [lsl %%0,2],&__commTable_put_%d_%d,lr\n\tjl (lr)",
2504 portArraySize, portBaseIndex);
2505 output_asm_insn (buf, operands);
2506 }
2507 else if (alternative == 1)
2508 {
2509 /* Constant port id. Emit a real instruction. */
2510 int portIndex = INTVAL(operands[0]) + portBaseIndex;
2511 if (portIndex < portBaseIndex ||
2512 portIndex >= (portBaseIndex + portArraySize))
2513 {
2514 error ("PUT uses port array index %d, which is out of range [%d..%d)",
2515 portIndex, portBaseIndex, portBaseIndex + portArraySize);
2516 }
2517 sprintf(buf, "PUT R[0:1],%d", portIndex);
2518 output_asm_insn (buf, operands);
2519 }
2520 else
2521 gcc_unreachable();
2522
2523 /* Both alternatives output the insn directly. */
2524 return "";
2525 }
2526
2527 const char *picochip_output_get_array (int alternative, rtx operands[])
2528 {
2529 /* Local output buffer. */
2530 char buf[256];
2531
2532 int portArraySize = INTVAL(operands[1]);
2533 int portBaseIndex = INTVAL(operands[2]);
2534
2535 if (alternative == 0)
2536 {
2537 sprintf (buf, "// Array get\n\tadd.0 [lsl %%0,2],&__commTable_get_%d_%d,lr\n\tjl (lr)",
2538 portArraySize, portBaseIndex);
2539 output_asm_insn (buf, operands);
2540 }
2541 else if (alternative == 1)
2542 {
2543 /* Constant port id. Emit a real instruction. */
2544 int portIndex = INTVAL(operands[0]) + portBaseIndex;
2545 if (portIndex < portBaseIndex ||
2546 portIndex >= (portBaseIndex + portArraySize))
2547 {
2548 error ("GET uses port array index %d, which is out of range [%d..%d)",
2549 portIndex, portBaseIndex, portBaseIndex + portArraySize);
2550 }
2551 sprintf(buf, "GET %d,R[0:1]", portIndex);
2552 output_asm_insn (buf, operands);
2553 }
2554 else
2555 gcc_unreachable();
2556
2557 /* Both alternatives output the insn directly. */
2558 return "";
2559 }
2560
2561 const char *picochip_output_testport_array (int alternative, rtx operands[])
2562 {
2563 /* Local output buffer. */
2564 char buf[256];
2565
2566 int portArraySize = INTVAL(operands[2]);
2567 int portBaseIndex = INTVAL(operands[3]);
2568
2569 if (alternative == 0)
2570 {
2571 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",
2572 portArraySize, portBaseIndex);
2573 output_asm_insn (buf, operands);
2574 }
2575 else if (alternative == 1)
2576 {
2577 /* Constant port id. Emit a real instruction. */
2578 int portIndex = INTVAL(operands[1]) + portBaseIndex;
2579 if (portIndex < portBaseIndex ||
2580 portIndex >= (portBaseIndex + portArraySize))
2581 {
2582 error ("PUT uses port array index %d, which is out of range [%d..%d)",
2583 portIndex, portBaseIndex, portBaseIndex + portArraySize);
2584 }
2585 sprintf(buf, "copy.1 0,%%0 %%| TSTPORT %d\n\tcopyeq 1,%%0", portIndex);
2586 output_asm_insn (buf, operands);
2587 }
2588 else
2589 gcc_unreachable();
2590
2591 /* Both alternatives output the insn directly. */
2592 return "";
2593 }
2594 \f
2595 /* Output a comparison operand as a symbol (e.g., >). */
2596 static void
2597 picochip_print_comparison (FILE * file, rtx operand, int letter)
2598 {
2599
2600 if (letter == 'i')
2601 {
2602 /* Output just the comparison symbol. */
2603 switch (GET_CODE (operand))
2604 {
2605 case NE:
2606 fprintf (file, "!=");
2607 break;
2608 case EQ:
2609 fprintf (file, "==");
2610 break;
2611 case GE:
2612 fprintf (file, ">=");
2613 break;
2614 case GEU:
2615 fprintf (file, ">={U}");
2616 break;
2617 case LT:
2618 fprintf (file, "<");
2619 break;
2620 case LTU:
2621 fprintf (file, "<{U}");
2622 break;
2623 case LE:
2624 fprintf (file, "<=");
2625 break;
2626 case LEU:
2627 fprintf (file, "<={U}");
2628 break;
2629 case GT:
2630 fprintf (file, ">");
2631 break;
2632 case GTU:
2633 fprintf (file, ">{U}");
2634 break;
2635 default:
2636 gcc_unreachable();
2637 }
2638 }
2639 else
2640 {
2641 /* Output the comparison formatted as operand,symbol,operand */
2642 rtx op0 = XEXP (operand, 0);
2643 rtx op1 = XEXP (operand, 1);
2644
2645 picochip_print_operand (file, op0, 0);
2646 picochip_print_comparison (file, operand, 'i');
2647 picochip_print_operand (file, op1, 0);
2648 }
2649 }
2650
2651 /* This function generates a memory address operand in the given
2652 mode. That is, if the address contains a constant offset, then the
2653 offset is divided by the required mode size to compute the
2654 mode specific offset. By default, picochip_print_operand_address calls
2655 this function using the natural mode of the operand, but special
2656 operand codes can be used to invoke the computation using an
2657 unnatural mode (e.g., compute the HI aligned address of an SI mode
2658 address). */
2659 static void
2660 picochip_print_memory_address (FILE * file, rtx operand,
2661 enum machine_mode mode)
2662 {
2663 rtx address = XEXP (operand, 0);
2664
2665 /* Sanity check. */
2666 if (MEM != GET_CODE (operand))
2667 fatal_insn ("picochip_print_memory_address - Operand isn't memory based",
2668 operand);
2669
2670 if (TARGET_DEBUG)
2671 {
2672 printf ("picochip_print_memory_address: ");
2673 print_rtl (stdout, operand);
2674 printf ("\n");
2675 }
2676
2677 switch (GET_CODE (address))
2678 {
2679 case PLUS:
2680 {
2681 /* Grab the address components. */
2682 rtx base = XEXP (address, 0);
2683 rtx offset = XEXP (address, 1);
2684
2685 /* Only handle reg+const addresses */
2686 if (REG == GET_CODE (base) && CONST_INT == GET_CODE (offset))
2687 {
2688 /* Sanity check. If an FP+offset address is given, ensure
2689 that the offset lies within the given frame, or a lower
2690 frame. */
2691 if (REGNO (base) == STACK_POINTER_REGNUM )
2692 gcc_assert (INTVAL (offset) <= (picochip_arg_area_byte_offset () +
2693 crtl->args.size));
2694
2695 /* Print the base register - identical for all modes. */
2696 fprintf (file, "(");
2697 picochip_print_operand (file, base, 'r');
2698 fprintf (file, ")");
2699
2700 /* Print the constant offset with compensation for the mode. */
2701 switch (mode)
2702 {
2703 case QImode:
2704 picochip_print_operand (file, offset, 'Q');
2705 break;
2706
2707 case HImode:
2708 picochip_print_operand (file, offset, 'H');
2709 break;
2710
2711 case SImode:
2712 case SFmode:
2713 picochip_print_operand (file, offset, 'S');
2714 break;
2715
2716 case DImode:
2717 picochip_print_operand (file, offset, 'D');
2718 break;
2719
2720 default:
2721 gcc_unreachable();
2722 }
2723
2724 }
2725
2726 }
2727
2728 break;
2729
2730 case SYMBOL_REF:
2731 picochip_print_operand (file, address, 's');
2732 break;
2733
2734 case CONST:
2735 {
2736 rtx inner;
2737 rtx base;
2738 rtx offset;
2739
2740 inner = XEXP (address, 0);
2741
2742 /* Sanity check - the CONST memory address must be a base+offset. */
2743 gcc_assert (PLUS == GET_CODE (inner));
2744
2745 base = XEXP (inner, 0);
2746 offset = XEXP (inner, 1);
2747
2748 fprintf (file, "&_%s%+d", XSTR (base, 0), XINT (offset, 0));
2749
2750 break;
2751 }
2752
2753 case REG:
2754 /* Register operand. Provide a zero offset. */
2755 fprintf (file, "(");
2756 picochip_print_operand (file, address, 'r');
2757 fprintf (file, ")0");
2758 break;
2759
2760 default:
2761 gcc_unreachable();
2762 }
2763
2764 }
2765
2766 /* Output an operand. Formatting letters allow particular parts of
2767 the operand to be output. */
2768 void
2769 picochip_print_operand (FILE * file, rtx operand, int letter)
2770 {
2771
2772 /* Handle special cases. */
2773 switch (letter)
2774 {
2775 /* VLIW continuation, for explicit VLIW sequences. */
2776 case '|':
2777 fprintf (file, "\\");
2778 return;
2779
2780 /* ALU selector. */
2781 case '#':
2782 fputc (picochip_get_vliw_alu_id (), file);
2783 return;
2784
2785 /* Delay slot specifier. */
2786 case '>':
2787 /* This should be handled in asm_output_opcode. */
2788 gcc_unreachable();
2789
2790 /* Instruction mnemonics (e.g., lshift becomes LSL). */
2791 case 'I':
2792 switch (GET_CODE (operand))
2793 {
2794 case AND:
2795 fprintf (file, "AND");
2796 break;
2797 case IOR:
2798 fprintf (file, "OR");
2799 break;
2800 case XOR:
2801 fprintf (file, "XOR");
2802 break;
2803 case PLUS:
2804 fprintf (file, "ADD");
2805 break;
2806 case MINUS:
2807 fprintf (file, "SUB");
2808 break;
2809 default:
2810 gcc_unreachable();
2811 }
2812 return;
2813
2814 /* Symbolic instructions (e.g., lshift becomes <<). */
2815 case 'i':
2816 switch (GET_CODE (operand))
2817 {
2818 case AND:
2819 fprintf (file, "&");
2820 break;
2821 case IOR:
2822 fprintf (file, "|");
2823 break;
2824 case XOR:
2825 fprintf (file, "^");
2826 break;
2827 case PLUS:
2828 fprintf (file, "+");
2829 break;
2830 case MINUS:
2831 fprintf (file, "-");
2832 break;
2833 default:
2834 fprintf (file, "UNKNOWN_INSN");
2835 break;
2836 }
2837 return;
2838
2839 default: /* Not a punctuation character - process as normal. */
2840 break;
2841 }
2842
2843 switch (GET_CODE (operand))
2844 {
2845 case REG:
2846 switch (letter)
2847 {
2848 case 'R':
2849 /* Write a range of registers. */
2850 fprintf (file, "R[%d:%d]", REGNO (operand) + 1, REGNO (operand));
2851 break;
2852
2853 case 'U':
2854 /* The upper register of a pair is requested. */
2855 fprintf (file, "%s", picochip_regnames[REGNO (operand) + 1]);
2856 break;
2857
2858 case 'L':
2859 /* The lower register of a pair is requested. Equivalent to the
2860 default, but included for completeness. */
2861 fprintf (file, "%s", picochip_regnames[REGNO (operand)]);
2862 break;
2863
2864 case 'X':
2865 /* The 3rd register of a DI mode register. */
2866 fprintf (file, "%s", picochip_regnames[REGNO (operand) + 2]);
2867 break;
2868
2869 case 'Y':
2870 /* The 4th register of a DI mode register. */
2871 fprintf (file, "%s", picochip_regnames[REGNO (operand) + 3]);
2872 break;
2873
2874 default:
2875 fprintf (file, "%s", picochip_regnames[REGNO (operand)]);
2876 }
2877 break;
2878
2879 case CONST_INT:
2880 /* A range of letters can be used to format integers. The
2881 letters Q/H/S are used to divide the constant by the width of
2882 QI/HI/SI mode integers in bytes. The U/L modifiers are used
2883 to obtain the upper and lower 16-bits of a 32-bit
2884 constant. Where possible, signed numbers are used, since
2885 signed representations of numbers may be more compact (e.g.,
2886 65535 can be represented as -1, which fits into a small
2887 constant, whereas 65535 requires a large constant). */
2888 switch (letter)
2889 {
2890 case 'Q':
2891 fprintf (file, "%ld", INTVAL (operand));
2892 break;
2893
2894 case 'H':
2895 fprintf (file, "%ld", INTVAL (operand) / 2);
2896 break;
2897
2898 case 'S':
2899 fprintf (file, "%ld", INTVAL (operand) / 4);
2900 break;
2901
2902 case 'P':
2903 fprintf (file, "%d", exact_log2 (INTVAL(operand)));
2904 break;
2905
2906 case 'U':
2907 fprintf (file, "%hi", (short) ((INTVAL (operand) >> 16) & 0xFFFF));
2908 break;
2909
2910 case 'L':
2911 fprintf (file, "%hi", (short) (INTVAL (operand) & 0xFFFF));
2912 break;
2913
2914 default:
2915 fprintf (file, "%ld", INTVAL (operand));
2916 break;
2917 }
2918 break;
2919
2920 case CONST_DOUBLE:
2921 {
2922 long val;
2923 REAL_VALUE_TYPE rv;
2924
2925 if (GET_MODE (operand) != SFmode)
2926 fatal_insn ("Unknown mode in print_operand (CONST_DOUBLE) :",
2927 operand);
2928 REAL_VALUE_FROM_CONST_DOUBLE (rv, operand);
2929 REAL_VALUE_TO_TARGET_SINGLE (rv, val);
2930
2931 switch (letter)
2932 {
2933 case 'U':
2934 fprintf (file, "%hi", (short) ((val >> 16) & 0xFFFF));
2935 break;
2936
2937 case 'L':
2938 fprintf (file, "%hi", (short) (val & 0xFFFF));
2939 break;
2940 }
2941
2942 break;
2943
2944 }
2945
2946 /* Output a symbol. The output format must match that of
2947 picochip_output_label. */
2948 case SYMBOL_REF:
2949 /* Ensure that the symbol is marked as referenced. Gcc can
2950 occasionally omit the function bodies when it believes them
2951 to be unreferenced. */
2952 if (SYMBOL_REF_DECL (operand))
2953 mark_decl_referenced (SYMBOL_REF_DECL (operand));
2954 fprintf (file, "&");
2955 assemble_name (file, XSTR (operand, 0));
2956 break;
2957
2958 case LABEL_REF:
2959 /* This format must match that of picochip_output_label. */
2960 fprintf (file, "&");
2961 output_asm_label (operand);
2962 break;
2963
2964 case MEM:
2965 {
2966 rtx addr = XEXP (operand, 0);
2967
2968 switch (letter)
2969 {
2970 case 'o':
2971 if (PLUS != GET_CODE (addr))
2972 fatal_insn ("Bad address, not (reg+disp):", addr);
2973 else
2974 picochip_print_operand (file, XEXP (addr, 1), 0);
2975 break;
2976
2977 case 'M':
2978 /* Output a memory address in byte mode notation (i.e., the
2979 constant address (if any) is the actual byte address. */
2980 picochip_print_memory_address (file, operand, QImode);
2981 break;
2982
2983 /* Output a constant offset of the given mode (i.e., divide
2984 the constant by the number of units in the mode to get the
2985 constant). */
2986 case 'Q':
2987 picochip_print_memory_address (file, operand, QImode);
2988 break;
2989
2990 case 'H':
2991 picochip_print_memory_address (file, operand, HImode);
2992 break;
2993
2994 case 'S':
2995 picochip_print_memory_address (file, operand, SImode);
2996 break;
2997
2998 case 'F':
2999 picochip_print_memory_address (file, operand, SFmode);
3000 break;
3001
3002 case 'b':
3003 if (PLUS != GET_CODE (addr))
3004 fatal_insn ("Bad address, not (reg+disp):", addr);
3005 else
3006 picochip_print_operand (file, XEXP (addr, 0), 0);
3007 break;
3008
3009 /* When the mem operand is (reg + big offset) which cannot
3010 be represented in an instruction as operand, the compiler
3011 automatically generates the instruction to put in (reg +
3012 big offset) into another register. In such cases, it
3013 returns '0' as the character. This needs to be handled
3014 as well. */
3015 case 0:
3016 case 'r':
3017 if (REG != GET_CODE (addr))
3018 fatal_insn ("Bad address, not register:", addr);
3019 else
3020 picochip_print_operand (file, addr, 0);
3021 break;
3022
3023 default:
3024 fprintf (file, "Unknown mem operand - letter %c ",
3025 (char) (letter));
3026 print_rtl (file, operand);
3027 }
3028
3029 break;
3030 }
3031
3032 case CONST:
3033 {
3034 rtx const_exp = XEXP (operand, 0);
3035
3036 /* Handle constant offsets to symbol references. */
3037 if (PLUS == GET_CODE (const_exp) &&
3038 SYMBOL_REF == GET_CODE (XEXP (const_exp, 0)) &&
3039 CONST_INT == GET_CODE (XEXP (const_exp, 1)))
3040 {
3041
3042 picochip_print_operand (file, XEXP (const_exp, 0), 0);
3043 if (INTVAL (XEXP (const_exp, 1)) >= 0)
3044 fprintf (file, "+");
3045 /* else use the - from the operand (i.e., AP-2)) */
3046
3047 picochip_print_operand (file, XEXP (const_exp, 1), letter);
3048
3049 }
3050 }
3051 break;
3052
3053
3054 case PLUS:
3055 {
3056 /* PLUS expressions are of the form (base + offset). Different
3057 options (analagous to those of memory PLUS expressions) are used
3058 to extract the base and offset components. */
3059
3060 switch (letter)
3061 {
3062 case 'b':
3063 picochip_print_operand (file, XEXP (operand, 0), 0);
3064 break;
3065
3066 case 'o':
3067 picochip_print_operand (file, XEXP (operand, 1), 0);
3068 break;
3069
3070 default:
3071
3072 /* If the expression is composed entirely of constants,
3073 evaluate the result. This should only occur with the
3074 picoChip specific comms instructions, which are emitted as
3075 base+offset expressions. */
3076 if (CONST_INT == GET_CODE (XEXP (operand, 0)) &&
3077 CONST_INT == GET_CODE (XEXP (operand, 1)))
3078 {
3079 HOST_WIDE_INT result = (XINT (XEXP (operand, 0), 0) +
3080 XINT (XEXP (operand, 1), 0));
3081 fprintf (file, "%ld", result);
3082 }
3083 else
3084 {
3085 fprintf (file, "(");
3086 picochip_print_operand (file, XEXP (operand, 0), 0);
3087 fprintf (file, "+");
3088 picochip_print_operand (file, XEXP (operand, 1), 0);
3089 fprintf (file, ")");
3090 }
3091 }
3092
3093 break;
3094 }
3095
3096 /* Comparison operations. */
3097 case NE:
3098 case EQ:
3099 case GE:
3100 case GEU:
3101 case LT:
3102 case LTU:
3103 case LE:
3104 case LEU:
3105 case GT:
3106 case GTU:
3107 picochip_print_comparison (file, operand, letter);
3108 return;
3109
3110 default:
3111 fprintf (stderr, "Unknown operand encountered in %s\n", __FUNCTION__);
3112 print_rtl (file, operand);
3113 break;
3114
3115 }
3116
3117 }
3118
3119 /* Output an operand address */
3120 void
3121 picochip_print_operand_address (FILE * file, rtx operand)
3122 {
3123
3124 switch (GET_CODE (operand))
3125 {
3126
3127 case SYMBOL_REF:
3128 /* This format must match that of picochip_output_label. */
3129 assemble_name (file, XSTR (operand, 0));
3130 break;
3131
3132 case CODE_LABEL:
3133 /* Note this format must match that of picochip_output_label. */
3134 fprintf (file, "_L%d", XINT (operand, 5));
3135 break;
3136
3137 case MEM:
3138 /* Pass on to a specialised memory address generator. */
3139 picochip_print_memory_address (file, operand, GET_MODE (operand));
3140 break;
3141
3142 default:
3143 gcc_unreachable();
3144
3145 }
3146
3147 }
3148 \f
3149
3150 /* Scheduling functions. */
3151
3152 /* Save some of the contents of recog_data. */
3153 static void
3154 picochip_save_recog_data (void)
3155 {
3156 picochip_saved_which_alternative = which_alternative;
3157 memcpy (&picochip_saved_recog_data, &recog_data,
3158 sizeof (struct recog_data_d));
3159 }
3160
3161 /* Restore some of the contents of global variable recog_data. */
3162 static void
3163 picochip_restore_recog_data (void)
3164 {
3165 which_alternative = picochip_saved_which_alternative;
3166 memcpy (&recog_data, &picochip_saved_recog_data,
3167 sizeof (struct recog_data_d));
3168 }
3169
3170 /* Ensure that no var tracking notes are emitted in the middle of a
3171 three-instruction bundle. */
3172 static void
3173 reorder_var_tracking_notes (void)
3174 {
3175 basic_block bb;
3176
3177 FOR_EACH_BB (bb)
3178 {
3179 rtx insn, next, last_insn = NULL_RTX;
3180 rtx queue = NULL_RTX;
3181
3182 /* Iterate through the bb and find the last non-debug insn */
3183 for (insn = BB_HEAD (bb); insn != NEXT_INSN(BB_END (bb)); insn = NEXT_INSN(insn))
3184 {
3185 if (NONDEBUG_INSN_P(insn))
3186 last_insn = insn;
3187 }
3188
3189 /* In all normal cases, queue up notes and emit them just before a TImode
3190 instruction. For the last instruction, emit the queued notes just after
3191 the last instruction. */
3192 for (insn = BB_HEAD (bb); insn != NEXT_INSN(BB_END (bb)); insn = next)
3193 {
3194 next = NEXT_INSN (insn);
3195
3196 if (insn == last_insn)
3197 {
3198 while (queue)
3199 {
3200 rtx next_queue = PREV_INSN (queue);
3201 PREV_INSN (NEXT_INSN(insn)) = queue;
3202 NEXT_INSN(queue) = NEXT_INSN(insn);
3203 PREV_INSN(queue) = insn;
3204 NEXT_INSN(insn) = queue;
3205 queue = next_queue;
3206 }
3207 /* There is no more to do for this bb. break*/
3208 break;
3209 }
3210 else if (NONDEBUG_INSN_P (insn))
3211 {
3212 /* Emit queued up notes before the first instruction of a bundle. */
3213 if (GET_MODE (insn) == TImode)
3214 {
3215 while (queue)
3216 {
3217 rtx next_queue = PREV_INSN (queue);
3218 NEXT_INSN (PREV_INSN(insn)) = queue;
3219 PREV_INSN (queue) = PREV_INSN(insn);
3220 PREV_INSN (insn) = queue;
3221 NEXT_INSN (queue) = insn;
3222 queue = next_queue;
3223 }
3224 }
3225 }
3226 else if (NOTE_P (insn))
3227 {
3228 rtx prev = PREV_INSN (insn);
3229 PREV_INSN (next) = prev;
3230 NEXT_INSN (prev) = next;
3231 /* Ignore call_arg notes. They are expected to be just after the
3232 call insn. If the call is start of a long VLIW, labels are
3233 emitted in the middle of a VLIW, which our assembler can not
3234 handle. */
3235 if (NOTE_KIND (insn) != NOTE_INSN_CALL_ARG_LOCATION)
3236 {
3237 PREV_INSN (insn) = queue;
3238 queue = insn;
3239 }
3240 }
3241 }
3242 /* Make sure we are not dropping debug instructions.*/
3243 gcc_assert (queue == NULL_RTX);
3244 }
3245 }
3246
3247 /* Perform machine dependent operations on the rtl chain INSNS. */
3248 void
3249 picochip_reorg (void)
3250 {
3251 rtx insn, insn1, vliw_start = NULL_RTX;
3252 int vliw_insn_location = 0;
3253
3254 /* We are freeing block_for_insn in the toplev to keep compatibility
3255 with old MDEP_REORGS that are not CFG based. Recompute it now. */
3256 compute_bb_for_insn ();
3257
3258 if (optimize == 0)
3259 split_all_insns ();
3260
3261 if (picochip_schedule_type != DFA_TYPE_NONE)
3262 {
3263 timevar_push (TV_SCHED2);
3264
3265 /* Process the instruction list, computing the sizes of each
3266 instruction, and consequently branch distances. This can
3267 result in some branches becoming short enough to be treated
3268 as a real branch instruction, rather than an assembly branch
3269 macro which may expand into multiple instructions. The
3270 benefit of shortening branches is that real branch
3271 instructions can be properly DFA scheduled, whereas macro
3272 branches cannot. */
3273 shorten_branches (get_insns ());
3274
3275 /* Do control and data sched analysis again,
3276 and write some more of the results to dump file. */
3277
3278 split_all_insns ();
3279
3280 schedule_ebbs ();
3281
3282 timevar_pop (TV_SCHED2);
3283
3284 ggc_collect ();
3285
3286 if (picochip_schedule_type == DFA_TYPE_SPEED)
3287 {
3288 /* Whenever a VLIW packet is generated, all instructions in
3289 that packet must appear to come from the same source
3290 location. The following code finds all the VLIW packets,
3291 and tags their instructions with the location of the first
3292 instruction from the packet. Clearly this will result in
3293 strange behaviour when debugging the code, but since
3294 debugging and optimisation are being used in conjunction,
3295 strange behaviour is certain to occur anyway. */
3296 /* Slight bit of change. If the vliw set contains a branch
3297 or call instruction, we pick its location.*/
3298 for (insn = get_insns (); insn; insn = next_real_insn (insn))
3299 {
3300
3301 /* If this is the first instruction in the VLIW packet,
3302 extract its location. */
3303 if (GET_MODE (insn) == TImode)
3304 {
3305 vliw_start = insn;
3306 vliw_insn_location = INSN_LOCATION (insn);
3307 }
3308 if (JUMP_P (insn) || CALL_P(insn))
3309 {
3310 vliw_insn_location = INSN_LOCATION (insn);
3311 for (insn1 = vliw_start; insn1 != insn ; insn1 = next_real_insn (insn1))
3312 INSN_LOCATION (insn1) = vliw_insn_location;
3313 }
3314 /* Tag subsequent instructions with the same location. */
3315 INSN_LOCATION (insn) = vliw_insn_location;
3316 }
3317 }
3318
3319 }
3320
3321 /* Locate the note marking the end of the function's prologue. If
3322 the note appears in the middle of a VLIW packet, move the note to
3323 the end. This avoids unpleasant consequences such as trying to
3324 emit prologue markers (e.g., .loc/.file directives) in the middle
3325 of VLIW packets. */
3326 if (picochip_schedule_type == DFA_TYPE_SPEED)
3327 {
3328 rtx prologue_end_note = NULL;
3329 rtx last_insn_in_packet = NULL;
3330
3331 for (insn = get_insns (); insn; insn = next_insn (insn))
3332 {
3333 /* The prologue end must be moved to the end of the VLIW packet. */
3334 if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_PROLOGUE_END)
3335 {
3336 prologue_end_note = insn;
3337 break;
3338 }
3339 }
3340
3341 /* Find the last instruction in this packet. */
3342 for (insn = prologue_end_note; insn; insn = next_real_insn (insn))
3343 {
3344 if (GET_MODE (insn) == TImode)
3345 break;
3346 else
3347 last_insn_in_packet = insn;
3348 }
3349
3350 if (last_insn_in_packet != NULL)
3351 {
3352 rtx tmp_note
3353 = emit_note_after ((enum insn_note) NOTE_KIND (prologue_end_note),
3354 last_insn_in_packet);
3355 memcpy(&NOTE_DATA (tmp_note), &NOTE_DATA(prologue_end_note), sizeof(NOTE_DATA(prologue_end_note)));
3356 delete_insn (prologue_end_note);
3357 }
3358 }
3359
3360 if (flag_var_tracking)
3361 {
3362 timevar_push (TV_VAR_TRACKING);
3363 variable_tracking_main ();
3364 /* We also have to deal with variable tracking notes in the
3365 middle of VLIW packets. */
3366 reorder_var_tracking_notes();
3367 timevar_pop (TV_VAR_TRACKING);
3368 }
3369 }
3370
3371 /* Return the ALU character identifier for the current
3372 instruction. This will be 0 or 1. */
3373 static char
3374 picochip_get_vliw_alu_id (void)
3375 {
3376 int attr_type = 0;
3377
3378 /* Always use ALU 0 if VLIW scheduling is disabled. */
3379 if (picochip_schedule_type != DFA_TYPE_SPEED)
3380 return '0';
3381
3382 /* Get the attribute type of the instruction. Note that this can
3383 ruin the contents of recog_data, so save/restore around the
3384 call. */
3385 picochip_save_recog_data ();
3386 attr_type = get_attr_type (picochip_current_prescan_insn);
3387 picochip_restore_recog_data ();
3388
3389 if (picochip_current_vliw_state.contains_pico_alu_insn)
3390 {
3391
3392 /* If this a picoAlu insn? If it is, then stuff it into ALU 0,
3393 else it must be the other ALU (either basic or nonCc)
3394 instruction which goes into 1. */
3395 if (attr_type == TYPE_PICOALU)
3396 return '0';
3397 else
3398 return '1';
3399
3400 }
3401 else if (picochip_current_vliw_state.contains_non_cc_alu_insn)
3402 {
3403 /* Is this the non CC instruction? If it is, then stuff it into
3404 ALU 1, else it must be a picoAlu or basicAlu, in which case
3405 it goes into ALU 0. */
3406 if (attr_type == TYPE_NONCCALU)
3407 return '1';
3408 else
3409 return '0';
3410 }
3411 else
3412 {
3413 /* No picoAlu/nonCc instructions in use, so purely dependent upon
3414 whether an ALU instruction has already been scheduled in this
3415 cycle. */
3416 switch (picochip_current_vliw_state.num_alu_insns_so_far)
3417 {
3418 case 0:
3419 picochip_current_vliw_state.num_alu_insns_so_far++;
3420 return '0';
3421
3422 case 1:
3423 picochip_current_vliw_state.num_alu_insns_so_far++;
3424 return '1';
3425
3426 default:
3427 internal_error ("too many ALU instructions emitted (%d)",
3428 picochip_current_vliw_state.num_alu_insns_so_far);
3429 return 'X';
3430 }
3431 }
3432
3433 }
3434
3435 /* Reset any information about the current VLIW packing status. */
3436 static void
3437 picochip_reset_vliw (rtx insn)
3438 {
3439 rtx local_insn = insn;
3440
3441 /* Nothing to do if VLIW scheduling isn't being used. */
3442 if (picochip_schedule_type != DFA_TYPE_SPEED)
3443 return;
3444
3445 if (TARGET_DEBUG)
3446 printf ("%s on insn %d\n", __FUNCTION__, INSN_UID (insn));
3447
3448 /* Reset. */
3449 picochip_current_vliw_state.contains_pico_alu_insn = 0;
3450 picochip_current_vliw_state.contains_non_cc_alu_insn = 0;
3451 picochip_current_vliw_state.num_alu_insns_so_far = 0;
3452 picochip_current_vliw_state.num_cfi_labels_deferred = 0;
3453 picochip_current_vliw_state.lm_label_name[0] = 0;
3454 picochip_current_vliw_state.num_insns_in_packet = 0;
3455
3456 /* Read through the VLIW packet, classifying the instructions where
3457 appropriate. */
3458 local_insn = insn;
3459 do
3460 {
3461 if (NOTE_P (local_insn) || DEBUG_INSN_P(local_insn))
3462 {
3463 local_insn = NEXT_INSN (local_insn);
3464 continue;
3465 }
3466 else if (!INSN_P (local_insn))
3467 break;
3468 else
3469 {
3470 /* It is an instruction, but is it ours? */
3471 if (INSN_CODE (local_insn) != -1)
3472 {
3473 int attr_type = 0;
3474
3475 picochip_current_vliw_state.num_insns_in_packet += 1;
3476
3477 /* Is it a picoAlu or nonCcAlu instruction? Note that the
3478 get_attr_type function can overwrite the values in
3479 the recog_data global, hence this is saved and
3480 restored around the call. Not doing so results in
3481 asm_output_opcode being called with a different
3482 instruction to final_prescan_insn, which is fatal. */
3483 picochip_save_recog_data ();
3484 attr_type = get_attr_type (local_insn);
3485 picochip_restore_recog_data ();
3486
3487 if (attr_type == TYPE_PICOALU)
3488 picochip_current_vliw_state.contains_pico_alu_insn = 1;
3489 if (attr_type == TYPE_NONCCALU)
3490 picochip_current_vliw_state.contains_non_cc_alu_insn = 1;
3491
3492 }
3493 }
3494
3495 /* Get the next instruction. */
3496 local_insn = NEXT_INSN (local_insn);
3497
3498 /* Keep going while the next instruction is part of the same
3499 VLIW packet (i.e., its a valid instruction and doesn't mark
3500 the start of a new VLIW packet. */
3501 }
3502 while (local_insn &&
3503 (GET_MODE (local_insn) != TImode) && (INSN_CODE (local_insn) != -1));
3504
3505 }
3506
3507 int
3508 picochip_sched_reorder (FILE * file, int verbose,
3509 rtx * ready ATTRIBUTE_UNUSED,
3510 int *n_readyp ATTRIBUTE_UNUSED, int clock)
3511 {
3512
3513 if (verbose > 0)
3514 fprintf (file, ";;\tClock %d\n", clock);
3515
3516 return picochip_sched_issue_rate ();
3517
3518 }
3519
3520 int
3521 picochip_sched_lookahead (void)
3522 {
3523 /* It should always be enough to lookahead by 2 insns. Only slot0/1 could
3524 have a conflict. */
3525 return 2;
3526 }
3527
3528 int
3529 picochip_sched_issue_rate (void)
3530 {
3531 return 3;
3532 }
3533
3534 /* Adjust the scheduling cost between the two given instructions,
3535 which have the given dependency. */
3536 int
3537 picochip_sched_adjust_cost (rtx insn, rtx link, rtx dep_insn, int cost)
3538 {
3539
3540 if (TARGET_DEBUG)
3541 {
3542 printf ("Sched Adjust Cost: %d->%d is %d\n",
3543 INSN_UID (insn), INSN_UID (dep_insn), cost);
3544
3545 printf (" Dependency type:");
3546 switch (REG_NOTE_KIND (link))
3547 {
3548 case 0:
3549 printf ("Data\n");
3550 break;
3551 case REG_DEP_ANTI:
3552 printf ("ANTI\n");
3553 break;
3554 case REG_DEP_OUTPUT:
3555 printf ("OUTPUT\n");
3556 break;
3557 default:
3558 printf ("Unknown (%d)\n", REG_NOTE_KIND (link));
3559 }
3560 }
3561
3562 /* Anti-dependencies are used to enforce the ordering between a
3563 * branch, and any subsequent instructions. For example:
3564 *
3565 * BNE someLabel
3566 * ADD.0 r0,r1,r2
3567 *
3568 * The ADD instruction must execute after the branch, and this is
3569 * enforced using an anti-dependency. Unfortunately, VLIW machines
3570 * are happy to execute anti-dependent instructions in the same
3571 * cycle, which then results in a schedule like the following being
3572 * created:
3573 *
3574 * BNE someLabel \ ADD.0 r0,r1,r2
3575 *
3576 * The instruction which would normally be conditionally executed
3577 * depending upon the outcome of the branch, is now unconditionally
3578 * executed every time. To prevent this happening, any
3579 * anti-dependencies between a branch and another instruction are
3580 * promoted to become real dependencies.
3581 */
3582 if ((JUMP_P (dep_insn) || CALL_P(dep_insn)) && REG_NOTE_KIND (link) == REG_DEP_ANTI)
3583 {
3584
3585 if (TARGET_DEBUG)
3586 printf ("Promoting anti-dependency %d->%d to a true-dependency\n",
3587 INSN_UID (insn), INSN_UID (dep_insn));
3588
3589 return 1;
3590 }
3591
3592 return cost;
3593
3594 }
3595
3596 /* Return the minimum of the two values */
3597 static int
3598 minimum (int a, int b)
3599 {
3600 if (a < b)
3601 return a;
3602 if (b < a)
3603 return b;
3604 /* I dont expect to get to this function with a==b.*/
3605 gcc_unreachable();
3606 }
3607
3608
3609 /* This function checks if the memory of the two stores are just off by 2 bytes.
3610 It returns the lower memory operand's index.*/
3611
3612 static int
3613 memory_just_off (rtx opnd1, rtx opnd2)
3614 {
3615 int offset1 = 0, offset2 = 0;
3616 int reg1, reg2;
3617
3618 if (GET_CODE(XEXP(opnd1, 0)) == PLUS && GET_CODE(XEXP(XEXP(opnd1, 0),1)) == CONST_INT)
3619 {
3620 offset1 = INTVAL(XEXP(XEXP(opnd1, 0), 1));
3621 reg1 = REGNO(XEXP(XEXP(opnd1, 0), 0));
3622 }
3623 else
3624 {
3625 reg1 = REGNO(XEXP(opnd1, 0));
3626 }
3627 if (GET_CODE(XEXP(opnd2, 0)) == PLUS && GET_CODE(XEXP(XEXP(opnd2, 0), 1)) == CONST_INT)
3628 {
3629 offset2 = INTVAL(XEXP(XEXP(opnd2, 0), 1));
3630 reg2 = REGNO(XEXP(XEXP(opnd2, 0), 0));
3631 }
3632 else
3633 {
3634 reg2 = REGNO(XEXP(opnd2, 0));
3635 }
3636
3637 /* Peepholing 2 STW/LDWs has the restriction that the resulting STL/LDL's address
3638 should be 4 byte aligned. We can currently guarantee that only if the base
3639 address is FP(R13) and the offset is aligned. */
3640
3641 if (reg1 == reg2 && reg1 == 13 && abs(offset1-offset2) == 2 && minimum(offset1, offset2) % 4 == 0)
3642 return (minimum(offset1, offset2) == offset1) ? 1:2;
3643
3644 return 0;
3645 }
3646
3647 static int
3648 registers_just_off (rtx opnd1, rtx opnd2)
3649 {
3650 int reg1, reg2;
3651 reg1 = REGNO(opnd1);
3652 reg2 = REGNO(opnd2);
3653 if (abs(reg1-reg2) == 1 && minimum(reg1, reg2) % 2 == 0)
3654 return (minimum(reg1, reg2) == reg1)?1:2;
3655 return 0;
3656 }
3657
3658 /* Check to see if the two LDWs can be peepholed together into a LDL
3659 They can be if the registers getting loaded into are contiguous
3660 and the memory addresses are contiguous as well.
3661 for eg.
3662 LDW r2,[r11]x
3663 LDW r3,[r11]x+1
3664 can be merged together into
3665 LDL r[3:2],[r11]
3666
3667 NOTE:
3668 1. The LDWs themselves only guarantee that r11 will be a 2-byte
3669 aligned address. Only FP can be assumed to be 4 byte aligned.
3670 2. The progression of addresses and the register numbers should
3671 be similar. For eg., if you swap r2 and r3 in the above instructions,
3672 the resultant pair cannot be merged.
3673
3674 */
3675 bool
3676 ok_to_peephole_ldw(rtx opnd0, rtx opnd1, rtx opnd2, rtx opnd3)
3677 {
3678 int memtest=0,regtest=0;
3679 regtest = registers_just_off(opnd1,opnd3);
3680 if (regtest == 0)
3681 return false;
3682
3683 memtest = memory_just_off(opnd0,opnd2);
3684 if (memtest == 0)
3685 return false;
3686
3687 if (regtest == memtest)
3688 {
3689 return true;
3690 }
3691 return false;
3692 }
3693
3694 /* Similar to LDW peephole */
3695 bool
3696 ok_to_peephole_stw(rtx opnd0, rtx opnd1, rtx opnd2, rtx opnd3)
3697 {
3698 int memtest=0,regtest=0;
3699 regtest = registers_just_off(opnd1,opnd3);
3700 if (regtest == 0)
3701 return false;
3702
3703 memtest = memory_just_off(opnd0,opnd2);
3704 if (memtest == 0)
3705 return false;
3706
3707 if (regtest == memtest)
3708 {
3709 return true;
3710 }
3711 return false;
3712 }
3713
3714
3715 /* Generate a SImode register with the register number that is the smaller of the two */
3716 rtx
3717 gen_min_reg(rtx opnd1,rtx opnd2)
3718 {
3719 return gen_rtx_REG (SImode, minimum(REGNO(opnd1),REGNO(opnd2)));
3720 }
3721
3722 /* Generate a SImode memory with the address that is the smaller of the two */
3723 rtx
3724 gen_SImode_mem(rtx opnd1,rtx opnd2)
3725 {
3726 int offset1=0,offset2=0;
3727 rtx reg;
3728 rtx address;
3729 if (GET_CODE(XEXP(opnd1,0)) == PLUS && GET_CODE(XEXP(XEXP(opnd1,0),1)) == CONST_INT)
3730 {
3731 offset1 = INTVAL(XEXP(XEXP(opnd1,0),1));
3732 reg = XEXP(XEXP(opnd1,0),0);
3733 }
3734 else
3735 {
3736 reg = XEXP(opnd1,0);
3737 }
3738 if (GET_CODE(XEXP(opnd2,0)) == PLUS && GET_CODE(XEXP(XEXP(opnd2,0),1)) == CONST_INT)
3739 {
3740 offset2 = INTVAL(XEXP(XEXP(opnd2,0),1));
3741 }
3742 address = gen_rtx_PLUS (HImode, reg, GEN_INT(minimum(offset1,offset2)));
3743 return gen_rtx_MEM(SImode,address);
3744 }
3745
3746 bool
3747 picochip_rtx_costs (rtx x, int code, int outer_code ATTRIBUTE_UNUSED,
3748 int opno ATTRIBUTE_UNUSED, int* total, bool speed)
3749 {
3750
3751 int localTotal = 0;
3752
3753 if (!speed)
3754 {
3755 /* Need to penalize immediates that need to be encoded as long constants.*/
3756 if (code == CONST_INT && !(INTVAL (x) >= 0 && INTVAL (x) < 16))
3757 {
3758 *total = COSTS_N_INSNS(1);
3759 return true;
3760 }
3761 }
3762 switch (code)
3763 {
3764 case SYMBOL_REF:
3765 case LABEL_REF:
3766 *total = COSTS_N_INSNS (outer_code != MEM);
3767 return true;
3768 break;
3769
3770 case IF_THEN_ELSE:
3771 /* if_then_else come out of cbranch instructions. It will get split into
3772 a condition code generating subtraction and a branch */
3773 *total = COSTS_N_INSNS (2);
3774 return true;
3775 break;
3776
3777 case AND:
3778 case IOR:
3779 case XOR:
3780 if (GET_MODE(x) == SImode)
3781 *total = COSTS_N_INSNS (2);
3782 if (GET_MODE(x) == DImode)
3783 *total = COSTS_N_INSNS (4);
3784 return false;
3785
3786 case MEM:
3787 /* Byte Memory access on a NO_BYTE_ACCESS machine would be expensive */
3788 if (GET_MODE(x) == QImode && !TARGET_HAS_BYTE_ACCESS)
3789 *total = COSTS_N_INSNS (10);
3790
3791 /* 64-bit accesses have to be done through 2 32-bit access */
3792 if (GET_MODE(x) == DImode)
3793 *total = COSTS_N_INSNS (2);
3794 return false;
3795 break;
3796
3797 case ASHIFTRT:
3798
3799 /* SImode shifts are expensive */
3800 if (GET_MODE(x) == SImode)
3801 *total = COSTS_N_INSNS (10);
3802
3803 /* Register shift by constant is cheap. */
3804 if ((GET_MODE(x) == QImode || GET_MODE(x) == HImode)
3805 && GET_CODE(XEXP(x, 0)) == REG
3806 && GET_CODE(XEXP(x, 1)) == CONST_INT)
3807 *total = COSTS_N_INSNS (1);
3808 else
3809 *total = COSTS_N_INSNS (4);
3810 return false;
3811 break;
3812
3813 case DIV:
3814 case MOD:
3815
3816 /* Divisions are more expensive than the default 7*/
3817 if (GET_MODE(x) == SImode)
3818 *total = COSTS_N_INSNS (20);
3819 else
3820 *total = COSTS_N_INSNS (12);
3821 return false;
3822 break;
3823
3824 case MULT:
3825 /* Look for the simple cases of multiplying register*register or
3826 register*constant. */
3827 if ((GET_MODE(x) == QImode || GET_MODE(x) == HImode)
3828 && ((GET_CODE(XEXP(x, 0)) == REG
3829 && (GET_CODE(XEXP(x, 1)) == REG || GET_CODE(XEXP(x,1)) == CONST_INT))
3830 || (GET_CODE(XEXP(x, 0)) == ZERO_EXTEND
3831 && GET_CODE(XEXP(XEXP(x, 0),0)) == REG
3832 && GET_CODE(XEXP(x, 1)) == ZERO_EXTEND
3833 && GET_CODE(XEXP(XEXP(x, 1),0)) == REG)))
3834 {
3835
3836 /* When optimising for size, multiplication by constant
3837 should be discouraged slightly over multiplication by a
3838 register. */
3839 if (picochip_has_mac_unit)
3840 {
3841 /* Single cycle multiplication, but the result must be
3842 loaded back into a general register afterwards. */
3843 *total = COSTS_N_INSNS(2);
3844 return true;
3845 }
3846 else if (picochip_has_mul_unit)
3847 {
3848 /* Single cycle multiplication. */
3849 *total = COSTS_N_INSNS(1);
3850 return true;
3851 }
3852 /* Else no multiply available. Use default cost. */
3853
3854 }
3855 break;
3856
3857 default:
3858 /* Do nothing. */
3859 break;
3860 }
3861
3862 if (localTotal != 0)
3863 {
3864 *total = localTotal;
3865 return true;
3866 }
3867 else
3868 {
3869 return false;
3870 }
3871
3872 }
3873
3874 void
3875 picochip_final_prescan_insn (rtx insn, rtx * opvec ATTRIBUTE_UNUSED,
3876 int num_operands ATTRIBUTE_UNUSED)
3877 {
3878 rtx local_insn;
3879
3880 picochip_current_prescan_insn = insn;
3881
3882 if (TARGET_DEBUG)
3883 printf ("Final prescan on INSN %d with mode %s\n",
3884 INSN_UID (insn), GET_MODE_NAME (GET_MODE (insn)));
3885
3886 /* If this is the start of a new instruction cycle, or no scheduling
3887 is used, then reset the VLIW status. */
3888 if (GET_MODE (insn) == TImode || !picochip_schedule_type == DFA_TYPE_SPEED)
3889 picochip_reset_vliw (insn);
3890
3891 /* No VLIW scheduling occurred, so don't go any further. */
3892 if (picochip_schedule_type != DFA_TYPE_SPEED)
3893 return;
3894
3895 /* Look for the next printable instruction. This loop terminates on
3896 any recognisable instruction, and on any unrecognisable
3897 instruction with TImode. */
3898 local_insn = insn;
3899 for (local_insn = NEXT_INSN (local_insn); local_insn;
3900 local_insn = NEXT_INSN (local_insn))
3901 {
3902 if (NOTE_P (local_insn) || DEBUG_INSN_P(local_insn))
3903 continue;
3904 else if (!INSN_P (local_insn))
3905 break;
3906 else if (GET_MODE (local_insn) == TImode
3907 || INSN_CODE (local_insn) != -1)
3908 break;
3909 }
3910
3911 /* Set the continuation flag if the next instruction can be packed
3912 with the current instruction (i.e., the next instruction is
3913 valid, and isn't the start of a new cycle). */
3914 picochip_vliw_continuation = (local_insn && NONDEBUG_INSN_P (local_insn) &&
3915 (GET_MODE (local_insn) != TImode));
3916
3917 }
3918 \f
3919 /* Builtin functions. */
3920 /* Given a builtin function taking 2 operands (i.e., target + source),
3921 emit the RTL for the underlying instruction. */
3922 static rtx
3923 picochip_expand_builtin_2op (enum insn_code icode, tree call, rtx target)
3924 {
3925 tree arg0;
3926 rtx op0, pat;
3927 enum machine_mode tmode, mode0;
3928
3929 /* Grab the incoming argument and emit its RTL. */
3930 arg0 = CALL_EXPR_ARG (call, 0);
3931 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, EXPAND_NORMAL);
3932
3933 /* Determine the modes of the instruction operands. */
3934 tmode = insn_data[icode].operand[0].mode;
3935 mode0 = insn_data[icode].operand[1].mode;
3936
3937 /* Ensure that the incoming argument RTL is in a register of the
3938 correct mode. */
3939 if (!(*insn_data[icode].operand[1].predicate) (op0, mode0))
3940 op0 = copy_to_mode_reg (mode0, op0);
3941
3942 /* If there isn't a suitable target, emit a target register. */
3943 if (target == 0
3944 || GET_MODE (target) != tmode
3945 || !(*insn_data[icode].operand[0].predicate) (target, tmode))
3946 target = gen_reg_rtx (tmode);
3947
3948 /* Emit and return the new instruction. */
3949 pat = GEN_FCN (icode) (target, op0);
3950 if (!pat)
3951 return 0;
3952 emit_insn (pat);
3953
3954 return target;
3955
3956 }
3957
3958 /* Given a builtin function taking 3 operands (i.e., target + two
3959 source), emit the RTL for the underlying instruction. */
3960 static rtx
3961 picochip_expand_builtin_3op (enum insn_code icode, tree call, rtx target)
3962 {
3963 tree arg0, arg1;
3964 rtx op0, op1, pat;
3965 enum machine_mode tmode, mode0, mode1;
3966
3967 /* Grab the function's arguments. */
3968 arg0 = CALL_EXPR_ARG (call, 0);
3969 arg1 = CALL_EXPR_ARG (call, 1);
3970
3971 /* Emit rtl sequences for the function arguments. */
3972 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, EXPAND_NORMAL);
3973 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, EXPAND_NORMAL);
3974
3975 /* Get the mode's of each of the instruction operands. */
3976 tmode = insn_data[icode].operand[0].mode;
3977 mode0 = insn_data[icode].operand[1].mode;
3978 mode1 = insn_data[icode].operand[2].mode;
3979
3980 /* Ensure that each of the function argument rtl sequences are in a
3981 register of the correct mode. */
3982 if (!(*insn_data[icode].operand[1].predicate) (op0, mode0))
3983 op0 = copy_to_mode_reg (mode0, op0);
3984 if (!(*insn_data[icode].operand[2].predicate) (op1, mode1))
3985 op1 = copy_to_mode_reg (mode1, op1);
3986
3987 /* If no target has been given, create a register to use as the target. */
3988 if (target == 0
3989 || GET_MODE (target) != tmode
3990 || !(*insn_data[icode].operand[0].predicate) (target, tmode))
3991 target = gen_reg_rtx (tmode);
3992
3993 /* Emit and return the new instruction. */
3994 pat = GEN_FCN (icode) (target, op0, op1);
3995 if (!pat)
3996 return 0;
3997 emit_insn (pat);
3998
3999 return target;
4000
4001 }
4002
4003 /* Expand a builtin function which takes two arguments, and returns a void. */
4004 static rtx
4005 picochip_expand_builtin_2opvoid (enum insn_code icode, tree call)
4006 {
4007 tree arg0, arg1;
4008 rtx op0, op1, pat;
4009 enum machine_mode mode0, mode1;
4010
4011 /* Grab the function's arguments. */
4012 arg0 = CALL_EXPR_ARG (call, 0);
4013 arg1 = CALL_EXPR_ARG (call, 1);
4014
4015 /* Emit rtl sequences for the function arguments. */
4016 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4017 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4018
4019 /* Get the mode's of each of the instruction operands. */
4020 mode0 = insn_data[icode].operand[0].mode;
4021 mode1 = insn_data[icode].operand[1].mode;
4022
4023 /* Ensure that each of the function argument rtl sequences are in a
4024 register of the correct mode. */
4025 if (!(*insn_data[icode].operand[0].predicate) (op0, mode0))
4026 op0 = copy_to_mode_reg (mode0, op0);
4027 if (!(*insn_data[icode].operand[1].predicate) (op1, mode1))
4028 op1 = copy_to_mode_reg (mode1, op1);
4029
4030 /* Emit and return the new instruction. */
4031 pat = GEN_FCN (icode) (op0, op1);
4032 if (!pat)
4033 return 0;
4034 emit_insn (pat);
4035
4036 return NULL_RTX;
4037
4038 }
4039
4040 /* Expand an array get into the corresponding RTL. */
4041 static rtx
4042 picochip_expand_array_get (tree call, rtx target)
4043 {
4044 tree arg0, arg1, arg2;
4045 rtx op0, op1, op2, pat;
4046
4047 /* Grab the function's arguments. */
4048 arg0 = CALL_EXPR_ARG (call, 0);
4049 arg1 = CALL_EXPR_ARG (call, 1);
4050 arg2 = CALL_EXPR_ARG (call, 2) ;
4051
4052 /* Emit rtl sequences for the function arguments. */
4053 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4054 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4055 op2 = expand_expr (arg2, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4056
4057 /* The second and third operands must be constant. Nothing else will
4058 do. */
4059 if (CONST_INT != GET_CODE (op1))
4060 internal_error ("%s: Second source operand is not a constant",
4061 __FUNCTION__);
4062 if (CONST_INT != GET_CODE (op2))
4063 internal_error ("%s: Third source operand is not a constant",
4064 __FUNCTION__);
4065
4066 /* If no target has been given, create a register to use as the target. */
4067 if (target == 0 || GET_MODE (target) != SImode)
4068 target = gen_reg_rtx (SImode);
4069
4070 /* The first operand must be a HImode register or a constant. If it
4071 isn't, force it into a HImode register. */
4072 if (GET_MODE (op0) != HImode || REG != GET_CODE (op0))
4073 op0 = copy_to_mode_reg (HImode, op0);
4074
4075
4076 /* Emit and return the new instruction. */
4077 pat = gen_commsArrayGet (target, op0, op1, op2);
4078 emit_insn (pat);
4079
4080 return target;
4081
4082 }
4083
4084 /* Expand an array put into the corresponding RTL. */
4085 static rtx
4086 picochip_expand_array_put (tree call, rtx target)
4087 {
4088 tree arg0, arg1, arg2, arg3;
4089 rtx op0, op1, op2, op3, pat;
4090
4091 /* Grab the function's arguments. */
4092 arg0 = CALL_EXPR_ARG (call, 0);
4093 arg1 = CALL_EXPR_ARG (call, 1);
4094 arg2 = CALL_EXPR_ARG (call, 2);
4095 arg3 = CALL_EXPR_ARG (call, 3);
4096
4097 /* Emit rtl sequences for the function arguments. */
4098 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4099 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4100 op2 = expand_expr (arg2, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4101 op3 = expand_expr (arg3, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4102
4103 /* The first operand must be an SImode register. */
4104 if (GET_MODE (op0) != SImode || REG != GET_CODE (op0))
4105 op0 = copy_to_mode_reg (SImode, op0);
4106
4107 /* The second (index) operand must be a HImode register, or a
4108 constant. If it isn't, force it into a HImode register. */
4109 if (GET_MODE (op1) != HImode || REG != GET_CODE (op1))
4110 op1 = copy_to_mode_reg (HImode, op1);
4111
4112 /* The remaining operands must be constant. Nothing else will do. */
4113 if (CONST_INT != GET_CODE (op2))
4114 internal_error ("%s: Third source operand is not a constant",
4115 __FUNCTION__);
4116 if (CONST_INT != GET_CODE (op3))
4117 internal_error ("%s: Fourth source operand is not a constant",
4118 __FUNCTION__);
4119
4120 /* Emit and return the new instruction. */
4121 pat = gen_commsArrayPut (op0, op1, op2, op3);
4122 emit_insn (pat);
4123
4124 return target;
4125
4126 }
4127
4128 /* Expand an array testport into the corresponding RTL. */
4129 static rtx
4130 picochip_expand_array_testport (tree call, rtx target)
4131 {
4132 tree arg0, arg1, arg2;
4133 rtx op0, op1, op2, pat;
4134
4135 /* Grab the function's arguments. */
4136 arg0 = CALL_EXPR_ARG (call, 0);
4137 arg1 = CALL_EXPR_ARG (call, 1);
4138 arg2 = CALL_EXPR_ARG (call, 2);
4139
4140 /* Emit rtl sequences for the function arguments. */
4141 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4142 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4143 op2 = expand_expr (arg2, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4144
4145 /* The first operand must be a HImode register, or a constant. If it
4146 isn't, force it into a HImode register. */
4147 if (GET_MODE (op0) != HImode || REG != GET_CODE (op0))
4148 op0 = copy_to_mode_reg (HImode, op0);
4149
4150 /* The second and third operands must be constant. Nothing else will
4151 do. */
4152 if (CONST_INT != GET_CODE (op1))
4153 internal_error ("%s: Second source operand is not a constant",
4154 __FUNCTION__);
4155 if (CONST_INT != GET_CODE (op2))
4156 internal_error ("%s: Third source operand is not a constant",
4157 __FUNCTION__);
4158
4159 /* If no target has been given, create a HImode register to use as
4160 the target. */
4161 if (target == 0 || GET_MODE (target) != HImode)
4162 target = gen_reg_rtx (HImode);
4163
4164 /* Emit and return the new instruction. */
4165 pat = gen_commsArrayTestPort (target, op0, op1, op2);
4166 emit_insn (pat);
4167
4168 return target;
4169
4170 }
4171
4172 /* Generate a unique HALT instruction by giving the instruction a
4173 unique integer. This integer makes no difference to the assembly
4174 output (other than a comment indicating the supplied id), but the
4175 presence of the unique integer prevents the compiler from combining
4176 several different halt instructions into one instruction. This
4177 means that each use of the halt instruction is unique, which in
4178 turn means that assertions work as expected. */
4179 static rtx
4180 picochip_generate_halt (void)
4181 {
4182 static int currentId = 0;
4183 rtx insns;
4184 rtx id = GEN_INT (currentId);
4185 currentId += 1;
4186
4187 start_sequence();
4188 emit_insn (gen_halt (id));
4189
4190 /* A barrier is inserted to prevent the compiler from thinking that
4191 it has to continue execution after the HALT.*/
4192 emit_barrier ();
4193
4194 insns = get_insns();
4195 end_sequence();
4196 emit_insn (insns);
4197
4198 return const0_rtx;
4199 }
4200
4201 /* Initialise the builtin functions. Start by initialising
4202 descriptions of different types of functions (e.g., void fn(int),
4203 int fn(void)), and then use these to define the builtins. */
4204 void
4205 picochip_init_builtins (void)
4206 {
4207 tree noreturn;
4208
4209 tree int_ftype_int, int_ftype_int_int;
4210 tree long_ftype_int, long_ftype_int_int_int;
4211 tree void_ftype_int_long, int_ftype_int_int_int,
4212 void_ftype_long_int_int_int;
4213 tree void_ftype_void, unsigned_ftype_unsigned;
4214
4215 /* void func (void) */
4216 void_ftype_void = build_function_type_list (void_type_node, NULL_TREE);
4217
4218 /* int func (int) */
4219 int_ftype_int = build_function_type_list (integer_type_node,
4220 integer_type_node, NULL_TREE);
4221
4222 /* unsigned int func (unsigned int) */
4223 unsigned_ftype_unsigned
4224 = build_function_type_list (unsigned_type_node,
4225 unsigned_type_node, NULL_TREE);
4226
4227 /* int func(int, int) */
4228 int_ftype_int_int
4229 = build_function_type_list (integer_type_node,
4230 integer_type_node, integer_type_node,
4231 NULL_TREE);
4232
4233 /* long func(int) */
4234 long_ftype_int = build_function_type_list (long_integer_type_node,
4235 integer_type_node, NULL_TREE);
4236
4237 /* long func(int, int, int) */
4238 long_ftype_int_int_int
4239 = build_function_type_list (long_integer_type_node,
4240 integer_type_node, integer_type_node,
4241 integer_type_node, NULL_TREE);
4242
4243 /* int func(int, int, int) */
4244 int_ftype_int_int_int
4245 = build_function_type_list (integer_type_node,
4246 integer_type_node, integer_type_node,
4247 integer_type_node, NULL_TREE);
4248
4249 /* void func(int, long) */
4250 void_ftype_int_long
4251 = build_function_type_list (void_type_node,
4252 integer_type_node, long_integer_type_node,
4253 NULL_TREE);
4254
4255 /* void func(long, int, int, int) */
4256 void_ftype_long_int_int_int
4257 = build_function_type_list (void_type_node,
4258 long_integer_type_node, integer_type_node,
4259 integer_type_node, integer_type_node,
4260 NULL_TREE);
4261
4262 /* Initialise the sign-bit-count function. */
4263 add_builtin_function ("__builtin_sbc", int_ftype_int,
4264 PICOCHIP_BUILTIN_SBC, BUILT_IN_MD, NULL,
4265 NULL_TREE);
4266 add_builtin_function ("picoSbc", int_ftype_int, PICOCHIP_BUILTIN_SBC,
4267 BUILT_IN_MD, NULL, NULL_TREE);
4268
4269 /* Initialise the bit reverse function. */
4270 add_builtin_function ("__builtin_brev", unsigned_ftype_unsigned,
4271 PICOCHIP_BUILTIN_BREV, BUILT_IN_MD, NULL,
4272 NULL_TREE);
4273 add_builtin_function ("picoBrev", unsigned_ftype_unsigned,
4274 PICOCHIP_BUILTIN_BREV, BUILT_IN_MD, NULL,
4275 NULL_TREE);
4276
4277 /* Initialise the byte swap function. */
4278 add_builtin_function ("__builtin_byteswap", unsigned_ftype_unsigned,
4279 PICOCHIP_BUILTIN_BYTESWAP, BUILT_IN_MD, NULL,
4280 NULL_TREE);
4281 add_builtin_function ("picoByteSwap", unsigned_ftype_unsigned,
4282 PICOCHIP_BUILTIN_BYTESWAP, BUILT_IN_MD, NULL,
4283 NULL_TREE);
4284
4285 /* Initialise the ASRI function (note that while this can be coded
4286 using a signed shift in C, extra scratch registers are required,
4287 which we avoid by having a direct builtin to map to the
4288 instruction). */
4289 add_builtin_function ("__builtin_asri", int_ftype_int_int,
4290 PICOCHIP_BUILTIN_ASRI, BUILT_IN_MD, NULL,
4291 NULL_TREE);
4292
4293 /* Initialise saturating addition. */
4294 add_builtin_function ("__builtin_adds", int_ftype_int_int,
4295 PICOCHIP_BUILTIN_ADDS, BUILT_IN_MD, NULL,
4296 NULL_TREE);
4297 add_builtin_function ("picoAdds", int_ftype_int_int,
4298 PICOCHIP_BUILTIN_ADDS, BUILT_IN_MD, NULL,
4299 NULL_TREE);
4300
4301 /* Initialise saturating subtraction. */
4302 add_builtin_function ("__builtin_subs", int_ftype_int_int,
4303 PICOCHIP_BUILTIN_SUBS, BUILT_IN_MD, NULL,
4304 NULL_TREE);
4305 add_builtin_function ("picoSubs", int_ftype_int_int,
4306 PICOCHIP_BUILTIN_SUBS, BUILT_IN_MD, NULL,
4307 NULL_TREE);
4308
4309 /* Scalar comms builtins. */
4310 add_builtin_function ("__builtin_get", long_ftype_int,
4311 PICOCHIP_BUILTIN_GET, BUILT_IN_MD, NULL,
4312 NULL_TREE);
4313 add_builtin_function ("__builtin_put", void_ftype_int_long,
4314 PICOCHIP_BUILTIN_PUT, BUILT_IN_MD, NULL,
4315 NULL_TREE);
4316 add_builtin_function ("__builtin_testport", int_ftype_int,
4317 PICOCHIP_BUILTIN_TESTPORT, BUILT_IN_MD, NULL,
4318 NULL_TREE);
4319
4320 /* Array comms builtins. */
4321 add_builtin_function ("__builtin_put_array",
4322 void_ftype_long_int_int_int,
4323 PICOCHIP_BUILTIN_PUT_ARRAY, BUILT_IN_MD, NULL,
4324 NULL_TREE);
4325 add_builtin_function ("__builtin_get_array", long_ftype_int_int_int,
4326 PICOCHIP_BUILTIN_GET_ARRAY, BUILT_IN_MD, NULL,
4327 NULL_TREE);
4328 add_builtin_function ("__builtin_testport_array",
4329 int_ftype_int_int_int,
4330 PICOCHIP_BUILTIN_TESTPORT_ARRAY, BUILT_IN_MD,
4331 NULL, NULL_TREE);
4332
4333 /* Halt instruction. Note that the builtin function is marked as
4334 having the attribute `noreturn' so that the compiler realises
4335 that the halt stops the program dead. */
4336 noreturn = tree_cons (get_identifier ("noreturn"), NULL, NULL);
4337 add_builtin_function ("__builtin_halt", void_ftype_void,
4338 PICOCHIP_BUILTIN_HALT, BUILT_IN_MD, NULL,
4339 noreturn);
4340 add_builtin_function ("picoHalt", void_ftype_void,
4341 PICOCHIP_BUILTIN_HALT, BUILT_IN_MD, NULL,
4342 noreturn);
4343
4344 }
4345
4346 /* Expand a call to a builtin function. */
4347 rtx
4348 picochip_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
4349 enum machine_mode mode ATTRIBUTE_UNUSED,
4350 int ignore ATTRIBUTE_UNUSED)
4351 {
4352 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
4353 int fcode = DECL_FUNCTION_CODE (fndecl);
4354
4355 switch (fcode)
4356 {
4357 case PICOCHIP_BUILTIN_ASRI:
4358 return picochip_expand_builtin_3op (CODE_FOR_builtin_asri, exp,
4359 target);
4360
4361 case PICOCHIP_BUILTIN_ADDS:
4362 return picochip_expand_builtin_3op (CODE_FOR_sataddhi3, exp,
4363 target);
4364
4365 case PICOCHIP_BUILTIN_SUBS:
4366 return picochip_expand_builtin_3op (CODE_FOR_satsubhi3, exp,
4367 target);
4368
4369 case PICOCHIP_BUILTIN_SBC:
4370 return picochip_expand_builtin_2op (CODE_FOR_sbc, exp, target);
4371
4372 case PICOCHIP_BUILTIN_BREV:
4373 return picochip_expand_builtin_2op (CODE_FOR_brev, exp, target);
4374
4375 case PICOCHIP_BUILTIN_BYTESWAP:
4376 return picochip_expand_builtin_2op (CODE_FOR_bswaphi2, exp, target);
4377
4378 case PICOCHIP_BUILTIN_GET:
4379 return picochip_expand_builtin_2op (CODE_FOR_commsGet, exp, target);
4380
4381 case PICOCHIP_BUILTIN_PUT:
4382 return picochip_expand_builtin_2opvoid (CODE_FOR_commsPut, exp);
4383
4384 case PICOCHIP_BUILTIN_TESTPORT:
4385 return picochip_expand_builtin_2op (CODE_FOR_commsTestPort, exp,
4386 target);
4387
4388 case PICOCHIP_BUILTIN_PUT_ARRAY:
4389 return picochip_expand_array_put (exp, target);
4390
4391 case PICOCHIP_BUILTIN_GET_ARRAY:
4392 return picochip_expand_array_get (exp, target);
4393
4394 case PICOCHIP_BUILTIN_TESTPORT_ARRAY:
4395 return picochip_expand_array_testport (exp, target);
4396
4397 case PICOCHIP_BUILTIN_HALT:
4398 return picochip_generate_halt ();
4399
4400 default:
4401 gcc_unreachable();
4402
4403 }
4404
4405 /* Should really do something sensible here. */
4406 return NULL_RTX;
4407 }
4408 \f
4409 /* Emit warnings. */
4410 static void
4411 picochip_warn_inefficient (const char *msg)
4412 {
4413 if (TARGET_INEFFICIENT_WARNINGS)
4414 warning (OPT_minefficient_warnings,
4415 "%s (disable warning using -mno-inefficient-warnings)", msg);
4416 }
4417
4418 void
4419 warn_of_byte_access (void)
4420 {
4421 static int warned = 0;
4422
4423 if (!warned)
4424 {
4425 picochip_warn_inefficient
4426 ("byte access is synthesised - consider using MUL AE");
4427 warned = 1;
4428 }
4429
4430 }
4431 \f
4432 rtx
4433 picochip_function_value (const_tree valtype, const_tree func,
4434 bool outgoing ATTRIBUTE_UNUSED)
4435 {
4436 enum machine_mode mode = TYPE_MODE (valtype);
4437 int unsignedp = TYPE_UNSIGNED (valtype);
4438
4439 /* Since we define PROMOTE_FUNCTION_RETURN, we must promote the mode
4440 just as PROMOTE_MODE does. */
4441 mode = promote_function_mode (valtype, mode, &unsignedp, func, 1);
4442
4443 return gen_rtx_REG (mode, 0);
4444
4445 }
4446 \f
4447 /* Check that the value of the given mode will fit in the register of
4448 the given mode. */
4449 int
4450 picochip_hard_regno_mode_ok (int regno, enum machine_mode mode)
4451 {
4452
4453 if (GET_MODE_CLASS (mode) == MODE_CC)
4454 return regno == CC_REGNUM;
4455
4456 /* If the CC register is being used, then only CC mode values are
4457 allowed (which have already been tested). */
4458 if (regno == CC_REGNUM || regno == ACC_REGNUM)
4459 return 0;
4460
4461 /* Must be a valid register. */
4462 if (regno > 16)
4463 return 0;
4464
4465 /* Modes QI and HI may be placed in any register except the CC. */
4466 if (mode == QImode || mode == HImode)
4467 return 1;
4468
4469 /* DI must be in a quad register. */
4470 if (mode == DImode)
4471 return (regno % 4 == 0);
4472
4473 /* All other modes must be placed in a even numbered register. */
4474 return !(regno & 1);
4475
4476 }
4477 \f
4478 /* Extract the lower and upper components of a constant value. */
4479
4480 rtx
4481 picochip_get_low_const (rtx value)
4482 {
4483 return gen_int_mode (INTVAL (value) & 0xFFFF, HImode);
4484 }
4485
4486 rtx
4487 picochip_get_high_const (rtx value)
4488 {
4489 /*return GEN_INT ((((INTVAL (value) >> 16) & 0xFFFF) ^ 0x8000) - 0x8000); */
4490 return gen_int_mode ((INTVAL (value) >> 16) & 0xFFFF, HImode);
4491 }
4492
4493 \f
4494 /* Loading and storing QImode values to and from memory in a machine
4495 without byte access requires might require a scratch
4496 register. However, the scratch register might correspond to the
4497 register in which the value is being loaded. To ensure that a
4498 scratch register is supplied which is definitely different to the
4499 output register, request a register pair. This effectively gives a
4500 choice of two registers to choose from, so that we a guaranteed to
4501 get at least one register which is different to the output
4502 register. This trick is taken from the alpha implementation. */
4503 static reg_class_t
4504 picochip_secondary_reload (bool in_p,
4505 rtx x ATTRIBUTE_UNUSED,
4506 reg_class_t cla ATTRIBUTE_UNUSED,
4507 enum machine_mode mode,
4508 secondary_reload_info *sri)
4509 {
4510 if (mode == QImode && !TARGET_HAS_BYTE_ACCESS)
4511 {
4512 if (in_p == 0)
4513 sri->icode = CODE_FOR_reload_outqi;
4514 else
4515 sri->icode = CODE_FOR_reload_inqi;
4516 }
4517
4518 /* We dont need to return a register class type when we need only a
4519 scratch register. It realizes the scratch register type by looking
4520 at the instruction definition for sri->icode. We only need to
4521 return the register type when we need intermediaries for copies.*/
4522 return NO_REGS;
4523 }
4524 \f
4525 /* Return true if the given memory operand can be aligned to a
4526 word+offset memory reference (e.g., FP+3 can be converted into the
4527 memory operand FP+2, with the offset 1). */
4528 int
4529 picochip_alignable_memory_operand (rtx mem_operand,
4530 enum machine_mode mode ATTRIBUTE_UNUSED)
4531 {
4532 rtx address;
4533
4534 /* Not a mem operand. Refuse immediately. */
4535 if (MEM != GET_CODE (mem_operand))
4536 return 0;
4537
4538 address = XEXP (mem_operand, 0);
4539
4540 /* Return true if a PLUS of the SP and a (valid) constant, or SP itself. */
4541 return ((PLUS == GET_CODE (address) &&
4542 REGNO (XEXP (address, 0)) == STACK_POINTER_REGNUM &&
4543 CONST_INT == GET_CODE (XEXP (address, 1)) &&
4544 picochip_const_ok_for_letter_p (INTVAL (XEXP (address, 1)), 'K'))
4545 || (REG == GET_CODE (address)
4546 && REGNO (address) == STACK_POINTER_REGNUM));
4547
4548 }
4549 \f
4550 /* Return true if the given memory reference is to a word aligned
4551 address. Currently this means it must be either SP, or
4552 SP+offset. We could replace this function with alignable
4553 memory references in the above function?. */
4554 int
4555 picochip_word_aligned_memory_reference (rtx operand)
4556 {
4557
4558
4559 /* The address must be the SP register, or a constant, aligned
4560 offset from SP which doesn't exceed the FP+offset
4561 restrictions. */
4562 return ((PLUS == GET_CODE (operand)
4563 && REGNO (XEXP (operand, 0)) == STACK_POINTER_REGNUM
4564 && picochip_is_aligned (INTVAL (XEXP (operand, 1)), 16)
4565 && picochip_const_ok_for_letter_p (INTVAL (XEXP (operand, 1)),
4566 'K'))
4567 || (REG == GET_CODE (operand)
4568 && REGNO (operand) == STACK_POINTER_REGNUM));
4569
4570 }
4571 \f
4572 /* Given an alignable memory location, convert the memory location
4573 into a HI mode access, storing the new memory reference in
4574 paligned_mem, and the number of bits by which to shift in pbitnum
4575 (i.e., given a reference to FP+3, this creates an aligned reference
4576 of FP+2, with an 8-bit shift). This code is a modification of that
4577 found in the Alpha port. */
4578 void
4579 picochip_get_hi_aligned_mem (rtx ref, rtx * paligned_mem, rtx * pbitnum)
4580 {
4581 rtx base;
4582 HOST_WIDE_INT offset = 0;
4583
4584 gcc_assert (GET_CODE (ref) == MEM);
4585
4586 if (reload_in_progress && !memory_address_p (GET_MODE (ref), XEXP (ref, 0)))
4587 {
4588 base = find_replacement (&XEXP (ref, 0));
4589
4590 gcc_assert(memory_address_p (GET_MODE (ref), base));
4591 }
4592 else
4593 {
4594 base = XEXP (ref, 0);
4595 }
4596
4597 if (GET_CODE (base) == PLUS)
4598 {
4599 offset += INTVAL (XEXP (base, 1));
4600 base = XEXP (base, 0);
4601 }
4602
4603 *paligned_mem = widen_memory_access (ref, HImode, (offset & ~1) - offset);
4604
4605 if (offset > 0)
4606 {
4607 if (TARGET_DEBUG)
4608 {
4609 printf
4610 ("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");
4611 }
4612 }
4613
4614 *pbitnum = GEN_INT ((offset & 1) * 8);
4615
4616 }
4617 \f
4618 /* Return true if the given operand is an absolute address in memory
4619 (i.e., a symbolic offset). */
4620 int
4621 picochip_absolute_memory_operand (rtx op,
4622 enum machine_mode mode ATTRIBUTE_UNUSED)
4623 {
4624
4625 if (MEM == GET_CODE (op))
4626 {
4627 rtx address = XEXP (op, 0);
4628
4629 /* Symbols are valid absolute addresses. */
4630 if (SYMBOL_REF == GET_CODE (address))
4631 return 1;
4632
4633 /* Constant offsets to symbols are valid absolute addresses. */
4634 if (CONST == GET_CODE (address) &&
4635 PLUS == GET_CODE (XEXP (address, 0)) &&
4636 SYMBOL_REF == GET_CODE (XEXP (XEXP (address, 0), 0)) &&
4637 CONST_INT == GET_CODE (XEXP (XEXP (address, 0), 1)))
4638 return 1;
4639
4640 }
4641 else
4642 return 0;
4643
4644 /* Symbols are valid absolute addresses. */
4645 if (SYMBOL_REF == GET_CODE (XEXP (op, 0)))
4646 return 1;
4647
4648
4649 return 0;
4650
4651 }
4652 \f
4653 void
4654 picochip_asm_named_section (const char *name,
4655 unsigned int flags ATTRIBUTE_UNUSED,
4656 tree decl ATTRIBUTE_UNUSED)
4657 {
4658 fprintf (asm_out_file, ".section %s\n", name);
4659 }
4660 \f
4661
4662 /* Check if we can make a conditional copy instruction. This is emitted as an
4663 instruction to set the condition register, followed by an instruction which
4664 uses the condition registers to perform the conditional move. */
4665 int
4666 picochip_check_conditional_copy (rtx * operands)
4667 {
4668
4669 rtx branch_op_0 = XEXP (operands[1], 0);
4670 rtx branch_op_1 = XEXP (operands[1], 1);
4671
4672 /* Only HI mode conditional moves are currently allowed. Can we add
4673 SI mode moves? */
4674 if (GET_CODE (operands[1]) != EQ && GET_CODE (operands[1]) != NE)
4675 return 0;
4676
4677 /* Is the comparison valid? Only allow operands which are registers
4678 if they are HImode. SI mode comparisons against 0 could be
4679 handled using logical operations (e.g., SIreg != 0 when low ||
4680 high). Need to find test cases to provoke this though (fixunssfdi
4681 in libgcc does, but is complicated). */
4682 if (register_operand(branch_op_0, GET_MODE(branch_op_0)) &&
4683 GET_MODE(branch_op_0) != HImode)
4684 return 0;
4685 if (register_operand(branch_op_1, GET_MODE(branch_op_1)) &&
4686 GET_MODE(branch_op_1) != HImode)
4687 return 0;
4688
4689 return 1;
4690
4691 }
4692
4693 \f
4694 static rtx
4695 picochip_static_chain (const_tree ARG_UNUSED (fndecl), bool incoming_p)
4696 {
4697 rtx addr;
4698 if (incoming_p)
4699 addr = arg_pointer_rtx;
4700 else
4701 addr = plus_constant (Pmode, stack_pointer_rtx, -2 * UNITS_PER_WORD);
4702 return gen_frame_mem (Pmode, addr);
4703 }