gcc/ChangeLog:

        * config/rx/predicates.md (rx_plt_call_operand): New predicate.
        (rx_pcrel_label_operand): Likewisze.
        * config/rx/rx.md (PIC_REG): New constant.
        (UNSPEC_PIC): Likewise.
        (UNSPEC_GOT): Likewise.
        (UNSPEC_GOTOFF): Likewise.
        (UNSPEC_PLT): Likewise.
        (UNSPEC_GOTFUNCDESC): Likewise.
        (UNSPEC_GOTOFFFUNCDESC): Likewise.
        (UNSPEC_PCREL): Likewise.
        (tablejump): Use relative in pic case.
        (call): Add FDPIC call.
        (call_value): Likewise.
        (call_internal): Add use PICREG.
        (call_value_internal): Likewise.
        (mov<register_modes:mode>): Add PIC case.
        (*mov<register_modes:mode>_internal): Added the condition that it is 
not UNSPEC.
        (movsi_pcrel): New.
        (addsi3): Add PIC case.
        (addsi_unspec): New.
        (addsi3_flags): New.
        (sym2GOT): New.
        (sym2GOTreg): New.
        (sym2GOTOFF): New.
        (sym2GOTOFFreg): New.
        (sym2GOTFUNCDESC): New.
        (sym2GOTFUNCDESCreg): New.
        (sym2GOTOFFFUNCDESC): New.
        (sym2GOTOFFFUNCDESCreg): New.
        (sym2PLT): New.
        (call_fdpic): New.
        (call_internal_fd): New.
        (call_internal_plt): New.
        (call_value_fdpic): New.
        (call_value_internal_fd): New.
        (call_value_internal_plt): New.

Signed-off-by: Yoshinori Sato <[email protected]>
---
 gcc/config/rx/predicates.md |  12 ++
 gcc/config/rx/rx.md         | 374 +++++++++++++++++++++++++++++++++---
 2 files changed, 358 insertions(+), 28 deletions(-)

diff --git a/gcc/config/rx/predicates.md b/gcc/config/rx/predicates.md
index 37fd63b538a..45248e3b7e5 100644
--- a/gcc/config/rx/predicates.md
+++ b/gcc/config/rx/predicates.md
@@ -321,3 +321,15 @@
             (match_operand 0 "general_operand"))
        (and (match_code "mem")
             (match_operand 0 "rx_restricted_mem_operand"))))
+
+(define_predicate "rx_plt_call_operand"
+  (match_code "const")
+)
+
+(define_predicate "rx_pcrel_label_operand"
+  (match_code "const")
+{
+  rtx unspec = XEXP (op, 0);
+
+  return (GET_CODE (unspec) == UNSPEC) && (XINT (unspec, 1) == UNSPEC_PCREL);
+})
diff --git a/gcc/config/rx/rx.md b/gcc/config/rx/rx.md
index b1cf21c9f2d..107bbf56565 100644
--- a/gcc/config/rx/rx.md
+++ b/gcc/config/rx/rx.md
@@ -35,6 +35,7 @@
 (define_constants
   [
    (SP_REG 0)
+   (PIC_REG               13)
    (CC_REG                16)
 
    (UNSPEC_LOW_REG         0)
@@ -76,6 +77,14 @@
 
    (UNSPEC_PID_ADDR       52)
 
+   (UNSPEC_PIC            60)
+   (UNSPEC_GOT            61)
+   (UNSPEC_GOTOFF         62)
+   (UNSPEC_PLT            63)
+   (UNSPEC_GOTFUNCDESC     64)
+   (UNSPEC_GOTOFFFUNCDESC  65)
+   (UNSPEC_PCREL           66)
+
    (CTRLREG_PSW                    0)
    (CTRLREG_PC             1)
    (CTRLREG_USP                    2)
@@ -342,9 +351,9 @@
        (match_operand:SI          0 "register_operand" "r"))
    (use (label_ref (match_operand  1 "" "")))]
   ""
