Debian Codegraph @

gcc/config/stormy16/stormy16.cc

raw
1 /* Xstormy16 target functions.
2 Copyright (C) 1997-2025 Free Software Foundation, Inc.
3 Contributed by Red Hat, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #define IN_TARGET_CODE 1
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "backend.h"
27 #include "target.h"
28 #include "rtl.h"
29 #include "tree.h"
30 #include "stringpool.h"
31 #include "attribs.h"
32 #include "gimple.h"
33 #include "df.h"
34 #include "memmodel.h"
35 #include "tm_p.h"
36 #include "stringpool.h"
37 #include "optabs.h"
38 #include "emit-rtl.h"
39 #include "recog.h"
40 #include "diagnostic-core.h"
41 #include "output.h"
42 #include "fold-const.h"
43 #include "stor-layout.h"
44 #include "varasm.h"
45 #include "calls.h"
46 #include "explow.h"
47 #include "expr.h"
48 #include "langhooks.h"
49 #include "cfgrtl.h"
50 #include "gimplify.h"
51 #include "reload.h"
52 #include "builtins.h"
53
54 /* This file should be included last. */
55 #include "target-def.h"
56
57 static rtx emit_addhi3_postreload (rtx, rtx, rtx);
58 static void xstormy16_asm_out_constructor (rtx, int);
59 static void xstormy16_asm_out_destructor (rtx, int);
60 static void xstormy16_asm_output_mi_thunk (FILE *, tree, HOST_WIDE_INT,
61 HOST_WIDE_INT, tree);
62
63 static void xstormy16_init_builtins (void);
64 static rtx xstormy16_expand_builtin (tree, rtx, rtx, machine_mode, int);
65 static int xstormy16_address_cost (rtx, machine_mode, addr_space_t, bool);
66 static bool xstormy16_return_in_memory (const_tree, const_tree);
67
68 static GTY(()) section *bss100_section;
69
70 /* Compute a (partial) cost for rtx X. Return true if the complete
71 cost has been computed, and false if subexpressions should be
72 scanned. In either case, *TOTAL contains the cost result. */
73
74 static bool
75 xstormy16_rtx_costs (rtx x, machine_mode mode,
76 int outer_code ATTRIBUTE_UNUSED,
77 int opno ATTRIBUTE_UNUSED, int *total, bool speed_p)
78 {
79 rtx_code code = GET_CODE (x);
80
81 switch (code)
82 {
83 case CONST_INT:
84 if (mode == SImode)
85 {
86 HOST_WIDE_INT lo_word = INTVAL (x) & 0xffff;
87 HOST_WIDE_INT hi_word = INTVAL (x) >> 16;
88 *total = COSTS_N_INSNS (IN_RANGE (lo_word, 0, 255) ? 1 : 2);
89 *total += COSTS_N_INSNS (IN_RANGE (hi_word, 0, 255) ? 1 : 2);
90 }
91 else if (mode == QImode || IN_RANGE(INTVAL (x), 0, 255))
92 *total = COSTS_N_INSNS (1);
93 else
94 *total = COSTS_N_INSNS (2);
95 return true;
96
97 case CONST_DOUBLE:
98 case CONST:
99 case SYMBOL_REF:
100 case LABEL_REF:
101 *total = COSTS_N_INSNS (2);
102 return true;
103
104 case PLUS:
105 case MINUS:
106 if (mode == SImode)
107 {
108 if (CONST_INT_P (XEXP (x, 1)))
109 {
110 HOST_WIDE_INT lo_word = INTVAL (XEXP (x, 1)) & 0xffff;
111 HOST_WIDE_INT hi_word = INTVAL (XEXP (x, 1)) >> 16;
112 if (IN_RANGE (lo_word, 0, 16))
113 *total = COSTS_N_INSNS (1);
114 else
115 *total = COSTS_N_INSNS (2);
116 if (IN_RANGE (hi_word, 0, 16))
117 *total += COSTS_N_INSNS (1);
118 else
119 *total += COSTS_N_INSNS (2);
120 }
121 else
122 {
123 *total = COSTS_N_INSNS (2);
124 *total += rtx_cost (XEXP (x, 1), mode, code, 0, speed_p);
125 }
126 *total += rtx_cost (XEXP (x, 0), mode, code, 0, speed_p);
127 return true;
128 }
129 else
130 {
131 if (CONST_INT_P (XEXP (x, 1)))
132 {
133 if (IN_RANGE (INTVAL (XEXP (x, 1)), 0, 16))
134 *total = COSTS_N_INSNS (1);
135 else
136 *total = COSTS_N_INSNS (2);
137 }
138 else
139 {
140 *total = COSTS_N_INSNS (1);
141 *total += rtx_cost (XEXP (x, 1), mode, code, 0, speed_p);
142 }
143 *total += rtx_cost (XEXP (x, 0), mode, code, 0, speed_p);
144 return true;
145 }
146 return false;
147
148 case MULT:
149 if (mode == QImode)
150 *total = COSTS_N_INSNS (speed_p ? 18 + 5 : 6);
151 else if (mode == SImode)
152 *total = COSTS_N_INSNS (speed_p ? 3 * 18 + 14 : 17);
153 else
154 *total = COSTS_N_INSNS (speed_p ? 18 + 3 : 4);
155 return false;
156
157 case DIV:
158 case MOD:
159 if (mode == QImode)
160 *total = COSTS_N_INSNS (speed_p ? 19 + 6 : 7);
161 else if (mode == SImode)
162 *total = COSTS_N_INSNS (speed_p ? 100 : 7);
163 else
164 *total = COSTS_N_INSNS (speed_p ? 19 + 3 : 4);
165 return false;
166
167 case UDIV:
168 case UMOD:
169 if (mode == QImode)
170 *total = COSTS_N_INSNS (speed_p ? 18 + 7 : 8);
171 else if (mode == SImode)
172 *total = COSTS_N_INSNS (speed_p ? 100 : 7);
173 else
174 *total = COSTS_N_INSNS (speed_p ? 18 + 3 : 4);
175 return false;
176
177 case ASHIFT:
178 case ASHIFTRT:
179 case LSHIFTRT:
180 if (REG_P (XEXP (x, 0))
181 && CONST_INT_P (XEXP (x, 1)))
182 {
183 if (mode == HImode)
184 {
185 /* asr/shl/shr. */
186 *total = COSTS_N_INSNS (1);
187 return true;
188 }
189 else if (mode == QImode)
190 {
191 /* (shl+shr)+shr. */
192 *total = COSTS_N_INSNS (3);
193 return true;
194 }
195 else if (mode == SImode)
196 {
197 if (IN_RANGE (INTVAL (XEXP (x, 1)), 16, 31))
198 *total = COSTS_N_INSNS (3);
199 else
200 *total = COSTS_N_INSNS (5);
201 return true;
202 }
203 }
204 return false;
205
206 case ZERO_EXTEND:
207 if (mode == HImode)
208 {
209 if (GET_MODE (XEXP (x, 0)) == QImode)
210 /* shl+shr. */
211 *total = COSTS_N_INSNS (2);
212 }
213 else if (mode == SImode)
214 {
215 if (GET_MODE (XEXP (x, 0)) == HImode)
216 /* mov+mov. */
217 *total = COSTS_N_INSNS (2);
218 else if (GET_MODE (XEXP (x, 0)) == QImode)
219 /* mov+shl+shr+mov. */
220 *total = COSTS_N_INSNS (4);
221 }
222 return false;
223
224 case SIGN_EXTEND:
225 if (mode == HImode)
226 {
227 if (GET_MODE (XEXP (x, 0)) == QImode)
228 /* cbw. */
229 *total = COSTS_N_INSNS (1);
230 }
231 else if (mode == SImode)
232 {
233 if (GET_MODE (XEXP (x, 0)) == HImode)
234 /* mov+asr. */
235 *total = COSTS_N_INSNS (2);
236 else if (GET_MODE (XEXP (x, 0)) == QImode)
237 /* mov+shl+shr+mov. */
238 *total = COSTS_N_INSNS (3);
239 }
240 return false;
241
242 case SET:
243 if (REG_P (XEXP (x, 0)))
244 {
245 if (!REG_P (XEXP (x, 1)))
246 *total = rtx_cost (XEXP (x, 1), mode, SET, 1, speed_p);
247 return true;
248 }
249 return false;
250
251 default:
252 return false;
253 }
254 }
255
256 static int
257 xstormy16_address_cost (rtx x, machine_mode mode ATTRIBUTE_UNUSED,
258 addr_space_t as ATTRIBUTE_UNUSED,
259 bool speed ATTRIBUTE_UNUSED)
260 {
261 return (CONST_INT_P (x) ? 2
262 : GET_CODE (x) == PLUS ? 7
263 : 5);
264 }
265
266 /* Worker function for TARGET_MEMORY_MOVE_COST. */
267
268 static int
269 xstormy16_memory_move_cost (machine_mode mode, reg_class_t rclass,
270 bool in)
271 {
272 return (5 + memory_move_secondary_cost (mode, rclass, in));
273 }
274
275 /* Branches are handled as follows:
276
277 1. HImode compare-and-branches. The machine supports these
278 natively, so the appropriate pattern is emitted directly.
279
280 2. SImode EQ and NE. These are emitted as pairs of HImode
281 compare-and-branches.
282
283 3. SImode LT, GE, LTU and GEU. These are emitted as a sequence
284 of a SImode subtract followed by a branch (not a compare-and-branch),
285 like this:
286 sub
287 sbc
288 blt
289
290 4. SImode GT, LE, GTU, LEU. These are emitted as a sequence like:
291 sub
292 sbc
293 blt
294 or
295 bne. */
296
297 /* Emit a branch of kind CODE to location LOC. */
298
299 void
300 xstormy16_emit_cbranch (enum rtx_code code, rtx op0, rtx op1, rtx loc)
301 {
302 rtx condition_rtx, loc_ref, branch, cy_clobber;
303 rtvec vec;
304 machine_mode mode;
305
306 mode = GET_MODE (op0);
307 gcc_assert (mode == HImode || mode == SImode);
308
309 if (mode == SImode
310 && (code == GT || code == LE || code == GTU || code == LEU))
311 {
312 int unsigned_p = (code == GTU || code == LEU);
313 int gt_p = (code == GT || code == GTU);
314 rtx lab = NULL_RTX;
315
316 if (gt_p)
317 lab = gen_label_rtx ();
318 xstormy16_emit_cbranch (unsigned_p ? LTU : LT, op0, op1, gt_p ? lab : loc);
319 /* This should be generated as a comparison against the temporary
320 created by the previous insn, but reload can't handle that. */
321 xstormy16_emit_cbranch (gt_p ? NE : EQ, op0, op1, loc);
322 if (gt_p)
323 emit_label (lab);
324 return;
325 }
326 else if (mode == SImode
327 && (code == NE || code == EQ)
328 && op1 != const0_rtx)
329 {
330 rtx op0_word, op1_word;
331 rtx lab = NULL_RTX;
332 int num_words = GET_MODE_BITSIZE (mode) / BITS_PER_WORD;
333 int i;
334
335 if (code == EQ)
336 lab = gen_label_rtx ();
337
338 for (i = 0; i < num_words - 1; i++)
339 {
340 op0_word = simplify_gen_subreg (word_mode, op0, mode,
341 i * UNITS_PER_WORD);
342 op1_word = simplify_gen_subreg (word_mode, op1, mode,
343 i * UNITS_PER_WORD);
344 xstormy16_emit_cbranch (NE, op0_word, op1_word, code == EQ ? lab : loc);
345 }
346 op0_word = simplify_gen_subreg (word_mode, op0, mode,
347 i * UNITS_PER_WORD);
348 op1_word = simplify_gen_subreg (word_mode, op1, mode,
349 i * UNITS_PER_WORD);
350 xstormy16_emit_cbranch (code, op0_word, op1_word, loc);
351
352 if (code == EQ)
353 emit_label (lab);
354 return;
355 }
356
357 /* We can't allow reload to try to generate any reload after a branch,
358 so when some register must match we must make the temporary ourselves. */
359 if (mode != HImode)
360 {
361 rtx tmp;
362 tmp = gen_reg_rtx (mode);
363 emit_move_insn (tmp, op0);
364 op0 = tmp;
365 }
366
367 condition_rtx = gen_rtx_fmt_ee (code, mode, op0, op1);
368 loc_ref = gen_rtx_LABEL_REF (VOIDmode, loc);
369 branch = gen_rtx_SET (pc_rtx,
370 gen_rtx_IF_THEN_ELSE (VOIDmode, condition_rtx,
371 loc_ref, pc_rtx));
372
373 cy_clobber = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (BImode, CARRY_REGNUM));
374
375 if (mode == HImode)
376 vec = gen_rtvec (2, branch, cy_clobber);
377 else if (code == NE || code == EQ)
378 vec = gen_rtvec (2, branch, gen_rtx_CLOBBER (VOIDmode, op0));
379 else
380 {
381 rtx sub;
382 #if 0
383 sub = gen_rtx_SET (op0, gen_rtx_MINUS (SImode, op0, op1));
384 #else
385 sub = gen_rtx_CLOBBER (SImode, op0);
386 #endif
387 vec = gen_rtvec (3, branch, sub, cy_clobber);
388 }
389
390 emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, vec));
391 }
392
393 /* Take a SImode conditional branch, one of GT/LE/GTU/LEU, and split
394 the arithmetic operation. Most of the work is done by
395 xstormy16_expand_arith. */
396
397 void
398 xstormy16_split_cbranch (machine_mode mode, rtx label, rtx comparison,
399 rtx dest)
400 {
401 rtx op0 = XEXP (comparison, 0);
402 rtx op1 = XEXP (comparison, 1);
403 rtx_insn *seq, *last_insn;
404 rtx compare;
405
406 start_sequence ();
407 xstormy16_expand_arith (mode, COMPARE, dest, op0, op1);
408 seq = get_insns ();
409 end_sequence ();
410
411 gcc_assert (INSN_P (seq));
412
413 last_insn = seq;
414 while (NEXT_INSN (last_insn) != NULL_RTX)
415 last_insn = NEXT_INSN (last_insn);
416
417 compare = SET_SRC (XVECEXP (PATTERN (last_insn), 0, 0));
418 PUT_CODE (XEXP (compare, 0), GET_CODE (comparison));
419 XEXP (compare, 1) = gen_rtx_LABEL_REF (VOIDmode, label);
420 emit_insn (seq);
421 }
422
423
424 /* Return the string to output a conditional branch to LABEL, which is
425 the operand number of the label.
426
427 OP is the conditional expression, or NULL for branch-always.
428
429 REVERSED is nonzero if we should reverse the sense of the comparison.
430
431 INSN is the insn. */
432
433 char *
434 xstormy16_output_cbranch_hi (rtx op, const char *label, int reversed,
435 rtx_insn *insn)
436 {
437 static char string[64];
438 int need_longbranch = (op != NULL_RTX
439 ? get_attr_length (insn) == 8
440 : get_attr_length (insn) == 4);
441 int really_reversed = reversed ^ need_longbranch;
442 const char *ccode;
443 const char *templ;
444 const char *operands;
445 enum rtx_code code;
446
447 if (! op)
448 {
449 if (need_longbranch)
450 ccode = "jmpf";
451 else
452 ccode = "br";
453 sprintf (string, "%s %s", ccode, label);
454 return string;
455 }
456
457 code = GET_CODE (op);
458
459 if (! REG_P (XEXP (op, 0)))
460 {
461 code = swap_condition (code);
462 operands = "%3,%2";
463 }
464 else
465 operands = "%2,%3";
466
467 /* Work out which way this really branches. */
468 if (really_reversed)
469 code = reverse_condition (code);
470
471 switch (code)
472 {
473 case EQ: ccode = "z"; break;
474 case NE: ccode = "nz"; break;
475 case GE: ccode = "ge"; break;
476 case LT: ccode = "lt"; break;
477 case GT: ccode = "gt"; break;
478 case LE: ccode = "le"; break;
479 case GEU: ccode = "nc"; break;
480 case LTU: ccode = "c"; break;
481 case GTU: ccode = "hi"; break;
482 case LEU: ccode = "ls"; break;
483
484 default:
485 gcc_unreachable ();
486 }
487
488 if (need_longbranch)
489 templ = "b%s %s,.+8 | jmpf %s";
490 else
491 templ = "b%s %s,%s";
492 sprintf (string, templ, ccode, operands, label);
493
494 return string;
495 }
496
497 /* Return the string to output a conditional branch to LABEL, which is
498 the operand number of the label, but suitable for the tail of a
499 SImode branch.
500
501 OP is the conditional expression (OP is never NULL_RTX).
502
503 REVERSED is nonzero if we should reverse the sense of the comparison.
504
505 INSN is the insn. */
506
507 char *
508 xstormy16_output_cbranch_si (rtx op, const char *label, int reversed,
509 rtx_insn *insn)
510 {
511 static char string[64];
512 int need_longbranch = get_attr_length (insn) >= 8;
513 int really_reversed = reversed ^ need_longbranch;
514 const char *ccode;
515 const char *templ;
516 char prevop[16];
517 enum rtx_code code;
518
519 code = GET_CODE (op);
520
521 /* Work out which way this really branches. */
522 if (really_reversed)
523 code = reverse_condition (code);
524
525 switch (code)
526 {
527 case EQ: ccode = "z"; break;
528 case NE: ccode = "nz"; break;
529 case GE: ccode = "ge"; break;
530 case LT: ccode = "lt"; break;
531 case GEU: ccode = "nc"; break;
532 case LTU: ccode = "c"; break;
533
534 /* The missing codes above should never be generated. */
535 default:
536 gcc_unreachable ();
537 }
538
539 switch (code)
540 {
541 case EQ: case NE:
542 {
543 int regnum;
544
545 gcc_assert (REG_P (XEXP (op, 0)));
546
547 regnum = REGNO (XEXP (op, 0));
548 sprintf (prevop, "or %s,%s", reg_names[regnum], reg_names[regnum+1]);
549 }
550 break;
551
552 case GE: case LT: case GEU: case LTU:
553 strcpy (prevop, "sbc %2,%3");
554 break;
555
556 default:
557 gcc_unreachable ();
558 }
559
560 if (need_longbranch)
561 templ = "%s | b%s .+6 | jmpf %s";
562 else
563 templ = "%s | b%s %s";
564 sprintf (string, templ, prevop, ccode, label);
565
566 return string;
567 }
568
569 /* Many machines have some registers that cannot be copied directly to or from
570 memory or even from other types of registers. An example is the `MQ'
571 register, which on most machines, can only be copied to or from general
572 registers, but not memory. Some machines allow copying all registers to and
573 from memory, but require a scratch register for stores to some memory
574 locations (e.g., those with symbolic address on the RT, and those with
575 certain symbolic address on the SPARC when compiling PIC). In some cases,
576 both an intermediate and a scratch register are required.
577
578 You should define these macros to indicate to the reload phase that it may
579 need to allocate at least one register for a reload in addition to the
580 register to contain the data. Specifically, if copying X to a register
581 RCLASS in MODE requires an intermediate register, you should define
582 `SECONDARY_INPUT_RELOAD_CLASS' to return the largest register class all of
583 whose registers can be used as intermediate registers or scratch registers.
584
585 If copying a register RCLASS in MODE to X requires an intermediate or scratch
586 register, `SECONDARY_OUTPUT_RELOAD_CLASS' should be defined to return the
587 largest register class required. If the requirements for input and output
588 reloads are the same, the macro `SECONDARY_RELOAD_CLASS' should be used
589 instead of defining both macros identically.
590
591 The values returned by these macros are often `GENERAL_REGS'. Return
592 `NO_REGS' if no spare register is needed; i.e., if X can be directly copied
593 to or from a register of RCLASS in MODE without requiring a scratch register.
594 Do not define this macro if it would always return `NO_REGS'.
595
596 If a scratch register is required (either with or without an intermediate
597 register), you should define patterns for `reload_inM' or `reload_outM', as
598 required.. These patterns, which will normally be implemented with a
599 `define_expand', should be similar to the `movM' patterns, except that
600 operand 2 is the scratch register.
601
602 Define constraints for the reload register and scratch register that contain
603 a single register class. If the original reload register (whose class is
604 RCLASS) can meet the constraint given in the pattern, the value returned by
605 these macros is used for the class of the scratch register. Otherwise, two
606 additional reload registers are required. Their classes are obtained from
607 the constraints in the insn pattern.
608
609 X might be a pseudo-register or a `subreg' of a pseudo-register, which could
610 either be in a hard register or in memory. Use `true_regnum' to find out;
611 it will return -1 if the pseudo is in memory and the hard register number if
612 it is in a register.
613
614 These macros should not be used in the case where a particular class of
615 registers can only be copied to memory and not to another class of
616 registers. In that case, secondary reload registers are not needed and
617 would not be helpful. Instead, a stack location must be used to perform the
618 copy and the `movM' pattern should use memory as an intermediate storage.
619 This case often occurs between floating-point and general registers. */
620
621 enum reg_class
622 xstormy16_secondary_reload_class (enum reg_class rclass,
623 machine_mode mode ATTRIBUTE_UNUSED,
624 rtx x)
625 {
626 /* This chip has the interesting property that only the first eight
627 registers can be moved to/from memory. */
628 if ((MEM_P (x)
629 || ((GET_CODE (x) == SUBREG || REG_P (x))
630 && (true_regnum (x) == -1
631 || true_regnum (x) >= FIRST_PSEUDO_REGISTER)))
632 && ! reg_class_subset_p (rclass, EIGHT_REGS))
633 return EIGHT_REGS;
634
635 return NO_REGS;
636 }
637
638 /* Worker function for TARGET_PREFERRED_RELOAD_CLASS
639 and TARGET_PREFERRED_OUTPUT_RELOAD_CLASS. */
640
641 static reg_class_t
642 xstormy16_preferred_reload_class (rtx x, reg_class_t rclass)
643 {
644 /* Only the first eight registers can be moved to/from memory.
645 So those prefer EIGHT_REGS.
646
647 Similarly reloading an auto-increment address is going to
648 require loads and stores, so we must use EIGHT_REGS for those
649 too. */
650 if (rclass == GENERAL_REGS
651 && (MEM_P (x)
652 || GET_CODE (x) == POST_INC
653 || GET_CODE (x) == PRE_DEC
654 || GET_CODE (x) == PRE_MODIFY))
655 return EIGHT_REGS;
656
657 return rclass;
658 }
659
660 /* Predicate for symbols and addresses that reflect special 8-bit
661 addressing. */
662
663 bool
664 xstormy16_below100_symbol (rtx x,
665 machine_mode mode ATTRIBUTE_UNUSED)
666 {
667 if (GET_CODE (x) == CONST)
668 x = XEXP (x, 0);
669 if (GET_CODE (x) == PLUS && CONST_INT_P (XEXP (x, 1)))
670 x = XEXP (x, 0);
671
672 if (GET_CODE (x) == SYMBOL_REF)
673 return (SYMBOL_REF_FLAGS (x) & SYMBOL_FLAG_XSTORMY16_BELOW100) != 0;
674
675 if (CONST_INT_P (x))
676 {
677 HOST_WIDE_INT i = INTVAL (x);
678
679 if ((i >= 0x0000 && i <= 0x00ff)
680 || (i >= 0x7f00 && i <= 0x7fff))
681 return 1;
682 }
683 return 0;
684 }
685
686 /* Likewise, but only for non-volatile MEMs, for patterns where the
687 MEM will get split into smaller sized accesses. */
688
689 bool
690 xstormy16_splittable_below100_operand (rtx x, machine_mode mode)
691 {
692 if (MEM_P (x) && MEM_VOLATILE_P (x))
693 return 0;
694 return xstormy16_below100_operand (x, mode);
695 }
696
697 /* Expand an 8-bit IOR. This either detects the one case we can
698 actually do, or uses a 16-bit IOR. */
699
700 void
701 xstormy16_expand_iorqi3 (rtx *operands)
702 {
703 rtx in, out, outsub, val;
704
705 out = operands[0];
706 in = operands[1];
707 val = operands[2];
708
709 if (xstormy16_onebit_set_operand (val, QImode))
710 {
711 if (!xstormy16_below100_or_register (in, QImode))
712 in = copy_to_mode_reg (QImode, in);
713 if (!xstormy16_below100_or_register (out, QImode))
714 out = gen_reg_rtx (QImode);
715 emit_insn (gen_iorqi3_internal (out, in, val));
716 if (out != operands[0])
717 emit_move_insn (operands[0], out);
718 return;
719 }
720
721 if (! REG_P (in))
722 in = copy_to_mode_reg (QImode, in);
723
724 if (! REG_P (val) && ! CONST_INT_P (val))
725 val = copy_to_mode_reg (QImode, val);
726
727 if (! REG_P (out))
728 out = gen_reg_rtx (QImode);
729
730 in = simplify_gen_subreg (HImode, in, QImode, 0);
731 outsub = simplify_gen_subreg (HImode, out, QImode, 0);
732
733 if (! CONST_INT_P (val))
734 val = simplify_gen_subreg (HImode, val, QImode, 0);
735
736 emit_insn (gen_iorhi3 (outsub, in, val));
737
738 if (out != operands[0])
739 emit_move_insn (operands[0], out);
740 }
741
742 /* Expand an 8-bit AND. This either detects the one case we can
743 actually do, or uses a 16-bit AND. */
744
745 void
746 xstormy16_expand_andqi3 (rtx *operands)
747 {
748 rtx in, out, outsub, val;
749
750 out = operands[0];
751 in = operands[1];
752 val = operands[2];
753
754 if (xstormy16_onebit_clr_operand (val, QImode))
755 {
756 if (!xstormy16_below100_or_register (in, QImode))
757 in = copy_to_mode_reg (QImode, in);
758 if (!xstormy16_below100_or_register (out, QImode))
759 out = gen_reg_rtx (QImode);
760 emit_insn (gen_andqi3_internal (out, in, val));
761 if (out != operands[0])
762 emit_move_insn (operands[0], out);
763 return;
764 }
765
766 if (! REG_P (in))
767 in = copy_to_mode_reg (QImode, in);
768
769 if (! REG_P (val) && ! CONST_INT_P (val))
770 val = copy_to_mode_reg (QImode, val);
771
772 if (! REG_P (out))
773 out = gen_reg_rtx (QImode);
774
775 in = simplify_gen_subreg (HImode, in, QImode, 0);
776 outsub = simplify_gen_subreg (HImode, out, QImode, 0);
777
778 if (! CONST_INT_P (val))
779 val = simplify_gen_subreg (HImode, val, QImode, 0);
780
781 emit_insn (gen_andhi3 (outsub, in, val));
782
783 if (out != operands[0])
784 emit_move_insn (operands[0], out);
785 }
786
787 #define LEGITIMATE_ADDRESS_INTEGER_P(X, OFFSET) \
788 (CONST_INT_P (X) \
789 && (unsigned HOST_WIDE_INT) (INTVAL (X) + (OFFSET) + 2048) < 4096)
790
791 #define LEGITIMATE_ADDRESS_CONST_INT_P(X, OFFSET) \
792 (CONST_INT_P (X) \
793 && INTVAL (X) + (OFFSET) >= 0 \
794 && INTVAL (X) + (OFFSET) < 0x8000 \
795 && (INTVAL (X) + (OFFSET) < 0x100 || INTVAL (X) + (OFFSET) >= 0x7F00))
796
797 bool
798 xstormy16_legitimate_address_p (machine_mode mode ATTRIBUTE_UNUSED, rtx x,
799 bool strict, code_helper)
800 {
801 if (LEGITIMATE_ADDRESS_CONST_INT_P (x, 0))
802 return true;
803
804 if (GET_CODE (x) == PLUS
805 && LEGITIMATE_ADDRESS_INTEGER_P (XEXP (x, 1), 0))
806 {
807 x = XEXP (x, 0);
808 /* PR 31232: Do not allow INT+INT as an address. */
809 if (CONST_INT_P (x))
810 return false;
811 }
812
813 if ((GET_CODE (x) == PRE_MODIFY && CONST_INT_P (XEXP (XEXP (x, 1), 1)))
814 || GET_CODE (x) == POST_INC
815 || GET_CODE (x) == PRE_DEC)
816 x = XEXP (x, 0);
817
818 if (REG_P (x)
819 && REGNO_OK_FOR_BASE_P (REGNO (x))
820 && (! strict || REGNO (x) < FIRST_PSEUDO_REGISTER))
821 return true;
822
823 if (xstormy16_below100_symbol (x, mode))
824 return true;
825
826 return false;
827 }
828
829 /* Worker function for TARGET_MODE_DEPENDENT_ADDRESS_P.
830
831 On this chip, this is true if the address is valid with an offset
832 of 0 but not of 6, because in that case it cannot be used as an
833 address for DImode or DFmode, or if the address is a post-increment
834 or pre-decrement address. */
835
836 static bool
837 xstormy16_mode_dependent_address_p (const_rtx x,
838 addr_space_t as ATTRIBUTE_UNUSED)
839 {
840 if (LEGITIMATE_ADDRESS_CONST_INT_P (x, 0)
841 && ! LEGITIMATE_ADDRESS_CONST_INT_P (x, 6))
842 return true;
843
844 if (GET_CODE (x) == PLUS
845 && LEGITIMATE_ADDRESS_INTEGER_P (XEXP (x, 1), 0)
846 && ! LEGITIMATE_ADDRESS_INTEGER_P (XEXP (x, 1), 6))
847 return true;
848
849 /* Auto-increment addresses are now treated generically in recog.cc. */
850 return false;
851 }
852
853 int
854 short_memory_operand (rtx x, machine_mode mode)
855 {
856 if (! memory_operand (x, mode))
857 return 0;
858 return (GET_CODE (XEXP (x, 0)) != PLUS);
859 }
860
861 /* Splitter for the 'move' patterns, for modes not directly implemented
862 by hardware. Emit insns to copy a value of mode MODE from SRC to
863 DEST.
864
865 This function is only called when reload_completed. */
866
867 void
868 xstormy16_split_move (machine_mode mode, rtx dest, rtx src)
869 {
870 int num_words = GET_MODE_BITSIZE (mode) / BITS_PER_WORD;
871 int direction, end, i;
872 int src_modifies = 0;
873 int dest_modifies = 0;
874 int src_volatile = 0;
875 int dest_volatile = 0;
876 rtx mem_operand;
877 rtx auto_inc_reg_rtx = NULL_RTX;
878
879 /* Check initial conditions. */
880 gcc_assert (reload_completed
881 && mode != QImode && mode != HImode
882 && nonimmediate_operand (dest, mode)
883 && general_operand (src, mode));
884
885 /* This case is not supported below, and shouldn't be generated. */
886 gcc_assert (! MEM_P (dest) || ! MEM_P (src));
887
888 /* This case is very very bad after reload, so trap it now. */
889 gcc_assert (GET_CODE (dest) != SUBREG && GET_CODE (src) != SUBREG);
890
891 /* The general idea is to copy by words, offsetting the source and
892 destination. Normally the least-significant word will be copied
893 first, but for pre-dec operations it's better to copy the
894 most-significant word first. Only one operand can be a pre-dec
895 or post-inc operand.
896
897 It's also possible that the copy overlaps so that the direction
898 must be reversed. */
899 direction = 1;
900
901 if (MEM_P (dest))
902 {
903 mem_operand = XEXP (dest, 0);
904 dest_modifies = side_effects_p (mem_operand);
905 if (auto_inc_p (mem_operand))
906 auto_inc_reg_rtx = XEXP (mem_operand, 0);
907 dest_volatile = MEM_VOLATILE_P (dest);
908 if (dest_volatile)
909 {
910 dest = copy_rtx (dest);
911 MEM_VOLATILE_P (dest) = 0;
912 }
913 }
914 else if (MEM_P (src))
915 {
916 mem_operand = XEXP (src, 0);
917 src_modifies = side_effects_p (mem_operand);
918 if (auto_inc_p (mem_operand))
919 auto_inc_reg_rtx = XEXP (mem_operand, 0);
920 src_volatile = MEM_VOLATILE_P (src);
921 if (src_volatile)
922 {
923 src = copy_rtx (src);
924 MEM_VOLATILE_P (src) = 0;
925 }
926 }
927 else
928 mem_operand = NULL_RTX;
929
930 if (mem_operand == NULL_RTX)
931 {
932 if (REG_P (src)
933 && REG_P (dest)
934 && reg_overlap_mentioned_p (dest, src)
935 && REGNO (dest) > REGNO (src))
936 direction = -1;
937 }
938 else if (GET_CODE (mem_operand) == PRE_DEC
939 || (GET_CODE (mem_operand) == PLUS
940 && GET_CODE (XEXP (mem_operand, 0)) == PRE_DEC))
941 direction = -1;
942 else if (MEM_P (src) && reg_overlap_mentioned_p (dest, src))
943 {
944 int regno;
945
946 gcc_assert (REG_P (dest));
947 regno = REGNO (dest);
948
949 gcc_assert (refers_to_regno_p (regno, regno + num_words,
950 mem_operand, 0));
951
952 if (refers_to_regno_p (regno, mem_operand))
953 direction = -1;
954 else if (refers_to_regno_p (regno + num_words - 1, regno + num_words,
955 mem_operand, 0))
956 direction = 1;
957 else
958 /* This means something like
959 (set (reg:DI r0) (mem:DI (reg:HI r1)))
960 which we'd need to support by doing the set of the second word
961 last. */
962 gcc_unreachable ();
963 }
964
965 end = direction < 0 ? -1 : num_words;
966 for (i = direction < 0 ? num_words - 1 : 0; i != end; i += direction)
967 {
968 rtx w_src, w_dest, insn;
969
970 if (src_modifies)
971 w_src = gen_rtx_MEM (word_mode, mem_operand);
972 else
973 w_src = simplify_gen_subreg (word_mode, src, mode, i * UNITS_PER_WORD);
974 if (src_volatile)
975 MEM_VOLATILE_P (w_src) = 1;
976 if (dest_modifies)
977 w_dest = gen_rtx_MEM (word_mode, mem_operand);
978 else
979 w_dest = simplify_gen_subreg (word_mode, dest, mode,
980 i * UNITS_PER_WORD);
981 if (dest_volatile)
982 MEM_VOLATILE_P (w_dest) = 1;
983
984 /* The simplify_subreg calls must always be able to simplify. */
985 gcc_assert (GET_CODE (w_src) != SUBREG
986 && GET_CODE (w_dest) != SUBREG);
987
988 insn = emit_insn (gen_rtx_SET (w_dest, w_src));
989 if (auto_inc_reg_rtx)
990 REG_NOTES (insn) = alloc_EXPR_LIST (REG_INC,
991 auto_inc_reg_rtx,
992 REG_NOTES (insn));
993 }
994 }
995
996 /* Expander for the 'move' patterns. Emit insns to copy a value of
997 mode MODE from SRC to DEST. */
998
999 void
1000 xstormy16_expand_move (machine_mode mode, rtx dest, rtx src)
1001 {
1002 if (MEM_P (dest) && (GET_CODE (XEXP (dest, 0)) == PRE_MODIFY))
1003 {
1004 rtx pmv = XEXP (dest, 0);
1005 rtx dest_reg = XEXP (pmv, 0);
1006 rtx dest_mod = XEXP (pmv, 1);
1007 rtx set = gen_rtx_SET (dest_reg, dest_mod);
1008 rtx clobber = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (BImode, CARRY_REGNUM));
1009
1010 dest = gen_rtx_MEM (mode, dest_reg);
1011 emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, set, clobber)));
1012 }
1013 else if (MEM_P (src) && (GET_CODE (XEXP (src, 0)) == PRE_MODIFY))
1014 {
1015 rtx pmv = XEXP (src, 0);
1016 rtx src_reg = XEXP (pmv, 0);
1017 rtx src_mod = XEXP (pmv, 1);
1018 rtx set = gen_rtx_SET (src_reg, src_mod);
1019 rtx clobber = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (BImode, CARRY_REGNUM));
1020
1021 src = gen_rtx_MEM (mode, src_reg);
1022 emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, set, clobber)));
1023 }
1024
1025 /* There are only limited immediate-to-memory move instructions. */
1026 if (! reload_in_progress
1027 && ! reload_completed
1028 && MEM_P (dest)
1029 && (! CONST_INT_P (XEXP (dest, 0))
1030 || ! xstormy16_legitimate_address_p (mode, XEXP (dest, 0), 0))
1031 && ! xstormy16_below100_operand (dest, mode)
1032 && ! REG_P (src)
1033 && GET_CODE (src) != SUBREG)
1034 src = copy_to_mode_reg (mode, src);
1035
1036 /* Don't emit something we would immediately split. */
1037 if (reload_completed
1038 && mode != HImode && mode != QImode)
1039 {
1040 xstormy16_split_move (mode, dest, src);
1041 return;
1042 }
1043
1044 emit_insn (gen_rtx_SET (dest, src));
1045 }
1046
1047 /* Stack Layout:
1048
1049 The stack is laid out as follows:
1050
1051 SP->
1052 FP-> Local variables
1053 Register save area (up to 4 words)
1054 Argument register save area for stdarg (NUM_ARGUMENT_REGISTERS words)
1055
1056 AP-> Return address (two words)
1057 9th procedure parameter word
1058 10th procedure parameter word
1059 ...
1060 last procedure parameter word
1061
1062 The frame pointer location is tuned to make it most likely that all
1063 parameters and local variables can be accessed using a load-indexed
1064 instruction. */
1065
1066 /* A structure to describe the layout. */
1067 struct xstormy16_stack_layout
1068 {
1069 /* Size of the topmost three items on the stack. */
1070 int locals_size;
1071 int register_save_size;
1072 int stdarg_save_size;
1073 /* Sum of the above items. */
1074 int frame_size;
1075 /* Various offsets. */
1076 int first_local_minus_ap;
1077 int sp_minus_fp;
1078 int fp_minus_ap;
1079 };
1080
1081 /* Does REGNO need to be saved? */
1082 #define REG_NEEDS_SAVE(REGNUM, IFUN) \
1083 ((df_regs_ever_live_p (REGNUM) && !call_used_or_fixed_reg_p (REGNUM)) \
1084 || (IFUN && !fixed_regs[REGNUM] && call_used_or_fixed_reg_p (REGNUM) \
1085 && (REGNUM != CARRY_REGNUM) \
1086 && (df_regs_ever_live_p (REGNUM) || ! crtl->is_leaf)))
1087
1088 /* Compute the stack layout. */
1089
1090 struct xstormy16_stack_layout
1091 xstormy16_compute_stack_layout (void)
1092 {
1093 struct xstormy16_stack_layout layout;
1094 int regno;
1095 const int ifun = xstormy16_interrupt_function_p ();
1096
1097 layout.locals_size = get_frame_size ();
1098
1099 layout.register_save_size = 0;
1100 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1101 if (REG_NEEDS_SAVE (regno, ifun))
1102 layout.register_save_size += UNITS_PER_WORD;
1103
1104 if (cfun->stdarg)
1105 layout.stdarg_save_size = NUM_ARGUMENT_REGISTERS * UNITS_PER_WORD;
1106 else
1107 layout.stdarg_save_size = 0;
1108
1109 layout.frame_size = (layout.locals_size
1110 + layout.register_save_size
1111 + layout.stdarg_save_size);
1112
1113 if (crtl->args.size <= 2048 && crtl->args.size != -1)
1114 {
1115 if (layout.frame_size - INCOMING_FRAME_SP_OFFSET
1116 + crtl->args.size <= 2048)
1117 layout.fp_minus_ap = layout.frame_size - INCOMING_FRAME_SP_OFFSET;
1118 else
1119 layout.fp_minus_ap = 2048 - crtl->args.size;
1120 }
1121 else
1122 layout.fp_minus_ap = (layout.stdarg_save_size
1123 + layout.register_save_size
1124 - INCOMING_FRAME_SP_OFFSET);
1125 layout.sp_minus_fp = (layout.frame_size - INCOMING_FRAME_SP_OFFSET
1126 - layout.fp_minus_ap);
1127 layout.first_local_minus_ap = layout.sp_minus_fp - layout.locals_size;
1128 return layout;
1129 }
1130
1131 /* Worker function for TARGET_CAN_ELIMINATE. */
1132
1133 static bool
1134 xstormy16_can_eliminate (const int from, const int to)
1135 {
1136 return (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM
1137 ? ! frame_pointer_needed
1138 : true);
1139 }
1140
1141 /* Determine how all the special registers get eliminated. */
1142
1143 int
1144 xstormy16_initial_elimination_offset (int from, int to)
1145 {
1146 struct xstormy16_stack_layout layout;
1147 int result;
1148
1149 layout = xstormy16_compute_stack_layout ();
1150
1151 if (from == FRAME_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
1152 result = layout.sp_minus_fp - layout.locals_size;
1153 else if (from == FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
1154 result = - layout.locals_size;
1155 else if (from == ARG_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
1156 result = - layout.fp_minus_ap;
1157 else if (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
1158 result = - (layout.sp_minus_fp + layout.fp_minus_ap);
1159 else
1160 gcc_unreachable ();
1161
1162 return result;
1163 }
1164
1165 static rtx
1166 emit_addhi3_postreload (rtx dest, rtx src0, rtx src1)
1167 {
1168 rtx set, clobber, insn;
1169
1170 set = gen_rtx_SET (dest, gen_rtx_PLUS (HImode, src0, src1));
1171 clobber = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (BImode, CARRY_REGNUM));
1172 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, set, clobber)));
1173 return insn;
1174 }
1175
1176 /* Called after register allocation to add any instructions needed for
1177 the prologue. Using a prologue insn is favored compared to putting
1178 all of the instructions in the TARGET_ASM_FUNCTION_PROLOGUE macro,
1179 since it allows the scheduler to intermix instructions with the
1180 saves of the caller saved registers. In some cases, it might be
1181 necessary to emit a barrier instruction as the last insn to prevent
1182 such scheduling.
1183
1184 Also any insns generated here should have RTX_FRAME_RELATED_P(insn) = 1
1185 so that the debug info generation code can handle them properly. */
1186
1187 void
1188 xstormy16_expand_prologue (void)
1189 {
1190 struct xstormy16_stack_layout layout;
1191 int regno;
1192 rtx insn;
1193 rtx mem_push_rtx;
1194 const int ifun = xstormy16_interrupt_function_p ();
1195
1196 mem_push_rtx = gen_rtx_POST_INC (Pmode, stack_pointer_rtx);
1197 mem_push_rtx = gen_rtx_MEM (HImode, mem_push_rtx);
1198
1199 layout = xstormy16_compute_stack_layout ();
1200
1201 if (layout.locals_size >= 32768)
1202 error ("local variable memory requirements exceed capacity");
1203
1204 if (flag_stack_usage_info)
1205 current_function_static_stack_size = layout.frame_size;
1206
1207 /* Save the argument registers if necessary. */
1208 if (layout.stdarg_save_size)
1209 for (regno = FIRST_ARGUMENT_REGISTER;
1210 regno < FIRST_ARGUMENT_REGISTER + NUM_ARGUMENT_REGISTERS;
1211 regno++)
1212 {
1213 rtx dwarf;
1214 rtx reg = gen_rtx_REG (HImode, regno);
1215
1216 insn = emit_move_insn (mem_push_rtx, reg);
1217 RTX_FRAME_RELATED_P (insn) = 1;
1218
1219 dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (2));
1220
1221 XVECEXP (dwarf, 0, 0) = gen_rtx_SET (gen_rtx_MEM (Pmode, stack_pointer_rtx),
1222 reg);
1223 XVECEXP (dwarf, 0, 1) = gen_rtx_SET (stack_pointer_rtx,
1224 plus_constant (Pmode,
1225 stack_pointer_rtx,
1226 GET_MODE_SIZE (Pmode)));
1227 add_reg_note (insn, REG_FRAME_RELATED_EXPR, dwarf);
1228 RTX_FRAME_RELATED_P (XVECEXP (dwarf, 0, 0)) = 1;
1229 RTX_FRAME_RELATED_P (XVECEXP (dwarf, 0, 1)) = 1;
1230 }
1231
1232 /* Push each of the registers to save. */
1233 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1234 if (REG_NEEDS_SAVE (regno, ifun))
1235 {
1236 rtx dwarf;
1237 rtx reg = gen_rtx_REG (HImode, regno);
1238
1239 insn = emit_move_insn (mem_push_rtx, reg);
1240 RTX_FRAME_RELATED_P (insn) = 1;
1241
1242 dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (2));
1243
1244 XVECEXP (dwarf, 0, 0) = gen_rtx_SET (gen_rtx_MEM (Pmode, stack_pointer_rtx),
1245 reg);
1246 XVECEXP (dwarf, 0, 1) = gen_rtx_SET (stack_pointer_rtx,
1247 plus_constant (Pmode,
1248 stack_pointer_rtx,
1249 GET_MODE_SIZE (Pmode)));
1250 add_reg_note (insn, REG_FRAME_RELATED_EXPR, dwarf);
1251 RTX_FRAME_RELATED_P (XVECEXP (dwarf, 0, 0)) = 1;
1252 RTX_FRAME_RELATED_P (XVECEXP (dwarf, 0, 1)) = 1;
1253 }
1254
1255 /* It's just possible that the SP here might be what we need for
1256 the new FP... */
1257 if (frame_pointer_needed && layout.sp_minus_fp == layout.locals_size)
1258 {
1259 insn = emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
1260 RTX_FRAME_RELATED_P (insn) = 1;
1261 }
1262
1263 /* Allocate space for local variables. */
1264 if (layout.locals_size)
1265 {
1266 insn = emit_addhi3_postreload (stack_pointer_rtx, stack_pointer_rtx,
1267 GEN_INT (layout.locals_size));
1268 RTX_FRAME_RELATED_P (insn) = 1;
1269 }
1270
1271 /* Set up the frame pointer, if required. */
1272 if (frame_pointer_needed && layout.sp_minus_fp != layout.locals_size)
1273 {
1274 insn = emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
1275 RTX_FRAME_RELATED_P (insn) = 1;
1276
1277 if (layout.sp_minus_fp)
1278 {
1279 insn = emit_addhi3_postreload (hard_frame_pointer_rtx,
1280 hard_frame_pointer_rtx,
1281 GEN_INT (- layout.sp_minus_fp));
1282 RTX_FRAME_RELATED_P (insn) = 1;
1283 }
1284 }
1285 }
1286
1287 /* Do we need an epilogue at all? */
1288
1289 int
1290 direct_return (void)
1291 {
1292 return (reload_completed
1293 && xstormy16_compute_stack_layout ().frame_size == 0
1294 && ! xstormy16_interrupt_function_p ());
1295 }
1296
1297 /* Called after register allocation to add any instructions needed for
1298 the epilogue. Using an epilogue insn is favored compared to putting
1299 all of the instructions in the TARGET_ASM_FUNCTION_PROLOGUE macro,
1300 since it allows the scheduler to intermix instructions with the
1301 saves of the caller saved registers. In some cases, it might be
1302 necessary to emit a barrier instruction as the last insn to prevent
1303 such scheduling. */
1304
1305 void
1306 xstormy16_expand_epilogue (void)
1307 {
1308 struct xstormy16_stack_layout layout;
1309 rtx mem_pop_rtx;
1310 int regno;
1311 const int ifun = xstormy16_interrupt_function_p ();
1312
1313 mem_pop_rtx = gen_rtx_PRE_DEC (Pmode, stack_pointer_rtx);
1314 mem_pop_rtx = gen_rtx_MEM (HImode, mem_pop_rtx);
1315
1316 layout = xstormy16_compute_stack_layout ();
1317
1318 /* Pop the stack for the locals. */
1319 if (layout.locals_size)
1320 {
1321 if (frame_pointer_needed && layout.sp_minus_fp == layout.locals_size)
1322 emit_move_insn (stack_pointer_rtx, hard_frame_pointer_rtx);
1323 else
1324 emit_addhi3_postreload (stack_pointer_rtx, stack_pointer_rtx,
1325 GEN_INT (- layout.locals_size));
1326 }
1327
1328 /* Restore any call-saved registers. */
1329 for (regno = FIRST_PSEUDO_REGISTER - 1; regno >= 0; regno--)
1330 if (REG_NEEDS_SAVE (regno, ifun))
1331 emit_move_insn (gen_rtx_REG (HImode, regno), mem_pop_rtx);
1332
1333 /* Pop the stack for the stdarg save area. */
1334 if (layout.stdarg_save_size)
1335 emit_addhi3_postreload (stack_pointer_rtx, stack_pointer_rtx,
1336 GEN_INT (- layout.stdarg_save_size));
1337
1338 /* Return. */
1339 if (ifun)
1340 emit_jump_insn (gen_return_internal_interrupt ());
1341 else
1342 emit_jump_insn (gen_return_internal ());
1343 }
1344
1345 int
1346 xstormy16_epilogue_uses (int regno)
1347 {
1348 if (reload_completed && call_used_or_fixed_reg_p (regno))
1349 {
1350 const int ifun = xstormy16_interrupt_function_p ();
1351 return REG_NEEDS_SAVE (regno, ifun);
1352 }
1353 return 0;
1354 }
1355
1356 void
1357 xstormy16_function_profiler (void)
1358 {
1359 sorry ("%<function_profiler%> support");
1360 }
1361
1362 /* Update CUM to advance past argument ARG. Once this is done,
1363 the variable CUM is suitable for analyzing the *following*
1364 argument with `TARGET_FUNCTION_ARG', etc.
1365
1366 This function need not do anything if the argument in question was
1367 passed on the stack. The compiler knows how to track the amount of
1368 stack space used for arguments without any special help. However,
1369 it makes life easier for xstormy16_build_va_list if it does update
1370 the word count. */
1371
1372 static void
1373 xstormy16_function_arg_advance (cumulative_args_t cum_v,
1374 const function_arg_info &arg)
1375 {
1376 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
1377
1378 /* If an argument would otherwise be passed partially in registers,
1379 and partially on the stack, the whole of it is passed on the
1380 stack. */
1381 if (*cum < NUM_ARGUMENT_REGISTERS
1382 && (*cum + XSTORMY16_WORD_SIZE (arg.type, arg.mode)
1383 > NUM_ARGUMENT_REGISTERS))
1384 *cum = NUM_ARGUMENT_REGISTERS;
1385
1386 *cum += XSTORMY16_WORD_SIZE (arg.type, arg.mode);
1387 }
1388
1389 static rtx
1390 xstormy16_function_arg (cumulative_args_t cum_v, const function_arg_info &arg)
1391 {
1392 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
1393
1394 if (arg.end_marker_p ())
1395 return const0_rtx;
1396 if (targetm.calls.must_pass_in_stack (arg)
1397 || (*cum + XSTORMY16_WORD_SIZE (arg.type, arg.mode)
1398 > NUM_ARGUMENT_REGISTERS))
1399 return NULL_RTX;
1400 return gen_rtx_REG (arg.mode, *cum + FIRST_ARGUMENT_REGISTER);
1401 }
1402
1403 /* Build the va_list type.
1404
1405 For this chip, va_list is a record containing a counter and a pointer.
1406 The counter is of type 'int' and indicates how many bytes
1407 have been used to date. The pointer indicates the stack position
1408 for arguments that have not been passed in registers.
1409 To keep the layout nice, the pointer is first in the structure. */
1410
1411 static tree
1412 xstormy16_build_builtin_va_list (void)
1413 {
1414 tree f_1, f_2, record, type_decl;
1415
1416 record = (*lang_hooks.types.make_type) (RECORD_TYPE);
1417 type_decl = build_decl (BUILTINS_LOCATION,
1418 TYPE_DECL, get_identifier ("__va_list_tag"), record);
1419
1420 f_1 = build_decl (BUILTINS_LOCATION,
1421 FIELD_DECL, get_identifier ("base"),
1422 ptr_type_node);
1423 f_2 = build_decl (BUILTINS_LOCATION,
1424 FIELD_DECL, get_identifier ("count"),
1425 unsigned_type_node);
1426
1427 DECL_FIELD_CONTEXT (f_1) = record;
1428 DECL_FIELD_CONTEXT (f_2) = record;
1429
1430 TYPE_STUB_DECL (record) = type_decl;
1431 TYPE_NAME (record) = type_decl;
1432 TYPE_FIELDS (record) = f_1;
1433 DECL_CHAIN (f_1) = f_2;
1434
1435 layout_type (record);
1436
1437 return record;
1438 }
1439
1440 /* Implement the stdarg/varargs va_start macro. STDARG_P is nonzero if this
1441 is stdarg.h instead of varargs.h. VALIST is the tree of the va_list
1442 variable to initialize. NEXTARG is the machine independent notion of the
1443 'next' argument after the variable arguments. */
1444
1445 static void
1446 xstormy16_expand_builtin_va_start (tree valist, rtx nextarg ATTRIBUTE_UNUSED)
1447 {
1448 tree f_base, f_count;
1449 tree base, count;
1450 tree t,u;
1451
1452 if (xstormy16_interrupt_function_p ())
1453 error ("cannot use %<va_start%> in interrupt function");
1454
1455 f_base = TYPE_FIELDS (va_list_type_node);
1456 f_count = DECL_CHAIN (f_base);
1457
1458 base = build3 (COMPONENT_REF, TREE_TYPE (f_base), valist, f_base, NULL_TREE);
1459 count = build3 (COMPONENT_REF, TREE_TYPE (f_count), valist, f_count,
1460 NULL_TREE);
1461
1462 t = make_tree (TREE_TYPE (base), virtual_incoming_args_rtx);
1463 u = build_int_cst (NULL_TREE, - INCOMING_FRAME_SP_OFFSET);
1464 u = fold_convert (TREE_TYPE (count), u);
1465 t = fold_build_pointer_plus (t, u);
1466 t = build2 (MODIFY_EXPR, TREE_TYPE (base), base, t);
1467 TREE_SIDE_EFFECTS (t) = 1;
1468 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
1469
1470 t = build2 (MODIFY_EXPR, TREE_TYPE (count), count,
1471 build_int_cst (NULL_TREE,
1472 crtl->args.info * UNITS_PER_WORD));
1473 TREE_SIDE_EFFECTS (t) = 1;
1474 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
1475 }
1476
1477 /* Implement the stdarg/varargs va_arg macro. VALIST is the variable
1478 of type va_list as a tree, TYPE is the type passed to va_arg.
1479 Note: This algorithm is documented in stormy-abi. */
1480
1481 static tree
1482 xstormy16_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
1483 gimple_seq *post_p ATTRIBUTE_UNUSED)
1484 {
1485 tree f_base, f_count;
1486 tree base, count;
1487 tree count_tmp, addr, t;
1488 tree lab_gotaddr, lab_fromstack;
1489 int size, size_of_reg_args, must_stack;
1490 tree size_tree;
1491
1492 f_base = TYPE_FIELDS (va_list_type_node);
1493 f_count = DECL_CHAIN (f_base);
1494
1495 base = build3 (COMPONENT_REF, TREE_TYPE (f_base), valist, f_base, NULL_TREE);
1496 count = build3 (COMPONENT_REF, TREE_TYPE (f_count), valist, f_count,
1497 NULL_TREE);
1498
1499 must_stack = must_pass_va_arg_in_stack (type);
1500 size_tree = round_up (size_in_bytes (type), UNITS_PER_WORD);
1501 gimplify_expr (&size_tree, pre_p, NULL, is_gimple_val, fb_rvalue);
1502
1503 size_of_reg_args = NUM_ARGUMENT_REGISTERS * UNITS_PER_WORD;
1504
1505 count_tmp = get_initialized_tmp_var (count, pre_p, NULL);
1506 lab_gotaddr = create_artificial_label (UNKNOWN_LOCATION);
1507 lab_fromstack = create_artificial_label (UNKNOWN_LOCATION);
1508 addr = create_tmp_var (ptr_type_node);
1509
1510 if (!must_stack)
1511 {
1512 tree r;
1513
1514 t = fold_convert (TREE_TYPE (count), size_tree);
1515 t = build2 (PLUS_EXPR, TREE_TYPE (count), count_tmp, t);
1516 r = fold_convert (TREE_TYPE (count), size_int (size_of_reg_args));
1517 t = build2 (GT_EXPR, boolean_type_node, t, r);
1518 t = build3 (COND_EXPR, void_type_node, t,
1519 build1 (GOTO_EXPR, void_type_node, lab_fromstack),
1520 NULL_TREE);
1521 gimplify_and_add (t, pre_p);
1522
1523 t = fold_build_pointer_plus (base, count_tmp);
1524 gimplify_assign (addr, t, pre_p);
1525
1526 t = build1 (GOTO_EXPR, void_type_node, lab_gotaddr);
1527 gimplify_and_add (t, pre_p);
1528
1529 t = build1 (LABEL_EXPR, void_type_node, lab_fromstack);
1530 gimplify_and_add (t, pre_p);
1531 }
1532
1533 /* Arguments larger than a word might need to skip over some
1534 registers, since arguments are either passed entirely in
1535 registers or entirely on the stack. */
1536 size = PUSH_ROUNDING (int_size_in_bytes (type));
1537 if (size > 2 || size < 0 || must_stack)
1538 {
1539 tree r, u;
1540
1541 r = size_int (NUM_ARGUMENT_REGISTERS * UNITS_PER_WORD);
1542 u = build2 (MODIFY_EXPR, TREE_TYPE (count_tmp), count_tmp, r);
1543
1544 t = fold_convert (TREE_TYPE (count), r);
1545 t = build2 (GE_EXPR, boolean_type_node, count_tmp, t);
1546 t = build3 (COND_EXPR, void_type_node, t, NULL_TREE, u);
1547 gimplify_and_add (t, pre_p);
1548 }
1549
1550 t = size_int (NUM_ARGUMENT_REGISTERS * UNITS_PER_WORD
1551 + INCOMING_FRAME_SP_OFFSET);
1552 t = fold_convert (TREE_TYPE (count), t);
1553 t = build2 (MINUS_EXPR, TREE_TYPE (count), count_tmp, t);
1554 t = build2 (PLUS_EXPR, TREE_TYPE (count), t,
1555 fold_convert (TREE_TYPE (count), size_tree));
1556 t = fold_convert (TREE_TYPE (t), fold (t));
1557 t = fold_build1 (NEGATE_EXPR, TREE_TYPE (t), t);
1558 t = fold_build_pointer_plus (base, t);
1559 gimplify_assign (addr, t, pre_p);
1560
1561 t = build1 (LABEL_EXPR, void_type_node, lab_gotaddr);
1562 gimplify_and_add (t, pre_p);
1563
1564 t = fold_convert (TREE_TYPE (count), size_tree);
1565 t = build2 (PLUS_EXPR, TREE_TYPE (count), count_tmp, t);
1566 gimplify_assign (count, t, pre_p);
1567
1568 addr = fold_convert (build_pointer_type (type), addr);
1569 return build_va_arg_indirect_ref (addr);
1570 }
1571
1572 /* Worker function for TARGET_TRAMPOLINE_INIT. */
1573
1574 static void
1575 xstormy16_trampoline_init (rtx m_tramp, tree fndecl, rtx static_chain)
1576 {
1577 rtx temp = gen_reg_rtx (HImode);
1578 rtx reg_fnaddr = gen_reg_rtx (HImode);
1579 rtx reg_addr, reg_addr_mem;
1580
1581 reg_addr = copy_to_reg (XEXP (m_tramp, 0));
1582 reg_addr_mem = adjust_automodify_address (m_tramp, HImode, reg_addr, 0);
1583
1584 emit_move_insn (temp, GEN_INT (0x3130 | STATIC_CHAIN_REGNUM));
1585 emit_move_insn (reg_addr_mem, temp);
1586 emit_insn (gen_addhi3 (reg_addr, reg_addr, const2_rtx));
1587 reg_addr_mem = adjust_automodify_address (reg_addr_mem, VOIDmode, NULL, 2);
1588
1589 emit_move_insn (temp, static_chain);
1590 emit_move_insn (reg_addr_mem, temp);
1591 emit_insn (gen_addhi3 (reg_addr, reg_addr, const2_rtx));
1592 reg_addr_mem = adjust_automodify_address (reg_addr_mem, VOIDmode, NULL, 2);
1593
1594 emit_move_insn (reg_fnaddr, XEXP (DECL_RTL (fndecl), 0));
1595 emit_move_insn (temp, reg_fnaddr);
1596 emit_insn (gen_andhi3 (temp, temp, GEN_INT (0xFF)));
1597 emit_insn (gen_iorhi3 (temp, temp, GEN_INT (0x0200)));
1598 emit_move_insn (reg_addr_mem, temp);
1599 emit_insn (gen_addhi3 (reg_addr, reg_addr, const2_rtx));
1600 reg_addr_mem = adjust_automodify_address (reg_addr_mem, VOIDmode, NULL, 2);
1601
1602 emit_insn (gen_lshrhi3 (reg_fnaddr, reg_fnaddr, GEN_INT (8)));
1603 emit_move_insn (reg_addr_mem, reg_fnaddr);
1604 }
1605
1606 /* Worker function for TARGET_FUNCTION_VALUE. */
1607
1608 static rtx
1609 xstormy16_function_value (const_tree valtype,
1610 const_tree func ATTRIBUTE_UNUSED,
1611 bool outgoing ATTRIBUTE_UNUSED)
1612 {
1613 machine_mode mode;
1614 mode = TYPE_MODE (valtype);
1615 PROMOTE_MODE (mode, 0, valtype);
1616 return gen_rtx_REG (mode, RETURN_VALUE_REGNUM);
1617 }
1618
1619 /* Worker function for TARGET_LIBCALL_VALUE. */
1620
1621 static rtx
1622 xstormy16_libcall_value (machine_mode mode,
1623 const_rtx fun ATTRIBUTE_UNUSED)
1624 {
1625 return gen_rtx_REG (mode, RETURN_VALUE_REGNUM);
1626 }
1627
1628 /* Worker function for TARGET_FUNCTION_VALUE_REGNO_P. */
1629
1630 static bool
1631 xstormy16_function_value_regno_p (const unsigned int regno)
1632 {
1633 return (regno == RETURN_VALUE_REGNUM);
1634 }
1635
1636 /* A C compound statement that outputs the assembler code for a thunk function,
1637 used to implement C++ virtual function calls with multiple inheritance. The
1638 thunk acts as a wrapper around a virtual function, adjusting the implicit
1639 object parameter before handing control off to the real function.
1640
1641 First, emit code to add the integer DELTA to the location that contains the
1642 incoming first argument. Assume that this argument contains a pointer, and
1643 is the one used to pass the `this' pointer in C++. This is the incoming
1644 argument *before* the function prologue, e.g. `%o0' on a sparc. The
1645 addition must preserve the values of all other incoming arguments.
1646
1647 After the addition, emit code to jump to FUNCTION, which is a
1648 `FUNCTION_DECL'. This is a direct pure jump, not a call, and does not touch
1649 the return address. Hence returning from FUNCTION will return to whoever
1650 called the current `thunk'.
1651
1652 The effect must be as if @var{function} had been called directly
1653 with the adjusted first argument. This macro is responsible for
1654 emitting all of the code for a thunk function;
1655 TARGET_ASM_FUNCTION_PROLOGUE and TARGET_ASM_FUNCTION_EPILOGUE are
1656 not invoked.
1657
1658 The THUNK_FNDECL is redundant. (DELTA and FUNCTION have already been
1659 extracted from it.) It might possibly be useful on some targets, but
1660 probably not. */
1661
1662 static void
1663 xstormy16_asm_output_mi_thunk (FILE *file,
1664 tree thunk_fndecl ATTRIBUTE_UNUSED,
1665 HOST_WIDE_INT delta,
1666 HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
1667 tree function)
1668 {
1669 const char *fnname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk_fndecl));
1670 int regnum = FIRST_ARGUMENT_REGISTER;
1671
1672 assemble_start_function (thunk_fndecl, fnname);
1673 /* There might be a hidden first argument for a returned structure. */
1674 if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
1675 regnum += 1;
1676
1677 fprintf (file, "\tadd %s,#0x%x\n", reg_names[regnum], (int) delta & 0xFFFF);
1678 fputs ("\tjmpf ", file);
1679 assemble_name (file, XSTR (XEXP (DECL_RTL (function), 0), 0));
1680 putc ('\n', file);
1681 assemble_end_function (thunk_fndecl, fnname);
1682 }
1683
1684 /* The purpose of this function is to override the default behavior of
1685 BSS objects. Normally, they go into .bss or .sbss via ".common"
1686 directives, but we need to override that and put them in
1687 .bss_below100. We can't just use a section override (like we do
1688 for .data_below100), because that makes them initialized rather
1689 than uninitialized. */
1690
1691 void
1692 xstormy16_asm_output_aligned_common (FILE *stream,
1693 tree decl,
1694 const char *name,
1695 int size,
1696 int align,
1697 int global)
1698 {
1699 rtx mem = decl == NULL_TREE ? NULL_RTX : DECL_RTL (decl);
1700 rtx symbol;
1701
1702 if (mem != NULL_RTX
1703 && MEM_P (mem)
1704 && GET_CODE (symbol = XEXP (mem, 0)) == SYMBOL_REF
1705 && SYMBOL_REF_FLAGS (symbol) & SYMBOL_FLAG_XSTORMY16_BELOW100)
1706 {
1707 const char *name2;
1708 int p2align = 0;
1709
1710 switch_to_section (bss100_section);
1711
1712 while (align > 8)
1713 {
1714 align /= 2;
1715 p2align ++;
1716 }
1717
1718 name2 = default_strip_name_encoding (name);
1719 if (global)
1720 fprintf (stream, "\t.globl\t%s\n", name2);
1721 if (p2align)
1722 fprintf (stream, "\t.p2align %d\n", p2align);
1723 fprintf (stream, "\t.type\t%s, @object\n", name2);
1724 fprintf (stream, "\t.size\t%s, %d\n", name2, size);
1725 fprintf (stream, "%s:\n\t.space\t%d\n", name2, size);
1726 return;
1727 }
1728
1729 if (!global)
1730 {
1731 fprintf (stream, "\t.local\t");
1732 assemble_name (stream, name);
1733 fprintf (stream, "\n");
1734 }
1735 fprintf (stream, "\t.comm\t");
1736 assemble_name (stream, name);
1737 fprintf (stream, ",%u,%u\n", size, align / BITS_PER_UNIT);
1738 }
1739
1740 /* Implement TARGET_ASM_INIT_SECTIONS. */
1741
1742 static void
1743 xstormy16_asm_init_sections (void)
1744 {
1745 bss100_section
1746 = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
1747 output_section_asm_op,
1748 "\t.section \".bss_below100\",\"aw\",@nobits");
1749 }
1750
1751 /* Mark symbols with the "below100" attribute so that we can use the
1752 special addressing modes for them. */
1753
1754 static void
1755 xstormy16_encode_section_info (tree decl, rtx r, int first)
1756 {
1757 default_encode_section_info (decl, r, first);
1758
1759 if (VAR_P (decl)
1760 && (lookup_attribute ("below100", DECL_ATTRIBUTES (decl))
1761 || lookup_attribute ("BELOW100", DECL_ATTRIBUTES (decl))))
1762 {
1763 rtx symbol = XEXP (r, 0);
1764
1765 gcc_assert (GET_CODE (symbol) == SYMBOL_REF);
1766 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_XSTORMY16_BELOW100;
1767 }
1768 }
1769
1770 #undef TARGET_ASM_CONSTRUCTOR
1771 #define TARGET_ASM_CONSTRUCTOR xstormy16_asm_out_constructor
1772 #undef TARGET_ASM_DESTRUCTOR
1773 #define TARGET_ASM_DESTRUCTOR xstormy16_asm_out_destructor
1774
1775 /* Output constructors and destructors. Just like
1776 default_named_section_asm_out_* but don't set the sections writable. */
1777
1778 static void
1779 xstormy16_asm_out_destructor (rtx symbol, int priority)
1780 {
1781 const char *section = ".dtors";
1782 char buf[18];
1783
1784 /* ??? This only works reliably with the GNU linker. */
1785 if (priority != DEFAULT_INIT_PRIORITY)
1786 {
1787 sprintf (buf, ".dtors.%.5u",
1788 /* Invert the numbering so the linker puts us in the proper
1789 order; constructors are run from right to left, and the
1790 linker sorts in increasing order. */
1791 MAX_INIT_PRIORITY - priority);
1792 section = buf;
1793 }
1794
1795 switch_to_section (get_section (section, 0, NULL));
1796 assemble_align (POINTER_SIZE);
1797 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1798 }
1799
1800 static void
1801 xstormy16_asm_out_constructor (rtx symbol, int priority)
1802 {
1803 const char *section = ".ctors";
1804 char buf[18];
1805
1806 /* ??? This only works reliably with the GNU linker. */
1807 if (priority != DEFAULT_INIT_PRIORITY)
1808 {
1809 sprintf (buf, ".ctors.%.5u",
1810 /* Invert the numbering so the linker puts us in the proper
1811 order; constructors are run from right to left, and the
1812 linker sorts in increasing order. */
1813 MAX_INIT_PRIORITY - priority);
1814 section = buf;
1815 }
1816
1817 switch_to_section (get_section (section, 0, NULL));
1818 assemble_align (POINTER_SIZE);
1819 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1820 }
1821
1822 /* Worker function for TARGET_PRINT_OPERAND_ADDRESS.
1823
1824 Print a memory address as an operand to reference that memory location. */
1825
1826 static void
1827 xstormy16_print_operand_address (FILE *file, machine_mode /*mode*/,
1828 rtx address)
1829 {
1830 HOST_WIDE_INT offset;
1831 int pre_dec, post_inc;
1832
1833 /* There are a few easy cases. */
1834 if (CONST_INT_P (address))
1835 {
1836 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (address) & 0xFFFF);
1837 return;
1838 }
1839
1840 if (CONSTANT_P (address) || LABEL_P (address))
1841 {
1842 output_addr_const (file, address);
1843 return;
1844 }
1845
1846 /* Otherwise, it's hopefully something of the form
1847 (plus:HI (pre_dec:HI (reg:HI ...)) (const_int ...)). */
1848 if (GET_CODE (address) == PLUS)
1849 {
1850 gcc_assert (CONST_INT_P (XEXP (address, 1)));
1851 offset = INTVAL (XEXP (address, 1));
1852 address = XEXP (address, 0);
1853 }
1854 else
1855 offset = 0;
1856
1857 pre_dec = (GET_CODE (address) == PRE_DEC);
1858 post_inc = (GET_CODE (address) == POST_INC);
1859 if (pre_dec || post_inc)
1860 address = XEXP (address, 0);
1861
1862 gcc_assert (REG_P (address));
1863
1864 fputc ('(', file);
1865 if (pre_dec)
1866 fputs ("--", file);
1867 fputs (reg_names [REGNO (address)], file);
1868 if (post_inc)
1869 fputs ("++", file);
1870 if (offset != 0)
1871 fprintf (file, "," HOST_WIDE_INT_PRINT_DEC, offset);
1872 fputc (')', file);
1873 }
1874
1875 /* Worker function for TARGET_PRINT_OPERAND.
1876
1877 Print an operand to an assembler instruction. */
1878
1879 static void
1880 xstormy16_print_operand (FILE *file, rtx x, int code)
1881 {
1882 switch (code)
1883 {
1884 case 'B':
1885 /* There is either one bit set, or one bit clear, in X.
1886 Print it preceded by '#'. */
1887 {
1888 static int bits_set[8] = { 0, 1, 1, 2, 1, 2, 2, 3 };
1889 HOST_WIDE_INT xx = 1;
1890 HOST_WIDE_INT l;
1891
1892 if (CONST_INT_P (x))
1893 xx = INTVAL (x);
1894 else
1895 output_operand_lossage ("'B' operand is not constant");
1896
1897 /* GCC sign-extends masks with the MSB set, so we have to
1898 detect all the cases that differ only in sign extension
1899 beyond the bits we care about. Normally, the predicates
1900 and constraints ensure that we have the right values. This
1901 works correctly for valid masks. */
1902 if (bits_set[xx & 7] <= 1)
1903 {
1904 /* Remove sign extension bits. */
1905 if ((~xx & ~(HOST_WIDE_INT)0xff) == 0)
1906 xx &= 0xff;
1907 else if ((~xx & ~(HOST_WIDE_INT)0xffff) == 0)
1908 xx &= 0xffff;
1909 l = exact_log2 (xx);
1910 }
1911 else
1912 {
1913 /* Add sign extension bits. */
1914 if ((xx & ~(HOST_WIDE_INT)0xff) == 0)
1915 xx |= ~(HOST_WIDE_INT)0xff;
1916 else if ((xx & ~(HOST_WIDE_INT)0xffff) == 0)
1917 xx |= ~(HOST_WIDE_INT)0xffff;
1918 l = exact_log2 (~xx);
1919 }
1920
1921 if (l == -1)
1922 output_operand_lossage ("'B' operand has multiple bits set");
1923
1924 fprintf (file, IMMEDIATE_PREFIX HOST_WIDE_INT_PRINT_DEC, l);
1925 return;
1926 }
1927
1928 case 'C':
1929 /* Print the symbol without a surrounding @fptr(). */
1930 if (GET_CODE (x) == SYMBOL_REF)
1931 assemble_name (file, XSTR (x, 0));
1932 else if (LABEL_P (x))
1933 output_asm_label (x);
1934 else
1935 xstormy16_print_operand_address (file, VOIDmode, x);
1936 return;
1937
1938 case 'o':
1939 case 'O':
1940 /* Print the immediate operand less one, preceded by '#'.
1941 For 'O', negate it first. */
1942 {
1943 HOST_WIDE_INT xx = 0;
1944
1945 if (CONST_INT_P (x))
1946 xx = INTVAL (x);
1947 else
1948 output_operand_lossage ("'o' operand is not constant");
1949
1950 if (code == 'O')
1951 xx = -xx;
1952
1953 fprintf (file, IMMEDIATE_PREFIX HOST_WIDE_INT_PRINT_DEC, xx - 1);
1954 return;
1955 }
1956
1957 case 'b':
1958 /* Print the shift mask for bp/bn. */
1959 {
1960 HOST_WIDE_INT xx = 1;
1961 HOST_WIDE_INT l;
1962
1963 if (CONST_INT_P (x))
1964 xx = INTVAL (x);
1965 else
1966 output_operand_lossage ("'B' operand is not constant");
1967
1968 l = 7 - xx;
1969
1970 fputs (IMMEDIATE_PREFIX, file);
1971 fprintf (file, HOST_WIDE_INT_PRINT_DEC, l);
1972 return;
1973 }
1974
1975 case 'h':
1976 /* Print the highpart register of an SI mode register pair. */
1977 if (REG_P (x) && GET_MODE (x) == SImode)
1978 fputs (reg_names [REGNO (x) + 1], file);
1979 else
1980 output_operand_lossage ("'h' operand is not SImode register");
1981 return;
1982
1983 case 0:
1984 /* Handled below. */
1985 break;
1986
1987 default:
1988 output_operand_lossage ("xstormy16_print_operand: unknown code");
1989 return;
1990 }
1991
1992 switch (GET_CODE (x))
1993 {
1994 case REG:
1995 fputs (reg_names [REGNO (x)], file);
1996 break;
1997
1998 case MEM:
1999 xstormy16_print_operand_address (file, GET_MODE (x), XEXP (x, 0));
2000 break;
2001
2002 default:
2003 /* Some kind of constant or label; an immediate operand,
2004 so prefix it with '#' for the assembler. */
2005 fputs (IMMEDIATE_PREFIX, file);
2006 output_addr_const (file, x);
2007 break;
2008 }
2009
2010 return;
2011 }
2012
2013 /* Expander for the `casesi' pattern.
2014 INDEX is the index of the switch statement.
2015 LOWER_BOUND is a CONST_INT that is the value of INDEX corresponding
2016 to the first table entry.
2017 RANGE is the number of table entries.
2018 TABLE is an ADDR_VEC that is the jump table.
2019 DEFAULT_LABEL is the address to branch to if INDEX is outside the
2020 range LOWER_BOUND to LOWER_BOUND + RANGE - 1. */
2021
2022 void
2023 xstormy16_expand_casesi (rtx index, rtx lower_bound, rtx range,
2024 rtx table, rtx default_label)
2025 {
2026 HOST_WIDE_INT range_i = INTVAL (range);
2027 rtx int_index;
2028
2029 /* This code uses 'br', so it can deal only with tables of size up to
2030 8192 entries. */
2031 if (range_i >= 8192)
2032 sorry ("switch statement of size %lu entries too large",
2033 (unsigned long) range_i);
2034
2035 index = expand_binop (SImode, sub_optab, index, lower_bound, NULL_RTX, 0,
2036 OPTAB_LIB_WIDEN);
2037 emit_cmp_and_jump_insns (index, range, GTU, NULL_RTX, SImode, 1,
2038 default_label);
2039 int_index = gen_lowpart_common (HImode, index);
2040 emit_insn (gen_ashlhi3 (int_index, int_index, const2_rtx));
2041 emit_jump_insn (gen_tablejump_pcrel (int_index, table));
2042 }
2043
2044 /* Output an ADDR_VEC. It is output as a sequence of 'jmpf'
2045 instructions, without label or alignment or any other special
2046 constructs. We know that the previous instruction will be the
2047 `tablejump_pcrel' output above.
2048
2049 TODO: it might be nice to output 'br' instructions if they could
2050 all reach. */
2051
2052 void
2053 xstormy16_output_addr_vec (FILE *file, rtx label ATTRIBUTE_UNUSED, rtx table)
2054 {
2055 int vlen, idx;
2056
2057 switch_to_section (current_function_section ());
2058
2059 vlen = XVECLEN (table, 0);
2060 for (idx = 0; idx < vlen; idx++)
2061 {
2062 fputs ("\tjmpf ", file);
2063 output_asm_label (XEXP (XVECEXP (table, 0, idx), 0));
2064 fputc ('\n', file);
2065 }
2066 }
2067
2068 /* Expander for the `call' patterns.
2069 RETVAL is the RTL for the return register or NULL for void functions.
2070 DEST is the function to call, expressed as a MEM.
2071 COUNTER is ignored. */
2072
2073 void
2074 xstormy16_expand_call (rtx retval, rtx dest, rtx counter)
2075 {
2076 rtx call, temp;
2077 machine_mode mode;
2078
2079 gcc_assert (MEM_P (dest));
2080 dest = XEXP (dest, 0);
2081
2082 if (! CONSTANT_P (dest) && ! REG_P (dest))
2083 dest = force_reg (Pmode, dest);
2084
2085 if (retval == NULL)
2086 mode = VOIDmode;
2087 else
2088 mode = GET_MODE (retval);
2089
2090 call = gen_rtx_CALL (mode, gen_rtx_MEM (FUNCTION_MODE, dest),
2091 counter);
2092 if (retval)
2093 call = gen_rtx_SET (retval, call);
2094
2095 if (! CONSTANT_P (dest))
2096 {
2097 temp = gen_reg_rtx (HImode);
2098 emit_move_insn (temp, const0_rtx);
2099 }
2100 else
2101 temp = const0_rtx;
2102
2103 call = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, call,
2104 gen_rtx_USE (VOIDmode, temp)));
2105 emit_call_insn (call);
2106 }
2107
2108 /* Expanders for multiword computational operations. */
2109
2110 /* Expander for arithmetic operations; emit insns to compute
2111
2112 (set DEST (CODE:MODE SRC0 SRC1))
2113
2114 When CODE is COMPARE, a branch template is generated
2115 (this saves duplicating code in xstormy16_split_cbranch). */
2116
2117 void
2118 xstormy16_expand_arith (machine_mode mode, enum rtx_code code,
2119 rtx dest, rtx src0, rtx src1)
2120 {
2121 int num_words = GET_MODE_BITSIZE (mode) / BITS_PER_WORD;
2122 int i;
2123 int firstloop = 1;
2124
2125 if (code == NEG)
2126 emit_move_insn (src0, const0_rtx);
2127
2128 for (i = 0; i < num_words; i++)
2129 {
2130 rtx w_src0, w_src1, w_dest;
2131 rtx insn;
2132
2133 w_src0 = simplify_gen_subreg (word_mode, src0, mode,
2134 i * UNITS_PER_WORD);
2135 w_src1 = simplify_gen_subreg (word_mode, src1, mode, i * UNITS_PER_WORD);
2136 w_dest = simplify_gen_subreg (word_mode, dest, mode, i * UNITS_PER_WORD);
2137
2138 switch (code)
2139 {
2140 case PLUS:
2141 if (firstloop
2142 && CONST_INT_P (w_src1)
2143 && INTVAL (w_src1) == 0)
2144 continue;
2145
2146 if (firstloop)
2147 insn = gen_addchi4 (w_dest, w_src0, w_src1);
2148 else
2149 insn = gen_addchi5 (w_dest, w_src0, w_src1);
2150 break;
2151
2152 case NEG:
2153 case MINUS:
2154 case COMPARE:
2155 if (code == COMPARE && i == num_words - 1)
2156 {
2157 rtx branch, sub, clobber, sub_1;
2158
2159 sub_1 = gen_rtx_MINUS (HImode, w_src0,
2160 gen_rtx_ZERO_EXTEND (HImode, gen_rtx_REG (BImode, CARRY_REGNUM)));
2161 sub = gen_rtx_SET (w_dest,
2162 gen_rtx_MINUS (HImode, sub_1, w_src1));
2163 clobber = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (BImode, CARRY_REGNUM));
2164 branch = gen_rtx_SET (pc_rtx,
2165 gen_rtx_IF_THEN_ELSE (VOIDmode,
2166 gen_rtx_EQ (HImode,
2167 sub_1,
2168 w_src1),
2169 pc_rtx,
2170 pc_rtx));
2171 insn = gen_rtx_PARALLEL (VOIDmode,
2172 gen_rtvec (3, branch, sub, clobber));
2173 }
2174 else if (firstloop
2175 && code != COMPARE
2176 && CONST_INT_P (w_src1)
2177 && INTVAL (w_src1) == 0)
2178 continue;
2179 else if (firstloop)
2180 insn = gen_subchi4 (w_dest, w_src0, w_src1);
2181 else
2182 insn = gen_subchi5 (w_dest, w_src0, w_src1);
2183 break;
2184
2185 case IOR:
2186 case XOR:
2187 case AND:
2188 if (CONST_INT_P (w_src1)
2189 && INTVAL (w_src1) == -(code == AND))
2190 continue;
2191
2192 insn = gen_rtx_SET (w_dest, gen_rtx_fmt_ee (code, mode,
2193 w_src0, w_src1));
2194 break;
2195
2196 case NOT:
2197 insn = gen_rtx_SET (w_dest, gen_rtx_NOT (mode, w_src0));
2198 break;
2199
2200 default:
2201 gcc_unreachable ();
2202 }
2203
2204 firstloop = 0;
2205 emit (insn);
2206 }
2207
2208 /* If we emit nothing, try_split() will think we failed. So emit
2209 something that does nothing and can be optimized away. */
2210 if (firstloop)
2211 emit (gen_nop ());
2212 }
2213
2214 /* The shift operations are split at output time for constant values;
2215 variable-width shifts get handed off to a library routine.
2216
2217 Generate an output string to do (set X (CODE:MODE X SIZE_R))
2218 SIZE_R will be a CONST_INT, X will be a hard register. */
2219
2220 const char *
2221 xstormy16_output_shift (machine_mode mode, enum rtx_code code,
2222 rtx x, rtx size_r, rtx temp)
2223 {
2224 HOST_WIDE_INT size;
2225 const char *r0, *r1, *rt;
2226 static char r[64];
2227
2228 gcc_assert (CONST_INT_P (size_r)
2229 && REG_P (x)
2230 && mode == SImode);
2231
2232 size = INTVAL (size_r) & (GET_MODE_BITSIZE (mode) - 1);
2233
2234 if (size == 0)
2235 return "";
2236
2237 r0 = reg_names [REGNO (x)];
2238 r1 = reg_names [REGNO (x) + 1];
2239
2240 /* For shifts of size 1, we can use the rotate instructions. */
2241 if (size == 1)
2242 {
2243 switch (code)
2244 {
2245 case ASHIFT:
2246 sprintf (r, "shl %s,#1 | rlc %s,#1", r0, r1);
2247 break;
2248 case ASHIFTRT:
2249 sprintf (r, "asr %s,#1 | rrc %s,#1", r1, r0);
2250 break;
2251 case LSHIFTRT:
2252 sprintf (r, "shr %s,#1 | rrc %s,#1", r1, r0);
2253 break;
2254 default:
2255 gcc_unreachable ();
2256 }
2257 return r;
2258 }
2259
2260 /* For shifts of size 2, we can use two shifts of size 1. */
2261 if (size == 2)
2262 {
2263 switch (code)
2264 {
2265 case ASHIFT:
2266 sprintf (r, "shl %s,#1 | rlc %s,#1 | shl %s,#1 | rlc %s,#1",
2267 r0, r1, r0, r1);
2268 break;
2269 case ASHIFTRT:
2270 sprintf (r, "asr %s,#1 | rrc %s,#1 | asr %s,#1 | rrc %s,#1",
2271 r1, r0, r1, r0);
2272 break;
2273 case LSHIFTRT:
2274 sprintf (r, "shr %s,#1 | rrc %s,#1 | shr %s,#1 | rrc %s,#1",
2275 r1, r0, r1, r0);
2276 break;
2277 default:
2278 gcc_unreachable ();
2279 }
2280 return r;
2281 }
2282
2283 /* For large shifts, there are easy special cases. */
2284 if (size == 16)
2285 {
2286 switch (code)
2287 {
2288 case ASHIFT:
2289 sprintf (r, "mov %s,%s | mov %s,#0", r1, r0, r0);
2290 break;
2291 case ASHIFTRT:
2292 sprintf (r, "mov %s,%s | asr %s,#15", r0, r1, r1);
2293 break;
2294 case LSHIFTRT:
2295 sprintf (r, "mov %s,%s | mov %s,#0", r0, r1, r1);
2296 break;
2297 default:
2298 gcc_unreachable ();
2299 }
2300 return r;
2301 }
2302 if (size > 16)
2303 {
2304 switch (code)
2305 {
2306 case ASHIFT:
2307 sprintf (r, "mov %s,%s | mov %s,#0 | shl %s,#%d",
2308 r1, r0, r0, r1, (int) size - 16);
2309 break;
2310 case ASHIFTRT:
2311 sprintf (r, "mov %s,%s | asr %s,#15 | asr %s,#%d",
2312 r0, r1, r1, r0, (int) size - 16);
2313 break;
2314 case LSHIFTRT:
2315 sprintf (r, "mov %s,%s | mov %s,#0 | shr %s,#%d",
2316 r0, r1, r1, r0, (int) size - 16);
2317 break;
2318 default:
2319 gcc_unreachable ();
2320 }
2321 return r;
2322 }
2323
2324 /* For the rest, we have to do more work. In particular, we
2325 need a temporary. */
2326 rt = reg_names [REGNO (temp)];
2327 switch (code)
2328 {
2329 case ASHIFT:
2330 sprintf (r,
2331 "mov %s,%s | shl %s,#%d | shl %s,#%d | shr %s,#%d | or %s,%s",
2332 rt, r0, r0, (int) size, r1, (int) size, rt, (int) (16 - size),
2333 r1, rt);
2334 break;
2335 case ASHIFTRT:
2336 sprintf (r,
2337 "mov %s,%s | asr %s,#%d | shr %s,#%d | shl %s,#%d | or %s,%s",
2338 rt, r1, r1, (int) size, r0, (int) size, rt, (int) (16 - size),
2339 r0, rt);
2340 break;
2341 case LSHIFTRT:
2342 sprintf (r,
2343 "mov %s,%s | shr %s,#%d | shr %s,#%d | shl %s,#%d | or %s,%s",
2344 rt, r1, r1, (int) size, r0, (int) size, rt, (int) (16 - size),
2345 r0, rt);
2346 break;
2347 default:
2348 gcc_unreachable ();
2349 }
2350 return r;
2351 }
2352
2353 /* Attribute handling. */
2354
2355 /* Return nonzero if the function is an interrupt function. */
2356
2357 int
2358 xstormy16_interrupt_function_p (void)
2359 {
2360 tree attributes;
2361
2362 /* The dwarf2 mechanism asks for INCOMING_FRAME_SP_OFFSET before
2363 any functions are declared, which is demonstrably wrong, but
2364 it is worked around here. FIXME. */
2365 if (!cfun)
2366 return 0;
2367
2368 attributes = TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl));
2369 return lookup_attribute ("interrupt", attributes) != NULL_TREE;
2370 }
2371
2372 #undef TARGET_ATTRIBUTE_TABLE
2373 #define TARGET_ATTRIBUTE_TABLE xstormy16_attribute_table
2374
2375 static tree xstormy16_handle_interrupt_attribute
2376 (tree *, tree, tree, int, bool *);
2377 static tree xstormy16_handle_below100_attribute
2378 (tree *, tree, tree, int, bool *);
2379
2380 TARGET_GNU_ATTRIBUTES (xstormy16_attribute_table,
2381 {
2382 /* name, min_len, max_len, decl_req, type_req, fn_type_req,
2383 affects_type_identity, handler, exclude. */
2384 { "interrupt", 0, 0, false, true, true, false,
2385 xstormy16_handle_interrupt_attribute, NULL },
2386 { "BELOW100", 0, 0, false, false, false, false,
2387 xstormy16_handle_below100_attribute, NULL },
2388 { "below100", 0, 0, false, false, false, false,
2389 xstormy16_handle_below100_attribute, NULL }
2390 });
2391
2392 /* Handle an "interrupt" attribute;
2393 arguments as in struct attribute_spec.handler. */
2394
2395 static tree
2396 xstormy16_handle_interrupt_attribute (tree *node, tree name,
2397 tree args ATTRIBUTE_UNUSED,
2398 int flags ATTRIBUTE_UNUSED,
2399 bool *no_add_attrs)
2400 {
2401 if (TREE_CODE (*node) != FUNCTION_TYPE)
2402 {
2403 warning (OPT_Wattributes, "%qE attribute only applies to functions",
2404 name);
2405 *no_add_attrs = true;
2406 }
2407
2408 return NULL_TREE;
2409 }
2410
2411 /* Handle an "below" attribute;
2412 arguments as in struct attribute_spec.handler. */
2413
2414 static tree
2415 xstormy16_handle_below100_attribute (tree *node,
2416 tree name ATTRIBUTE_UNUSED,
2417 tree args ATTRIBUTE_UNUSED,
2418 int flags ATTRIBUTE_UNUSED,
2419 bool *no_add_attrs)
2420 {
2421 if (TREE_CODE (*node) != VAR_DECL
2422 && TREE_CODE (*node) != POINTER_TYPE
2423 && TREE_CODE (*node) != TYPE_DECL)
2424 {
2425 warning (OPT_Wattributes,
2426 "%<__BELOW100__%> attribute only applies to variables");
2427 *no_add_attrs = true;
2428 }
2429 else if (args == NULL_TREE && VAR_P (*node))
2430 {
2431 if (! (TREE_PUBLIC (*node) || TREE_STATIC (*node)))
2432 {
2433 warning (OPT_Wattributes, "%<__BELOW100__%> attribute not allowed "
2434 "with auto storage class");
2435 *no_add_attrs = true;
2436 }
2437 }
2438
2439 return NULL_TREE;
2440 }
2441
2442 #undef TARGET_INIT_BUILTINS
2443 #define TARGET_INIT_BUILTINS xstormy16_init_builtins
2444 #undef TARGET_EXPAND_BUILTIN
2445 #define TARGET_EXPAND_BUILTIN xstormy16_expand_builtin
2446
2447 static struct
2448 {
2449 const char * name;
2450 int md_code;
2451 const char * arg_ops; /* 0..9, t for temp register, r for return value. */
2452 const char * arg_types; /* s=short,l=long, upper case for unsigned. */
2453 }
2454 s16builtins[] =
2455 {
2456 { "__sdivlh", CODE_FOR_sdivlh, "rt01", "sls" },
2457 { "__smodlh", CODE_FOR_sdivlh, "tr01", "sls" },
2458 { "__udivlh", CODE_FOR_udivlh, "rt01", "SLS" },
2459 { "__umodlh", CODE_FOR_udivlh, "tr01", "SLS" },
2460 { NULL, 0, NULL, NULL }
2461 };
2462
2463 static void
2464 xstormy16_init_builtins (void)
2465 {
2466 tree args[2], ret_type, arg = NULL_TREE, ftype;
2467 int i, a, n_args;
2468
2469 ret_type = void_type_node;
2470
2471 for (i = 0; s16builtins[i].name; i++)
2472 {
2473 n_args = strlen (s16builtins[i].arg_types) - 1;
2474
2475 gcc_assert (n_args <= (int) ARRAY_SIZE (args));
2476
2477 for (a = n_args - 1; a >= 0; a--)
2478 args[a] = NULL_TREE;
2479
2480 for (a = n_args; a >= 0; a--)
2481 {
2482 switch (s16builtins[i].arg_types[a])
2483 {
2484 case 's': arg = short_integer_type_node; break;
2485 case 'S': arg = short_unsigned_type_node; break;
2486 case 'l': arg = long_integer_type_node; break;
2487 case 'L': arg = long_unsigned_type_node; break;
2488 default: gcc_unreachable ();
2489 }
2490 if (a == 0)
2491 ret_type = arg;
2492 else
2493 args[a-1] = arg;
2494 }
2495 ftype = build_function_type_list (ret_type, args[0], args[1], NULL_TREE);
2496 add_builtin_function (s16builtins[i].name, ftype,
2497 i, BUILT_IN_MD, NULL, NULL_TREE);
2498 }
2499 }
2500
2501 static rtx
2502 xstormy16_expand_builtin (tree exp, rtx target,
2503 rtx subtarget ATTRIBUTE_UNUSED,
2504 machine_mode mode ATTRIBUTE_UNUSED,
2505 int ignore ATTRIBUTE_UNUSED)
2506 {
2507 rtx op[10], args[10], pat, copyto[10], retval = 0;
2508 tree fndecl, argtree;
2509 int i, a, o, code;
2510
2511 fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
2512 argtree = TREE_OPERAND (exp, 1);
2513 i = DECL_MD_FUNCTION_CODE (fndecl);
2514 code = s16builtins[i].md_code;
2515
2516 for (a = 0; a < 10 && argtree; a++)
2517 {
2518 args[a] = expand_normal (TREE_VALUE (argtree));
2519 argtree = TREE_CHAIN (argtree);
2520 }
2521
2522 for (o = 0; s16builtins[i].arg_ops[o]; o++)
2523 {
2524 char ao = s16builtins[i].arg_ops[o];
2525 char c = insn_data[code].operand[o].constraint[0];
2526 machine_mode omode;
2527
2528 copyto[o] = 0;
2529
2530 omode = (machine_mode) insn_data[code].operand[o].mode;
2531 if (ao == 'r')
2532 op[o] = target ? target : gen_reg_rtx (omode);
2533 else if (ao == 't')
2534 op[o] = gen_reg_rtx (omode);
2535 else
2536 op[o] = args[(int) hex_value (ao)];
2537
2538 if (! (*insn_data[code].operand[o].predicate) (op[o], GET_MODE (op[o])))
2539 {
2540 if (c == '+' || c == '=')
2541 {
2542 copyto[o] = op[o];
2543 op[o] = gen_reg_rtx (omode);
2544 }
2545 else
2546 op[o] = copy_to_mode_reg (omode, op[o]);
2547 }
2548
2549 if (ao == 'r')
2550 retval = op[o];
2551 }
2552
2553 pat = GEN_FCN (code) (op[0], op[1], op[2], op[3], op[4],
2554 op[5], op[6], op[7], op[8], op[9]);
2555 emit_insn (pat);
2556
2557 for (o = 0; s16builtins[i].arg_ops[o]; o++)
2558 if (copyto[o])
2559 {
2560 emit_move_insn (copyto[o], op[o]);
2561 if (op[o] == retval)
2562 retval = copyto[o];
2563 }
2564
2565 return retval;
2566 }
2567
2568 /* Look for combinations of insns that can be converted to BN or BP
2569 opcodes. This is, unfortunately, too complex to do with MD
2570 patterns. */
2571
2572 static void
2573 combine_bnp (rtx_insn *insn)
2574 {
2575 int insn_code, regno, need_extend;
2576 unsigned int mask;
2577 rtx cond, reg, qireg, mem;
2578 rtx_insn *and_insn, *load;
2579 machine_mode load_mode = QImode;
2580 machine_mode and_mode = QImode;
2581 rtx_insn *shift = NULL;
2582
2583 insn_code = recog_memoized (insn);
2584 if (insn_code != CODE_FOR_cbranchhi
2585 && insn_code != CODE_FOR_cbranchhi_neg)
2586 return;
2587
2588 cond = XVECEXP (PATTERN (insn), 0, 0); /* set */
2589 cond = XEXP (cond, 1); /* if */
2590 cond = XEXP (cond, 0); /* cond */
2591 switch (GET_CODE (cond))
2592 {
2593 case NE:
2594 case EQ:
2595 need_extend = 0;
2596 break;
2597 case LT:
2598 case GE:
2599 need_extend = 1;
2600 break;
2601 default:
2602 return;
2603 }
2604
2605 reg = XEXP (cond, 0);
2606 if (! REG_P (reg))
2607 return;
2608 regno = REGNO (reg);
2609 if (XEXP (cond, 1) != const0_rtx)
2610 return;
2611 if (! find_regno_note (insn, REG_DEAD, regno))
2612 return;
2613 qireg = gen_rtx_REG (QImode, regno);
2614
2615 if (need_extend)
2616 {
2617 /* LT and GE conditionals should have a sign extend before
2618 them. */
2619 for (and_insn = prev_real_insn (insn);
2620 and_insn != NULL_RTX;
2621 and_insn = prev_real_insn (and_insn))
2622 {
2623 int and_code = recog_memoized (and_insn);
2624
2625 if (and_code == CODE_FOR_extendqihi2
2626 && rtx_equal_p (SET_DEST (PATTERN (and_insn)), reg)
2627 && rtx_equal_p (XEXP (SET_SRC (PATTERN (and_insn)), 0), qireg))
2628 break;
2629
2630 if (and_code == CODE_FOR_movhi_internal
2631 && rtx_equal_p (SET_DEST (PATTERN (and_insn)), reg))
2632 {
2633 /* This is for testing bit 15. */
2634 and_insn = insn;
2635 break;
2636 }
2637
2638 if (reg_mentioned_p (reg, and_insn))
2639 return;
2640
2641 if (! NOTE_P (and_insn) && ! NONJUMP_INSN_P (and_insn))
2642 return;
2643 }
2644 }
2645 else
2646 {
2647 /* EQ and NE conditionals have an AND before them. */
2648 for (and_insn = prev_real_insn (insn);
2649 and_insn != NULL_RTX;
2650 and_insn = prev_real_insn (and_insn))
2651 {
2652 if (recog_memoized (and_insn) == CODE_FOR_andhi3
2653 && rtx_equal_p (SET_DEST (PATTERN (and_insn)), reg)
2654 && rtx_equal_p (XEXP (SET_SRC (PATTERN (and_insn)), 0), reg))
2655 break;
2656
2657 if (reg_mentioned_p (reg, and_insn))
2658 return;
2659
2660 if (! NOTE_P (and_insn) && ! NONJUMP_INSN_P (and_insn))
2661 return;
2662 }
2663
2664 if (and_insn)
2665 {
2666 /* Some mis-optimizations by GCC can generate a RIGHT-SHIFT
2667 followed by an AND like this:
2668
2669 (parallel [(set (reg:HI r7) (lshiftrt:HI (reg:HI r7) (const_int 3)))
2670 (clobber (reg:BI carry))]
2671
2672 (set (reg:HI r7) (and:HI (reg:HI r7) (const_int 1)))
2673
2674 Attempt to detect this here. */
2675 for (shift = prev_real_insn (and_insn); shift;
2676 shift = prev_real_insn (shift))
2677 {
2678 if (recog_memoized (shift) == CODE_FOR_lshrhi3
2679 && rtx_equal_p (SET_DEST (XVECEXP (PATTERN (shift), 0, 0)), reg)
2680 && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (shift), 0, 0)), 0), reg))
2681 break;
2682
2683 if (reg_mentioned_p (reg, shift)
2684 || (! NOTE_P (shift) && ! NONJUMP_INSN_P (shift)))
2685 {
2686 shift = NULL;
2687 break;
2688 }
2689 }
2690 }
2691 }
2692
2693 if (and_insn == NULL_RTX)
2694 return;
2695
2696 for (load = shift ? prev_real_insn (shift) : prev_real_insn (and_insn);
2697 load;
2698 load = prev_real_insn (load))
2699 {
2700 int load_code = recog_memoized (load);
2701
2702 if (load_code == CODE_FOR_movhi_internal
2703 && rtx_equal_p (SET_DEST (PATTERN (load)), reg)
2704 && xstormy16_below100_operand (SET_SRC (PATTERN (load)), HImode)
2705 && ! MEM_VOLATILE_P (SET_SRC (PATTERN (load))))
2706 {
2707 load_mode = HImode;
2708 break;
2709 }
2710
2711 if (load_code == CODE_FOR_movqi_internal
2712 && rtx_equal_p (SET_DEST (PATTERN (load)), qireg)
2713 && xstormy16_below100_operand (SET_SRC (PATTERN (load)), QImode))
2714 {
2715 load_mode = QImode;
2716 break;
2717 }
2718
2719 if (load_code == CODE_FOR_zero_extendqihi2
2720 && rtx_equal_p (SET_DEST (PATTERN (load)), reg)
2721 && xstormy16_below100_operand (XEXP (SET_SRC (PATTERN (load)), 0), QImode))
2722 {
2723 load_mode = QImode;
2724 and_mode = HImode;
2725 break;
2726 }
2727
2728 if (reg_mentioned_p (reg, load))
2729 return;
2730
2731 if (! NOTE_P (load) && ! NONJUMP_INSN_P (load))
2732 return;
2733 }
2734 if (!load)
2735 return;
2736
2737 mem = SET_SRC (PATTERN (load));
2738
2739 if (need_extend)
2740 {
2741 mask = (load_mode == HImode) ? 0x8000 : 0x80;
2742
2743 /* If the mem includes a zero-extend operation and we are
2744 going to generate a sign-extend operation then move the
2745 mem inside the zero-extend. */
2746 if (GET_CODE (mem) == ZERO_EXTEND)
2747 mem = XEXP (mem, 0);
2748 }
2749 else
2750 {
2751 if (!xstormy16_onebit_set_operand (XEXP (SET_SRC (PATTERN (and_insn)), 1),
2752 load_mode))
2753 return;
2754
2755 mask = (int) INTVAL (XEXP (SET_SRC (PATTERN (and_insn)), 1));
2756
2757 if (shift)
2758 mask <<= INTVAL (XEXP (SET_SRC (XVECEXP (PATTERN (shift), 0, 0)), 1));
2759 }
2760
2761 if (load_mode == HImode)
2762 {
2763 rtx addr = XEXP (mem, 0);
2764
2765 if (! (mask & 0xff))
2766 {
2767 addr = plus_constant (Pmode, addr, 1);
2768 mask >>= 8;
2769 }
2770 mem = gen_rtx_MEM (QImode, addr);
2771 }
2772
2773 if (need_extend)
2774 XEXP (cond, 0) = gen_rtx_SIGN_EXTEND (HImode, mem);
2775 else
2776 XEXP (cond, 0) = gen_rtx_AND (and_mode, mem, GEN_INT (mask));
2777
2778 INSN_CODE (insn) = -1;
2779 delete_insn (load);
2780
2781 if (and_insn != insn)
2782 delete_insn (and_insn);
2783
2784 if (shift != NULL_RTX)
2785 delete_insn (shift);
2786 }
2787
2788 static void
2789 xstormy16_reorg (void)
2790 {
2791 rtx_insn *insn;
2792
2793 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
2794 {
2795 if (! JUMP_P (insn))
2796 continue;
2797 combine_bnp (insn);
2798 }
2799 }
2800
2801 /* Worker function for TARGET_RETURN_IN_MEMORY. */
2802
2803 static bool
2804 xstormy16_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
2805 {
2806 const HOST_WIDE_INT size = int_size_in_bytes (type);
2807 return (size == -1 || size > UNITS_PER_WORD * NUM_ARGUMENT_REGISTERS);
2808 }
2809
2810 /* Implement TARGET_HARD_REGNO_MODE_OK. */
2811
2812 static bool
2813 xstormy16_hard_regno_mode_ok (unsigned int regno, machine_mode mode)
2814 {
2815 return regno != 16 || mode == BImode;
2816 }
2817
2818 /* Implement TARGET_MODES_TIEABLE_P. */
2819
2820 static bool
2821 xstormy16_modes_tieable_p (machine_mode mode1, machine_mode mode2)
2822 {
2823 return mode1 != BImode && mode2 != BImode;
2824 }
2825
2826 /* Implement PUSH_ROUNDING. */
2827
2828 poly_int64
2829 xstormy16_push_rounding (poly_int64 bytes)
2830 {
2831 return (bytes + 1) & ~1;
2832 }
2833
2834 #undef TARGET_ASM_ALIGNED_HI_OP
2835 #define TARGET_ASM_ALIGNED_HI_OP "\t.hword\t"
2836 #undef TARGET_ASM_ALIGNED_SI_OP
2837 #define TARGET_ASM_ALIGNED_SI_OP "\t.word\t"
2838 #undef TARGET_ENCODE_SECTION_INFO
2839 #define TARGET_ENCODE_SECTION_INFO xstormy16_encode_section_info
2840
2841 /* Select_section doesn't handle .bss_below100. */
2842 #undef TARGET_HAVE_SWITCHABLE_BSS_SECTIONS
2843 #define TARGET_HAVE_SWITCHABLE_BSS_SECTIONS false
2844
2845 #undef TARGET_ASM_OUTPUT_MI_THUNK
2846 #define TARGET_ASM_OUTPUT_MI_THUNK xstormy16_asm_output_mi_thunk
2847 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
2848 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
2849
2850 #undef TARGET_PRINT_OPERAND
2851 #define TARGET_PRINT_OPERAND xstormy16_print_operand
2852 #undef TARGET_PRINT_OPERAND_ADDRESS
2853 #define TARGET_PRINT_OPERAND_ADDRESS xstormy16_print_operand_address
2854
2855 #undef TARGET_MEMORY_MOVE_COST
2856 #define TARGET_MEMORY_MOVE_COST xstormy16_memory_move_cost
2857 #undef TARGET_RTX_COSTS
2858 #define TARGET_RTX_COSTS xstormy16_rtx_costs
2859 #undef TARGET_ADDRESS_COST
2860 #define TARGET_ADDRESS_COST xstormy16_address_cost
2861
2862 #undef TARGET_BUILD_BUILTIN_VA_LIST
2863 #define TARGET_BUILD_BUILTIN_VA_LIST xstormy16_build_builtin_va_list
2864 #undef TARGET_EXPAND_BUILTIN_VA_START
2865 #define TARGET_EXPAND_BUILTIN_VA_START xstormy16_expand_builtin_va_start
2866 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
2867 #define TARGET_GIMPLIFY_VA_ARG_EXPR xstormy16_gimplify_va_arg_expr
2868
2869 #undef TARGET_PROMOTE_FUNCTION_MODE
2870 #define TARGET_PROMOTE_FUNCTION_MODE default_promote_function_mode_always_promote
2871 #undef TARGET_PROMOTE_PROTOTYPES
2872 #define TARGET_PROMOTE_PROTOTYPES hook_bool_const_tree_true
2873
2874 #undef TARGET_FUNCTION_ARG
2875 #define TARGET_FUNCTION_ARG xstormy16_function_arg
2876 #undef TARGET_FUNCTION_ARG_ADVANCE
2877 #define TARGET_FUNCTION_ARG_ADVANCE xstormy16_function_arg_advance
2878
2879 #undef TARGET_RETURN_IN_MEMORY
2880 #define TARGET_RETURN_IN_MEMORY xstormy16_return_in_memory
2881 #undef TARGET_FUNCTION_VALUE
2882 #define TARGET_FUNCTION_VALUE xstormy16_function_value
2883 #undef TARGET_LIBCALL_VALUE
2884 #define TARGET_LIBCALL_VALUE xstormy16_libcall_value
2885 #undef TARGET_FUNCTION_VALUE_REGNO_P
2886 #define TARGET_FUNCTION_VALUE_REGNO_P xstormy16_function_value_regno_p
2887
2888 #undef TARGET_MACHINE_DEPENDENT_REORG
2889 #define TARGET_MACHINE_DEPENDENT_REORG xstormy16_reorg
2890
2891 #undef TARGET_PREFERRED_RELOAD_CLASS
2892 #define TARGET_PREFERRED_RELOAD_CLASS xstormy16_preferred_reload_class
2893 #undef TARGET_PREFERRED_OUTPUT_RELOAD_CLASS
2894 #define TARGET_PREFERRED_OUTPUT_RELOAD_CLASS xstormy16_preferred_reload_class
2895
2896 #undef TARGET_LEGITIMATE_ADDRESS_P
2897 #define TARGET_LEGITIMATE_ADDRESS_P xstormy16_legitimate_address_p
2898 #undef TARGET_MODE_DEPENDENT_ADDRESS_P
2899 #define TARGET_MODE_DEPENDENT_ADDRESS_P xstormy16_mode_dependent_address_p
2900
2901 #undef TARGET_CAN_ELIMINATE
2902 #define TARGET_CAN_ELIMINATE xstormy16_can_eliminate
2903
2904 #undef TARGET_TRAMPOLINE_INIT
2905 #define TARGET_TRAMPOLINE_INIT xstormy16_trampoline_init
2906
2907 #undef TARGET_HARD_REGNO_MODE_OK
2908 #define TARGET_HARD_REGNO_MODE_OK xstormy16_hard_regno_mode_ok
2909 #undef TARGET_MODES_TIEABLE_P
2910 #define TARGET_MODES_TIEABLE_P xstormy16_modes_tieable_p
2911
2912 #undef TARGET_CONSTANT_ALIGNMENT
2913 #define TARGET_CONSTANT_ALIGNMENT constant_alignment_word_strings
2914
2915 #undef TARGET_HAVE_SPECULATION_SAFE_VALUE
2916 #define TARGET_HAVE_SPECULATION_SAFE_VALUE speculation_safe_value_not_needed
2917
2918 #undef TARGET_DOCUMENTATION_NAME
2919 #define TARGET_DOCUMENTATION_NAME "Xstormy16"
2920
2921 struct gcc_target targetm = TARGET_INITIALIZER;
2922
2923 #include "gt-stormy16.h"