1 ;; Machine Description for Renesas RL78 processors
2 ;; Copyright (C) 2011-2024 Free Software Foundation, Inc.
3 ;; Contributed by Red Hat.
5 ;; This file is part of GCC.
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)
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.
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/>.
50 (UNS_TRAMPOLINE_INIT 20)
51 (UNS_TRAMPOLINE_UNINIT 21)
52 (UNS_NONLOCAL_GOTO 22)
62 (define_mode_iterator QHI [QI HI])
64 (include "predicates.md")
65 (include "constraints.md")
66 (include "rl78-expand.md")
67 (include "rl78-virt.md")
68 (include "rl78-real.md")
70 (define_attr "is_g13_muldiv_insn" "yes,no" (const_string "no"))
72 ;; Function Prologue/Epilogue Instructions
74 (define_expand "prologue"
77 "rl78_expand_prologue (); DONE;"
80 (define_expand "epilogue"
83 "rl78_expand_epilogue (); DONE;"
86 (define_expand "sibcall_epilogue"
92 (define_insn "rl78_return"
98 (define_insn "interrupt_return"
99 [(unspec_volatile [(return)] UNS_RETI) ]
104 (define_insn "brk_interrupt_return"
105 [(unspec_volatile [(return)] UNS_RETB) ]
110 (define_expand "eh_return"
111 [(match_operand:HI 0 "")]
113 "rl78_expand_eh_epilogue (operands[0]);
118 ;; These are used only by prologue/epilogue so it's "safe" to pass
119 ;; virtual registers.
121 [(set (reg:HI SP_REG)
122 (plus:HI (reg:HI SP_REG)
124 (set (mem:HI (reg:HI SP_REG))
125 (match_operand:HI 0 "register_operand" "ABDT,vZint"))]
133 [(set (match_operand:HI 0 "register_operand" "=ABDT,vZint")
134 (mem:HI (reg:HI SP_REG)))
136 (plus:HI (reg:HI SP_REG)
144 (define_insn "sel_rb"
145 [(unspec_volatile [(match_operand 0 "immediate_operand" "")] UNS_SET_RB)]
150 (define_insn "trampoline_init"
151 [(set (match_operand 0 "register_operand" "=Z08W")
152 (unspec_volatile [(match_operand 1 "register_operand" "Z08W")
153 (match_operand 2 "register_operand" "Z10W")
154 ] UNS_TRAMPOLINE_INIT))
157 "call !!___trampoline_init ; %0 <= %1 %2"
160 (define_insn "trampoline_uninit"
161 [(unspec_volatile [(const_int 0)] UNS_TRAMPOLINE_UNINIT)
164 "call !!___trampoline_uninit"
167 ;; GCC restores $fp *before* using it to access values on the *old*
168 ;; frame. So, we do it ourselves, to ensure this is not the case.
169 ;; Note that while %1 is usually a label_ref, we allow for a
170 ;; non-immediate as well.
171 (define_expand "nonlocal_goto"
173 (unspec_volatile [(match_operand 0 "") ;; fp (ignore)
174 (match_operand 1 "") ;; target
175 (match_operand 2 "") ;; sp
176 (match_operand 3 "") ;; ?
177 ] UNS_NONLOCAL_GOTO))
180 "emit_jump_insn (gen_nonlocal_goto_insn (operands[0], operands[1], operands[2], operands[3]));
185 (define_insn "nonlocal_goto_insn"
187 (unspec_volatile [(match_operand 0 "" "") ;; fp (ignore)
188 (match_operand 1 "" "vi") ;; target
189 (match_operand 2 "" "vi") ;; sp
190 (match_operand 3 "" "vi") ;; ?
191 ] UNS_NONLOCAL_GOTO))
204 (define_expand "es_addr"
205 [(unspec:SI [(reg:QI ES_REG)
206 (match_operand:HI 0 "")
212 ;;======================================================================
214 ;; "macro" insns - cases where inline chunks of code are more
215 ;; efficient than anything else.
217 (define_expand "addsi3"
218 [(set (match_operand:SI 0 "nonimmediate_operand" "=&vm")
219 (plus:SI (match_operand:SI 1 "general_operand" "vim")
220 (match_operand 2 "general_operand" "vim")))
223 "emit_insn (gen_addsi3_internal_virt (operands[0], operands[1], operands[2]));
227 (define_expand "adddi3"
228 [(set (match_operand:DI 0 "nonimmediate_operand" "")
229 (plus:DI (match_operand:DI 1 "general_operand" "")
230 (match_operand:DI 2 "general_operand" "")))
233 "rl78_emit_libcall (\"__adddi3\", PLUS, DImode, DImode, 3, operands);
237 (define_insn "addsi3_internal_virt"
238 [(set (match_operand:SI 0 "nonimmediate_operand" "=v,&vm, vm")
239 (plus:SI (match_operand:SI 1 "general_operand" "0, vim, vim")
240 (match_operand 2 "general_operand" "vim,vim,vim")))
241 (clobber (reg:HI AX_REG))
242 (clobber (reg:HI BC_REG))
244 "rl78_virt_insns_ok ()"
246 [(set_attr "valloc" "macax")]
249 (define_insn "addsi3_internal_real"
250 [(set (match_operand:SI 0 "nonimmediate_operand" "=v,&vU, vU")
251 (plus:SI (match_operand:SI 1 "general_operand" "+0, viU, viU")
252 (match_operand 2 "general_operand" "viWabWhlWh1,viWabWhlWh1,viWabWhlWh1")))
253 (clobber (reg:HI AX_REG))
254 (clobber (reg:HI BC_REG))
256 "rl78_real_insns_ok ()"
257 { return rl78_addsi3_internal (operands, which_alternative); }
258 [(set_attr "valloc" "macax")]
261 (define_expand "subsi3"
262 [(set (match_operand:SI 0 "nonimmediate_operand")
263 (minus:SI (match_operand:SI 1 "general_operand")
264 (match_operand 2 "general_operand")))
267 "emit_insn (gen_subsi3_internal_virt (operands[0], operands[1], operands[2]));
271 (define_expand "subdi3"
272 [(set (match_operand:DI 0 "nonimmediate_operand" "")
273 (minus:DI (match_operand:DI 1 "general_operand" "")
274 (match_operand:DI 2 "general_operand" "")))
277 "rl78_emit_libcall (\"__subdi3\", MINUS, DImode, DImode, 3, operands);
281 (define_insn "subsi3_internal_virt"
282 [(set (match_operand:SI 0 "nonimmediate_operand" "=v,&vm, vm")
283 (minus:SI (match_operand:SI 1 "general_operand" "0, vim, vim")
284 (match_operand 2 "general_operand" "vim,vim,vim")))
285 (clobber (reg:HI AX_REG))
286 (clobber (reg:HI BC_REG))
288 "rl78_virt_insns_ok ()"
290 [(set_attr "valloc" "macax")]
293 (define_insn "subsi3_internal_real"
294 [(set (match_operand:SI 0 "nonimmediate_operand" "=v,&vU, vU")
295 (minus:SI (match_operand:SI 1 "general_operand" "+0, viU, viU")
296 (match_operand 2 "general_operand" "viWabWhlWh1,viWabWhlWh1,viWabWhlWh1")))
297 (clobber (reg:HI AX_REG))
298 (clobber (reg:HI BC_REG))
300 "rl78_real_insns_ok ()"
302 movw ax,%h1 \;subw ax,%h2 \;movw %h0, ax \;movw ax,%H1 \;sknc \;decw ax \;subw ax,%H2 \;movw %H0,ax
303 movw ax,%h1 \;subw ax,%h2 \;movw %h0, ax \;movw ax,%H1 \;sknc \;decw ax \;subw ax,%H2 \;movw %H0,ax
304 movw ax,%h1 \;subw ax,%h2 \;movw bc, ax \;movw ax,%H1 \;sknc \;decw ax \;subw ax,%H2 \;movw %H0,ax \;movw ax,bc \;movw %h0, ax"
305 [(set_attr "valloc" "macax")]
308 (define_expand "mulqi3"
310 [(set (match_operand:QI 0 "register_operand")
311 (mult:QI (match_operand:QI 1 "general_operand")
312 (match_operand:QI 2 "nonmemory_operand")))
313 (clobber (reg:HI AX_REG))
316 "" ; mulu supported by all targets
320 (define_expand "mulhi3"
321 [(set (match_operand:HI 0 "register_operand")
322 (mult:HI (match_operand:HI 1 "general_operand")
323 (match_operand:HI 2 "nonmemory_operand")))
328 emit_insn (gen_mulhi3_g14 (operands[0], operands[1], operands[2]));
329 else /* RL78_MUL_G13 */
330 emit_insn (gen_mulhi3_g13 (operands[0], operands[1], operands[2]));
335 (define_expand "mulsi3"
336 [(set (match_operand:SI 0 "register_operand")
337 (mult:SI (match_operand:SI 1 "general_operand")
338 (match_operand:SI 2 "nonmemory_operand")))
343 emit_insn (gen_mulsi3_g14 (operands[0], operands[1], operands[2]));
344 else /* RL78_MUL_G13 */
345 emit_insn (gen_mulsi3_g13 (operands[0], operands[1], operands[2]));
350 (define_insn "*mulqi3_rl78"
351 [(set (match_operand:QI 0 "register_operand" "=&v")
352 (mult:QI (match_operand:QI 1 "general_operand" "viU")
353 (match_operand:QI 2 "general_operand" "vi")))
354 (clobber (reg:HI AX_REG))
356 "" ; mulu supported by all targets
357 "; mulqi macro %0 = %1 * %2
364 ; end of mulqi macro"
365 [(set_attr "valloc" "macax")]
368 (define_insn "mulhi3_g14"
369 [(set (match_operand:HI 0 "register_operand" "=&v")
370 (mult:HI (match_operand:HI 1 "general_operand" "viU")
371 (match_operand:HI 2 "general_operand" "vi")))
372 (clobber (reg:HI AX_REG))
373 (clobber (reg:HI BC_REG))
376 "; G14 mulhi macro %0 = %1 * %2
379 mulhu ; bcax = bc * ax
381 ; end of mulhi macro"
382 [(set_attr "valloc" "macax")]
385 (define_insn "mulhi3_g13"
386 [(set (match_operand:HI 0 "register_operand" "=&v")
387 (mult:HI (match_operand:HI 1 "general_operand" "viU")
388 (match_operand:HI 2 "general_operand" "vi")))
389 (clobber (reg:HI AX_REG))
392 "; G13 mulhi macro %0 = %1 * %2
394 mov !0xf00e8, a ; MDUC
396 movw 0xffff0, ax ; MDAL
398 movw 0xffff2, ax ; MDAH
399 nop ; mdb = mdal * mdah
400 movw ax, 0xffff6 ; MDBL
402 ; end of mulhi macro"
403 [(set_attr "valloc" "macax")
404 (set_attr "is_g13_muldiv_insn" "yes")]
407 ;; 0xFFFF0 is MACR(L). 0xFFFF2 is MACR(H) but we don't care about it
408 ;; because we're only using the lower 16 bits (which is the upper 16
409 ;; bits of the result).
410 (define_insn "mulsi3_g14"
411 [(set (match_operand:SI 0 "register_operand" "=&v")
412 (mult:SI (match_operand:SI 1 "general_operand" "viU")
413 (match_operand:SI 2 "general_operand" "vi")))
414 (clobber (reg:HI AX_REG))
415 (clobber (reg:HI BC_REG))
418 "; G14 mulsi macro %0 = %1 * %2
421 MULHU ; bcax = bc * ax
427 MACHU ; MACR += bc * ax
430 MACHU ; MACR += bc * ax
433 ; end of mulsi macro"
434 [(set_attr "valloc" "macax")]
437 ;; 0xFFFF0 is MDAL. 0xFFFF2 is MDAH.
438 ;; 0xFFFF6 is MDBL. 0xFFFF4 is MDBH.
439 ;; 0xF00E0 is MDCL. 0xF00E2 is MDCH.
441 ;; Warning: this matches the silicon not the documentation.
442 (define_insn "mulsi3_g13"
443 [(set (match_operand:SI 0 "register_operand" "=&v")
444 (mult:SI (match_operand:SI 1 "general_operand" "viU")
445 (match_operand:SI 2 "general_operand" "viU")))
446 (clobber (reg:HI AX_REG))
447 (clobber (reg:HI BC_REG))
450 "; G13 mulsi macro %0 = %1 * %2
452 mov !0xf00e8, a ; MDUC
454 movw 0xffff0, ax ; MDAL
456 movw 0xffff2, ax ; MDAH
457 nop ; mdb = mdal * mdah
458 movw ax, 0xffff6 ; MDBL
462 mov !0xf00e8, a ; MDUC
463 movw ax, 0xffff4 ; MDBH
464 movw !0xf00e0, ax ; MDCL
466 movw !0xf00e2, ax ; MDCL
468 movw 0xffff0, ax ; MDAL
470 movw 0xffff2, ax ; MDAH
471 nop ; mdc += mdal * mdah
474 mov !0xf00e8, a ; MDUC
476 movw 0xffff0, ax ; MDAL
478 movw 0xffff2, ax ; MDAH
479 nop ; mdc += mdal * mdah
480 nop ; Additional nop for MAC
481 movw ax, !0xf00e0 ; MDCL
483 ; end of mulsi macro"
484 [(set_attr "valloc" "macax")
485 (set_attr "is_g13_muldiv_insn" "yes")]
488 (define_expand "udivmodhi4"
490 [(set (match_operand:HI 0 "register_operand")
491 (udiv:HI (match_operand:HI 1 "register_operand")
492 (match_operand:HI 2 "register_operand")))
493 (set (match_operand:HI 3 "register_operand")
494 (umod:HI (match_dup 1) (match_dup 2)))
495 (clobber (reg:HI AX_REG))
496 (clobber (reg:HI DE_REG))
503 (define_insn "*udivmodhi4_g14"
504 [(set (match_operand:HI 0 "register_operand" "=v")
505 (udiv:HI (match_operand:HI 1 "register_operand" "v")
506 (match_operand:HI 2 "register_operand" "v")))
507 (set (match_operand:HI 3 "register_operand" "=v")
508 (umod:HI (match_dup 1) (match_dup 2)))
509 (clobber (reg:HI AX_REG))
510 (clobber (reg:HI DE_REG))
514 if (find_reg_note (insn, REG_UNUSED, operands[3]))
515 return "; G14 udivhi macro %0 = %1 / %2 \n\
518 push psw ; Save the current interrupt status \n\
519 di ; Disable interrupts. See Renesas Technical update TN-RL*-A025B/E \n\
520 divhu ; ax = ax / de \n\
521 pop psw ; Restore saved interrupt status \n\
523 ; end of udivhi macro";
524 else if (find_reg_note (insn, REG_UNUSED, operands[0]))
525 return "; G14 umodhi macro %3 = %1 %% %2 \n\
528 push psw ; Save the current interrupt status \n\
529 di ; Disable interrupts. See Renesas Technical update TN-RL*-A025B/E \n\
530 divhu ; de = ax %% de \n\
531 pop psw ; Restore saved interrupt status \n\
534 ; end of umodhi macro";
536 return "; G14 udivmodhi macro %0 = %1 / %2 and %3 = %1 %% %2 \n\
539 push psw ; Save the current interrupt status \n\
540 di ; Disable interrupts. See Renesas Technical update TN-RL*-A025B/E \n\
541 divhu ; ax = ax / de, de = ax %% de \n\
542 pop psw ; Restore saved interrupt status \n\
546 ; end of udivmodhi macro";
548 [(set_attr "valloc" "divhi")]
551 (define_expand "udivmodsi4"
553 [(set (match_operand:SI 0 "register_operand")
554 (udiv:SI (match_operand:SI 1 "register_operand")
555 (match_operand:SI 2 "register_operand")))
556 (set (match_operand:SI 3 "register_operand")
557 (umod:SI (match_dup 1) (match_dup 2)))
560 "! RL78_MUL_NONE && ! optimize_size"
563 emit_insn (gen_udivmodsi4_g14 (operands[0], operands[1], operands[2], operands[3]));
564 else /* RL78_MUL_G13 */
565 emit_insn (gen_udivmodsi4_g13 (operands[0], operands[1], operands[2], operands[3]));
570 (define_insn "udivmodsi4_g14"
571 [(set (match_operand:SI 0 "register_operand" "=v")
572 (udiv:SI (match_operand:SI 1 "register_operand" "v")
573 (match_operand:SI 2 "register_operand" "v")))
574 (set (match_operand:SI 3 "register_operand" "=v")
575 (umod:SI (match_dup 1) (match_dup 2)))
576 (clobber (reg:HI AX_REG))
577 (clobber (reg:HI BC_REG))
578 (clobber (reg:HI DE_REG))
579 (clobber (reg:HI HL_REG))
583 if (find_reg_note (insn, REG_UNUSED, operands[3]))
584 return "; G14 udivsi macro %0 = %1 / %2 \n\
589 push psw ; Save the current interrupt status \n\
590 di ; Disable interrupts. See Renesas Technical update TN-RL*-A025B/E \n\
591 divwu ; bcax = bcax / hlde \n\
592 pop psw ; Restore saved interrupt status \n\
596 ; end of udivsi macro";
597 else if (find_reg_note (insn, REG_UNUSED, operands[0]))
598 return "; G14 umodsi macro %3 = %1 %% %2 \n\
603 push psw ; Save the current interrupt status \n\
604 di ; Disable interrupts. See Renesas Technical update TN-RL*-A025B/E \n\
605 divwu ; hlde = bcax %% hlde \n\
606 pop psw ; Restore saved interrupt status \n\
611 ; end of umodsi macro";
613 return "; G14 udivmodsi macro %0 = %1 / %2 and %3 = %1 %% %2 \n\
618 push psw ; Save the current interrupt status \n\
619 di ; Disable interrupts. See Renesas Technical update TN-RL*-A025B/E \n\
620 divwu ; bcax = bcax / hlde, hlde = bcax %% hlde \n\
621 pop psw ; Restore saved interrupt status \n\
629 ; end of udivmodsi macro";
631 [(set_attr "valloc" "divsi")]
634 ;; Warning: these values match the silicon not the documentation.
635 ;; 0xFFFF0 is MDAL. 0xFFFF2 is MDAH.
636 ;; 0xFFFF6 is MDBL. 0xFFFF4 is MDBH.
637 ;; 0xF00E0 is MDCL. 0xF00E2 is MDCH.
640 (define_insn "udivmodsi4_g13"
641 [(set (match_operand:SI 0 "register_operand" "=v")
642 (udiv:SI (match_operand:SI 1 "register_operand" "v")
643 (match_operand:SI 2 "register_operand" "v")))
644 (set (match_operand:SI 3 "register_operand" "=v")
645 (umod:SI (match_dup 1) (match_dup 2)))
646 (clobber (reg:HI AX_REG))
650 if (find_reg_note (insn, REG_UNUSED, operands[3]))
651 return "; G13 udivsi macro %0 = %1 / %2 \n\
652 mov a, #0xC0 ; Set DIVMODE=1 and MACMODE=1 \n\
653 mov !0xf00e8, a ; This preps the peripheral for division without interrupt generation \n\
655 movw 0xffff2, ax ; MDAH \n\
657 movw 0xffff0, ax ; MDAL \n\
659 movw 0xffff4, ax ; MDBH \n\
661 movw 0xffff6, ax ; MDBL \n\
662 mov a, #0xC1 ; Set the DIVST bit in MDUC \n\
663 mov !0xf00e8, a ; This starts the division op \n\
664 1: mov a, !0xf00e8 ; Wait 16 clocks or until DIVST is clear \n\
666 movw ax, 0xffff0 ; Read the quotient \n\
670 ; end of udivsi macro";
671 else if (find_reg_note (insn, REG_UNUSED, operands[0]))
672 return "; G13 umodsi macro %3 = %1 %% %2 \n\
673 mov a, #0xC0 ; Set DIVMODE=1 and MACMODE=1 \n\
674 mov !0xf00e8, a ; This preps the peripheral for division without interrupt generation \n\
676 movw 0xffff2, ax ; MDAH \n\
678 movw 0xffff0, ax ; MDAL \n\
680 movw 0xffff4, ax ; MDBH \n\
682 movw 0xffff6, ax ; MDBL \n\
683 mov a, #0xC1 ; Set the DIVST bit in MDUC \n\
684 mov !0xf00e8, a ; This starts the division op \n\
685 1: mov a, !0xf00e8 ; Wait 16 clocks or until DIVST is clear \n\
687 movw ax, !0xf00e0 ; Read the remainder \n\
689 movw ax, !0xf00e2 \n\
691 ; end of umodsi macro";
693 return "; G13 udivmodsi macro %0 = %1 / %2 and %3 = %1 %% %2 \n\
694 mov a, #0xC0 ; Set DIVMODE=1 and MACMODE=1 \n\
695 mov !0xf00e8, a ; This preps the peripheral for division without interrupt generation \n\
697 movw 0xffff2, ax ; MDAH \n\
699 movw 0xffff0, ax ; MDAL \n\
701 movw 0xffff4, ax ; MDBH \n\
703 movw 0xffff6, ax ; MDBL \n\
704 mov a, #0xC1 ; Set the DIVST bit in MDUC \n\
705 mov !0xf00e8, a ; This starts the division op \n\
706 1: mov a, !0xf00e8 ; Wait 16 clocks or until DIVST is clear \n\
708 movw ax, 0xffff0 ; Read the quotient \n\
712 movw ax, !0xf00e0 ; Read the remainder \n\
714 movw ax, !0xf00e2 \n\
716 ; end of udivmodsi macro";
718 [(set_attr "valloc" "macax")
719 (set_attr "is_g13_muldiv_insn" "yes")]
722 (define_expand "movdi"
723 [(set (match_operand:DI 0 "nonimmediate_operand" "")
724 (match_operand:DI 1 "general_operand" ""))]
726 "rl78_split_movdi(operands, DImode);
730 (define_expand "movdf"
731 [(set (match_operand:DF 0 "nonimmediate_operand" "")
732 (match_operand:DF 1 "general_operand" ""))]
734 "rl78_split_movdi(operands, DFmode);
738 (define_expand "umindi3"
739 [(set (match_operand:DI 0 "nonimmediate_operand" "")
740 (umin:DI (match_operand:DI 1 "general_operand" "")
741 (match_operand:DI 2 "general_operand" "")))
744 "rl78_emit_libcall (\"__umindi3\", UMIN, DImode, DImode, 3, operands);
748 (define_expand "umaxdi3"
749 [(set (match_operand:DI 0 "nonimmediate_operand" "")
750 (umax:DI (match_operand:DI 1 "general_operand" "")
751 (match_operand:DI 2 "general_operand" "")))
754 "rl78_emit_libcall (\"__umaxdi3\", UMAX, DImode, DImode, 3, operands);
758 (define_expand "smindi3"
759 [(set (match_operand:DI 0 "nonimmediate_operand" "")
760 (smin:DI (match_operand:DI 1 "general_operand" "")
761 (match_operand:DI 2 "general_operand" "")))
764 "rl78_emit_libcall (\"__smindi3\", SMIN, DImode, DImode, 3, operands);
768 (define_expand "smaxdi3"
769 [(set (match_operand:DI 0 "nonimmediate_operand" "")
770 (smax:DI (match_operand:DI 1 "general_operand" "")
771 (match_operand:DI 2 "general_operand" "")))
774 "rl78_emit_libcall (\"__smaxdi3\", SMAX, DImode, DImode, 3, operands);
778 (define_expand "anddi3"
779 [(set (match_operand:DI 0 "nonimmediate_operand" "")
780 (and:DI (match_operand:DI 1 "general_operand" "")
781 (match_operand:DI 2 "general_operand" "")))
784 "rl78_emit_libcall (\"__anddi3\", AND, DImode, DImode, 3, operands);