-  { return TARGET_PID ? (TARGET_AS100_SYNTAX ? "\n?:\tbra\t%0"
-                                            : "\n1:\tbra\t%0")
-                                            : "\n1:jmp\t%0";
+  { return TARGET_PID || flag_pic ? (TARGET_AS100_SYNTAX ? "\n?:\tbra\t%0"
+                                                        : "\n1:\tbra\t%0")
+                                                         : "\n1:jmp\t%0";
   }
   [(set_attr "timings" "33")
    (set_attr "length" "2")]
@@ -425,14 +434,23 @@
 
 (define_expand "call"
   [(call (match_operand:QI 0 "general_operand")
-        (match_operand:SI 1 "general_operand"))]
+        (match_operand:SI 1 "general_operand"))
+        (use (match_operand 2 "" ""))
+        (use (reg:SI PIC_REG))]
   ""
   {
     rtx dest = XEXP (operands[0], 0);
 
-    if (! rx_call_operand (dest, Pmode))
-      dest = force_reg (Pmode, dest);
-    emit_call_insn (gen_call_internal (dest));
+    if (!TARGET_FDPIC)
+      {
+        if (! rx_call_operand (dest, Pmode))
+          dest = force_reg (Pmode, dest);
+        emit_call_insn (gen_call_internal (dest));
+      }
+    else
+      {
+       emit_call_insn (gen_call_fdpic(operands[0], operands[1], operands[2]));
+      }
     DONE;
   }
 )
@@ -440,7 +458,8 @@
 (define_insn "call_internal"
   [(call (mem:QI (match_operand:SI 0 "rx_call_operand" "r,CALL_OP_SYMBOL_REF"))
         (const_int 0))
-   (clobber (reg:CC CC_REG))]
+   (clobber (reg:CC CC_REG))
+   (use (reg:SI PIC_REG))]
   ""
   "@
   jsr\t%0
@@ -452,14 +471,24 @@
 (define_expand "call_value"
   [(set (match_operand          0 "register_operand")
        (call (match_operand:QI 1 "general_operand")
-             (match_operand:SI 2 "general_operand")))]
+             (match_operand:SI 2 "general_operand")))
+        (use (match_operand 3 "" ""))
+         (use (reg:SI PIC_REG))]
   ""
   {
     rtx dest = XEXP (operands[1], 0);
 
-    if (! rx_call_operand (dest, Pmode))
-      dest = force_reg (Pmode, dest);
-    emit_call_insn (gen_call_value_internal (operands[0], dest));
+    if (!TARGET_FDPIC ||
+       (SYMBOL_REF_P(dest) && SYMBOL_REF_LOCAL_P(dest)))
+      {
+        if (! rx_call_operand (dest, Pmode))
+          dest = force_reg (Pmode, dest);
+        emit_call_insn (gen_call_value_internal (operands[0], dest));
+      }
+    else
+      {
+        emit_call_insn (gen_call_value_fdpic(operands[0], operands[1], 
operands[2]));
+      }
     DONE;
   }
 )
@@ -468,7 +497,8 @@
   [(set (match_operand                  0 "register_operand" "=r,r")
        (call (mem:QI (match_operand:SI 1 "rx_call_operand"   
"r,CALL_OP_SYMBOL_REF"))
              (const_int 0)))
-   (clobber (reg:CC CC_REG))]
+   (clobber (reg:CC CC_REG))
+   (use (reg:SI PIC_REG))]
   ""
   "@
   jsr\t%1
@@ -574,10 +604,66 @@
        (match_operand:register_modes 1 "general_operand"))]
   ""
   {
-    operands[0] = rx_maybe_pidify_operand (operands[0], 0);
-    operands[1] = rx_maybe_pidify_operand (operands[1], 0);
-    if (MEM_P (operands[0]) && GET_CODE (operands[1]) == PLUS)
-      operands[1] = force_reg (<register_modes:MODE>mode, operands[1]);
+    if (MEM_P (operands[0]) && MEM_P (operands[1]))
+      operands[1] = copy_to_mode_reg (<register_modes:MODE>mode, operands[1]);
+    if (flag_pic)
+      {
+       if (flag_pic && rx_pcrel_label_operand (operands[1], SImode))
+         {
+           if (!register_operand (operands[0], SImode))
+             operands[0] = force_reg (SImode, operands[0]);
+
+           emit_insn (gen_movsi_pcrel (operands[0], operands[1]));
+           DONE;
+         }
+
+       operands[0] = rx_mov_pic_operands(operands[0]);
+       rtx src = operands[1];
+       if (CONSTANT_P (src))
+         {
+           rtx inner = (GET_CODE (src) == CONST) ? XEXP (src, 0) : src;
+           if (GET_CODE (inner) == UNSPEC && XINT (inner, 1) == UNSPEC_PCREL)
+             {
+               emit_insn (gen_rtx_SET (operands[0], operands[1]));
+               DONE;
+             }
+         }
+       operands[1] = rx_mov_pic_operands(operands[1]);
+      }
+    else
+      {
+       operands[0] = rx_maybe_pidify_operand (operands[0], 0);
+       operands[1] = rx_maybe_pidify_operand (operands[1], 0);
+      }
+    if (GET_CODE (operands[0]) != REG
+       && GET_CODE (operands[1]) == PLUS)
+      operands[1] = copy_to_mode_reg (<register_modes:MODE>mode, operands[1]);
+    if (flag_pic)
+      {
+       if (GET_CODE (operands[1]) == CONST
+           && GET_CODE (XEXP (operands[1], 0)) == UNSPEC
+           && XINT (XEXP (operands[1], 0), 1) == UNSPEC_PCREL)
+         {
+           if (MEM_P (operands[0]))
+             {
+               rtx tmp = gen_reg_rtx (SImode);
+               emit_insn (gen_movsi_pcrel (tmp, operands[1]));
+               emit_move_insn (operands[0], tmp);
+             }
+           else
+             {
+               emit_insn(gen_movsi_pcrel(operands[0], operands[1]));
+             }
+           DONE;
+         }
+      }
+    else
+    if (GET_CODE (operands[1]) == PLUS && GET_MODE (operands[1]) == SImode)
+      {
+        emit_insn (gen_addsi3 (operands[0], XEXP (operands[1], 0), XEXP 
(operands[1], 1)));
+        DONE;
+      }
+
     if (CONST_INT_P (operand1)
         && ! rx_is_legitimate_constant (<register_modes:MODE>mode, operand1))
       FAIL;
@@ -589,12 +675,21 @@
         0 "nonimmediate_operand" "=r,r,r,r,r,r,m,Q,Q,Q,Q,r")
        (match_operand:register_modes
         1 "general_operand" 
"Int08,Sint16,Sint24,i,r,m,r,Int08,Sint16,Sint24,i,RpdaRpid"))]
-  ""
+  "!rx_is_unspec_offset(operands[1])"
   { return rx_gen_move_template (operands, false); }
   [(set_attr "length" "3,4,5,6,2,4,6,5,6,7,8,8")
    (set_attr "timings" "11,11,11,11,11,12,11,11,11,11,11,11")]
 )
 
+(define_insn "movsi_pcrel"
+  [(set (match_operand:SI 0 "register_operand" "=r")
+        (match_operand:SI 1 "rx_pcrel_label_operand" "i"))
+   (clobber (reg:CC CC_REG))]
+  "flag_pic"
+  "mvfc\tpc, %0 ! add\t%1 - . + 3, %0"
+  [(set_attr "length" "6")]
+)
+
 (define_expand "movdi"
   [(set (match_operand:DI 0 "nonimmediate_operand" "")
         (match_operand:DI 1 "general_operand" ""))]
@@ -923,11 +1018,20 @@
                 (match_operand:SI 2 "rx_source_operand" "")))
     (clobber (reg:CC CC_REG))])]
   ""
-  "
-      operands[0] = rx_maybe_pidify_operand (operands[0], 1);
-      operands[1] = rx_maybe_pidify_operand (operands[1], 1);
-      operands[2] = rx_maybe_pidify_operand (operands[2], 1);
-  "
+  {
+    if (flag_pic)
+      {
+        operands[0] = rx_mov_pic_operands(operands[0]);
+        operands[1] = rx_mov_pic_operands(operands[1]);
+        operands[2] = rx_mov_pic_operands(operands[2]);
+      }
+    else
+      {
+        operands[0] = rx_maybe_pidify_operand (operands[0], 1);
+        operands[1] = rx_maybe_pidify_operand (operands[1], 1);
+        operands[2] = rx_maybe_pidify_operand (operands[2], 1);
+      }
+  }
 )
 
 (define_insn "addsi3_internal"
@@ -982,16 +1086,29 @@
    (set_attr "length"   "2,2,2,3,4,5,6,2,3,3,4,5,6,5")]
 )
 
-(define_insn "addsi3_pid"
+(define_insn "addsi3_unspec"
   [(set (match_operand:SI 0 "register_operand" "=r")
-        (plus:SI (match_operand:SI 1 "register_operand" "%0")
-                 (const:SI (unspec:SI [(match_operand:SI 2 "immediate_operand" 
"i")] UNSPEC_PID_ADDR))))]
-  ""
-  "add\t%2, %0"
+        (plus:SI (match_operand:SI 1 "register_operand" "r")
+                 (match_operand:SI 2 "immediate_operand" "i")))]
+  "rx_is_unspec_offset(operands[2])"
+  {
+    return "add\t%2, %1, %0";
+  }
   [(set_attr "length" "6")
    (set_attr "timings" "11")]
 )
 
+;; A helper to expand the above with the CC_MODE filled in.
+(define_expand "addsi3_flags"
+  [(parallel [(set (reg:CC_ZSC CC_REG)
+                  (compare:CC_ZSC
+                    (plus:SI (match_operand:SI 1 "register_operand")
+                             (match_operand:SI 2 "rx_source_operand"))
+                    (const_int 0)))
+             (set (match_operand:SI 0 "register_operand")
+                  (plus:SI (match_dup 1) (match_dup 2)))])]
+)
+
 (define_insn "adddi3"
   [(set (match_operand:DI 0 "register_operand" "=r,r,r")
        (plus:DI (match_operand:DI 1 "register_operand"  "%0,0,0")
@@ -2802,3 +2919,204 @@
   ""
   ""
 )
+
+(define_expand "sym2GOT"
+  [(const (unspec [(match_operand 0 "" "")] UNSPEC_GOT))]
+  ""
+  "")
+
+(define_expand "symGOT2reg"
+  [(match_operand 0 "" "") (match_operand 1 "" "")]
+  ""
+  {
+    rtx mem = !can_create_pseudo_p ()
+      ? operands[0] : gen_reg_rtx (GET_MODE (operands[0]));
+    rtx gotsym = gen_sym2GOT (operands[1]);
+    rtx picreg = gen_rtx_REG (Pmode, PIC_REG);
+    PUT_MODE (gotsym, Pmode);
+
+    emit_move_insn (operands[0],
+                   gen_rtx_MEM(Pmode, gen_rtx_PLUS(Pmode, picreg, gotsym)));
+    DONE;
+  }
+)
+
+(define_expand "sym2GOTOFF"
+  [(const (unspec [(match_operand 0 "" "")] UNSPEC_GOTOFF))]
+  ""
+  "")
+
+(define_expand "symGOTOFF2reg"
+  [(match_operand 0 "" "") (match_operand 1 "" "")]
+  ""
+  {
+    rtx picreg = gen_rtx_REG (Pmode, PIC_REG);
+
+    rtx gotoffsym = gen_sym2GOTOFF (operands[1]);
+    emit_insn (gen_addsi3 (operands[0],  picreg, gotoffsym));
+    DONE;
+  }
+)
+
+(define_expand "sym2GOTFUNCDESC"
+  [(const (unspec [(match_operand 0)] UNSPEC_GOTFUNCDESC))]
+  "TARGET_FDPIC")
+
+(define_expand "sym2GOTOFFFUNCDESC"
+  [(const (unspec [(match_operand 0)] UNSPEC_GOTOFFFUNCDESC))]
+  "TARGET_FDPIC")
+
+(define_expand "symGOTFUNCDESC2reg"
+  [(match_operand 0) (match_operand 1)]
+  "TARGET_FDPIC"
+  {
+    rtx picreg = rx_get_fdpic_reg_initial_val();
+    rtx gotsym = gen_sym2GOTFUNCDESC (operands[1]);
+    PUT_MODE (gotsym, Pmode);
+    emit_move_insn (operands[0],
+                   gen_rtx_MEM(Pmode, gen_rtx_PLUS(Pmode, picreg, gotsym)));
+    DONE;
+  }
+)
+
+(define_expand "symGOTOFFFUNCDESC2reg"
+  [(match_operand 0) (match_operand 1)]
+  "TARGET_FDPIC"
+  {
+    rtx picreg = rx_get_fdpic_reg_initial_val();
+    rtx gotsym = gen_sym2GOTOFFFUNCDESC (operands[1]);
+    PUT_MODE (gotsym, Pmode);
+    emit_move_insn (operands[0], gen_rtx_PLUS(Pmode, picreg, gotsym));
+    DONE;
+  }
+)
+
+(define_expand "sym2PLT"
+  [(const (unspec [(match_operand 0 "" "")] UNSPEC_PLT))]
+  ""
+  "")
+
+(define_expand "call_fdpic"
+  [(call (match_operand:QI 0 "general_operand")
+        (match_operand:SI 1 "general_operand"))
+        (use (match_operand 2 "" ""))
+         (use (reg:SI PIC_REG))]
+  ""
+  {
+    rtx picreg = force_reg (Pmode, gen_rtx_REG(Pmode, PIC_REG));
+    rtx dest = XEXP (operands[0], 0);
+
+    if (SYMBOL_REF_P(dest) && SYMBOL_REF_LOCAL_P(dest))
+      {
+       if (! rx_call_operand (dest, Pmode))
+         dest = force_reg (Pmode, dest);
+       emit_call_insn (gen_call_internal (dest));
+      }
+    else
+      {
+       rtx savereg = gen_reg_rtx(Pmode);
+
+       emit_move_insn (savereg, picreg);
+       if (MEM_P(operands[0]) && SYMBOL_REF_P(dest))
+         {
+           dest = gen_sym2PLT(dest);
+           emit_call_insn (gen_call_internal_plt (dest));
+         }
+       else
+         {
+           rtx func = gen_reg_rtx(Pmode);
+           emit_move_insn(picreg, gen_rtx_MEM(Pmode,
+                                              plus_constant(Pmode, dest, 4)));
+           emit_move_insn(func, gen_rtx_MEM(Pmode, dest));
+           emit_call_insn (gen_call_internal_fd (func));
+         }
+       emit_move_insn (picreg, savereg);
+      }
+    DONE;
+  }
+)
+
+(define_insn "call_internal_fd"
+  [(call (mem:QI (match_operand:SI 0 "register_operand" "r"))
+        (const_int 0))
+   (clobber (reg:CC CC_REG))
+   (use (reg:SI PIC_REG))]
+  ""
+  "jsr\t%A0"
+  [(set_attr "length" "2")
+   (set_attr "timings" "33")]
+)
+
+(define_insn "call_internal_plt"
+  [(call (mem:QI (match_operand 0 "rx_plt_call_operand" ""))
+        (const_int 0))
+   (clobber (reg:CC CC_REG))
+   (use (reg:SI PIC_REG))]
+  ""
+  "bsr\t%A0"
+  [(set_attr "length" "4")
+   (set_attr "timings" "33")]
+)
+
+(define_expand "call_value_fdpic"
+  [(call (match_operand:QI 0 "general_operand")
+        (match_operand:SI 1 "general_operand"))
+        (use (match_operand 2 "" ""))
+         (use (reg:SI PIC_REG))]
+  ""
+  {
+    rtx picreg = force_reg (Pmode, gen_rtx_REG(Pmode, PIC_REG));
+    rtx dest = XEXP (operands[1], 0);
+
+    if (SYMBOL_REF_P(dest) && SYMBOL_REF_LOCAL_P(dest))
+      {
+       if (! rx_call_operand (dest, Pmode))
+         dest = force_reg (Pmode, dest);
+       emit_call_insn (gen_call_internal (dest));
+      }
+    else
+      {
+       rtx savereg = gen_reg_rtx(Pmode);
+       emit_move_insn (savereg, picreg);
+       if (MEM_P(operands[1]) && SYMBOL_REF_P(dest))
+         {
+           dest = gen_sym2PLT(dest);
+           emit_call_insn (gen_call_value_internal_plt (operands[0], dest));
+         }
+       else
+         {
+           rtx fnc = gen_reg_rtx(Pmode);
+           emit_move_insn(picreg, gen_rtx_MEM(Pmode,
+                                              plus_constant(Pmode, dest, 4)));
+           emit_move_insn(fnc, gen_rtx_MEM(Pmode, dest));
+           emit_call_insn (gen_call_value_internal_fd (operands[0], fnc));
+         }
+       emit_move_insn (picreg, savereg);
+      }
+    DONE;
+  }
+)
+
+(define_insn "call_value_internal_plt"
+  [(set (match_operand               0 "register_operand" "=r")
+       (call (mem:QI (match_operand 1 "rx_plt_call_operand" ""))
+             (const_int 0)))
+   (clobber (reg:CC CC_REG))
+   (use (reg:SI PIC_REG))]
+  ""
+  "bsr\t%A1"
+  [(set_attr "length" "4")
+   (set_attr "timings" "33")]
+)
+
+(define_insn "call_value_internal_fd"
+  [(set (match_operand                  0 "register_operand" "=r")
+       (call (mem:QI (match_operand:SI 1 "register_operand"   "r"))
+             (const_int 0)))
+   (clobber (reg:CC CC_REG))
+   (use (reg:SI PIC_REG))]
+  ""
+  "jsr\t%1"
+  [(set_attr "length" "2")
+   (set_attr "timings" "33")]
+)
-- 
2.47.3

Reply via email to