m68k-protos.h: Rename m68k_interrupt_function_p to m68k_get_function_kind.
[gcc.git] / gcc / config / m68k / m68k.c
index 642927e7d824aba477cf5cde1cdd7470d449a6c3..87665596726f734cde3704f8d8b3aad19a3995fb 100644 (file)
@@ -191,7 +191,7 @@ int m68k_last_compare_had_fp_operands;
 #undef TARGET_ASM_OUTPUT_MI_THUNK
 #define TARGET_ASM_OUTPUT_MI_THUNK m68k_output_mi_thunk
 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
-#define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
+#define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_tree_hwi_hwi_tree_true
 
 #undef TARGET_ASM_FILE_START_APP_OFF
 #define TARGET_ASM_FILE_START_APP_OFF true
@@ -223,6 +223,7 @@ static const struct attribute_spec m68k_attribute_table[] =
 {
   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
   { "interrupt_handler", 0, 0, true,  false, false, m68k_handle_fndecl_attribute },
+  { "interrupt_thread", 0, 0, true,  false, false, m68k_handle_fndecl_attribute },
   { NULL,                0, 0, false, false, false, NULL }
 };
 
@@ -633,10 +634,12 @@ m68k_cpp_cpu_family (const char *prefix)
   return concat ("__m", prefix, "_family_", m68k_cpu_entry->family, NULL);
 }
 \f
-/* Return nonzero if FUNC is an interrupt function as specified by the
-   "interrupt_handler" attribute.  */
-bool
-m68k_interrupt_function_p (tree func)
+/* Return m68k_fk_interrupt_handler if FUNC has an "interrupt_handler"
+   attribute and interrupt_thread if FUNC has an "interrupt_thread"
+   attribute.  Otherwise, return m68k_fk_normal_function.  */
+
+enum m68k_function_kind
+m68k_get_function_kind (tree func)
 {
   tree a;
 
@@ -644,7 +647,14 @@ m68k_interrupt_function_p (tree func)
     return false;
 
   a = lookup_attribute ("interrupt_handler", DECL_ATTRIBUTES (func));
-  return (a != NULL_TREE);
+  if (a != NULL_TREE)
+    return m68k_fk_interrupt_handler;
+
+  a = lookup_attribute ("interrupt_thread", DECL_ATTRIBUTES (func));
+  if (a != NULL_TREE)
+    return m68k_fk_interrupt_thread;
+
+  return m68k_fk_normal_function;
 }
 
 /* Handle an attribute requiring a FUNCTION_DECL; arguments as in
@@ -662,6 +672,19 @@ m68k_handle_fndecl_attribute (tree *node, tree name,
       *no_add_attrs = true;
     }
 
+  if (m68k_get_function_kind (*node) != m68k_fk_normal_function)
+    {
+      error ("multiple interrupt attributes not allowed");
+      *no_add_attrs = true;
+    }
+
+  if (!TARGET_FIDOA
+      && !strcmp (IDENTIFIER_POINTER (name), "interrupt_thread"))
+    {
+      error ("interrupt_thread is available only on fido");
+      *no_add_attrs = true;
+    }
+
   return NULL_TREE;
 }
 
@@ -670,7 +693,10 @@ m68k_compute_frame_layout (void)
 {
   int regno, saved;
   unsigned int mask;
-  bool interrupt_handler = m68k_interrupt_function_p (current_function_decl);
+  enum m68k_function_kind func_kind =
+    m68k_get_function_kind (current_function_decl);
+  bool interrupt_handler = func_kind == m68k_fk_interrupt_handler;
+  bool interrupt_thread = func_kind == m68k_fk_interrupt_thread;
 
   /* Only compute the frame once per function.
      Don't cache information until reload has been completed.  */
@@ -681,12 +707,15 @@ m68k_compute_frame_layout (void)
   current_frame.size = (get_frame_size () + 3) & -4;
 
   mask = saved = 0;
-  for (regno = 0; regno < 16; regno++)
-    if (m68k_save_reg (regno, interrupt_handler))
-      {
-       mask |= 1 << (regno - D0_REG);
-       saved++;
-      }
+
+  /* Interrupt thread does not need to save any register.  */
+  if (!interrupt_thread)
+    for (regno = 0; regno < 16; regno++)
+      if (m68k_save_reg (regno, interrupt_handler))
+       {
+         mask |= 1 << (regno - D0_REG);
+         saved++;
+       }
   current_frame.offset = saved * 4;
   current_frame.reg_no = saved;
   current_frame.reg_mask = mask;
@@ -695,12 +724,14 @@ m68k_compute_frame_layout (void)
   mask = saved = 0;
   if (TARGET_HARD_FLOAT)
     {
-      for (regno = 16; regno < 24; regno++)
-       if (m68k_save_reg (regno, interrupt_handler))
-         {
-           mask |= 1 << (regno - FP0_REG);
-           saved++;
-         }
+      /* Interrupt thread does not need to save any register.  */
+      if (!interrupt_thread)
+       for (regno = 16; regno < 24; regno++)
+         if (m68k_save_reg (regno, interrupt_handler))
+           {
+             mask |= 1 << (regno - FP0_REG);
+             saved++;
+           }
       current_frame.foffset = saved * TARGET_FP_REG_SIZE;
       current_frame.offset += current_frame.foffset;
     }
@@ -745,7 +776,7 @@ m68k_initial_elimination_offset (int from, int to)
 static bool
 m68k_save_reg (unsigned int regno, bool interrupt_handler)
 {
-  if (flag_pic && regno == PIC_OFFSET_TABLE_REGNUM)
+  if (flag_pic && regno == PIC_REG)
     {
       /* A function that receives a nonlocal goto must save all call-saved
         registers.  */
@@ -1550,6 +1581,27 @@ output_btst (rtx *operands, rtx countop, rtx dataop, rtx insn, int signpos)
       if (count == 7
          && next_insn_tests_no_inequality (insn))
        return "tst%.b %1";
+      /* Try to use `movew to ccr' followed by the appropriate branch insn.
+         On some m68k variants unfortunately that's slower than btst.
+         On 68000 and higher, that should also work for all HImode operands. */
+      if (TUNE_CPU32 || TARGET_COLDFIRE || optimize_size)
+       {
+         if (count == 3 && DATA_REG_P (operands[1])
+             && next_insn_tests_no_inequality (insn))
+           {
+           cc_status.flags = CC_NOT_NEGATIVE | CC_Z_IN_NOT_N | CC_NO_OVERFLOW;
+           return "move%.w %1,%%ccr";
+           }
+         if (count == 2 && DATA_REG_P (operands[1])
+             && next_insn_tests_no_inequality (insn))
+           {
+           cc_status.flags = CC_NOT_NEGATIVE | CC_INVERTED | CC_NO_OVERFLOW;
+           return "move%.w %1,%%ccr";
+           }
+         /* count == 1 followed by bvc/bvs and
+            count == 0 followed by bcc/bcs are also possible, but need
+            m68k-specific CC_Z_IN_NOT_V and CC_Z_IN_NOT_C flags. */
+       }
 
       cc_status.flags = CC_NOT_NEGATIVE;
     }
@@ -3732,7 +3784,7 @@ print_operand_address (FILE *file, rtx addr)
       int labelno;
 
       /* If ADDR is a (d8,pc,Xn) address, this is the number of the
-        label being acceesed, otherwise it is -1.  */
+        label being accessed, otherwise it is -1.  */
       labelno = (address.offset
                 && !address.base
                 && GET_CODE (address.offset) == LABEL_REF
@@ -4083,59 +4135,92 @@ m68k_coff_asm_named_section (const char *name, unsigned int flags,
 
 static void
 m68k_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
-                     HOST_WIDE_INT delta,
-                     HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
+                     HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
                      tree function)
 {
-  rtx xops[1];
-  const char *fmt;
-
-  if (delta > 0 && delta <= 8)
-    asm_fprintf (file, (MOTOROLA
-                       ? "\taddq.l %I%d,4(%Rsp)\n"
-                       : "\taddql %I%d,%Rsp@(4)\n"),
-                (int) delta);
-  else if (delta < 0 && delta >= -8)
-    asm_fprintf (file, (MOTOROLA
-                       ? "\tsubq.l %I%d,4(%Rsp)\n"
-                       : "\tsubql %I%d,%Rsp@(4)\n"),
-                (int) -delta);
-  else if (TARGET_COLDFIRE)
+  rtx this_slot, offset, addr, mem, insn;
+
+  /* Pretend to be a post-reload pass while generating rtl.  */
+  no_new_pseudos = 1;
+  reload_completed = 1;
+  allocate_reg_info (FIRST_PSEUDO_REGISTER, true, true);
+
+  /* The "this" pointer is stored at 4(%sp).  */
+  this_slot = gen_rtx_MEM (Pmode, plus_constant (stack_pointer_rtx, 4));
+
+  /* Add DELTA to THIS.  */
+  if (delta != 0)
     {
-      /* ColdFire can't add/sub a constant to memory unless it is in
-        the range of addq/subq.  So load the value into %d0 and
-        then add it to 4(%sp). */
-      if (delta >= -128 && delta <= 127)
-       asm_fprintf (file, (MOTOROLA
-                           ? "\tmoveq.l %I%wd,%Rd0\n"
-                           : "\tmoveql %I%wd,%Rd0\n"),
-                    delta);
-      else
-       asm_fprintf (file, (MOTOROLA
-                           ? "\tmove.l %I%wd,%Rd0\n"
-                           : "\tmovel %I%wd,%Rd0\n"),
-                    delta);
-      asm_fprintf (file, (MOTOROLA
-                         ? "\tadd.l %Rd0,4(%Rsp)\n"
-                         : "\taddl %Rd0,%Rsp@(4)\n"));
+      /* Make the offset a legitimate operand for memory addition.  */
+      offset = GEN_INT (delta);
+      if ((delta < -8 || delta > 8)
+         && (TARGET_COLDFIRE || USE_MOVQ (delta)))
+       {
+         emit_move_insn (gen_rtx_REG (Pmode, D0_REG), offset);
+         offset = gen_rtx_REG (Pmode, D0_REG);
+       }
+      emit_insn (gen_add3_insn (copy_rtx (this_slot),
+                               copy_rtx (this_slot), offset));
     }
-  else
-    asm_fprintf (file, (MOTOROLA
-                       ? "\tadd.l %I%wd,4(%Rsp)\n"
-                       : "\taddl %I%wd,%Rsp@(4)\n"),
-                delta);
 
-  xops[0] = DECL_RTL (function);
+  /* If needed, add *(*THIS + VCALL_OFFSET) to THIS.  */
+  if (vcall_offset != 0)
+    {
+      /* Set the static chain register to *THIS.  */
+      emit_move_insn (static_chain_rtx, this_slot);
+      emit_move_insn (static_chain_rtx, gen_rtx_MEM (Pmode, static_chain_rtx));
 
-  gcc_assert (MEM_P (xops[0])
-             && symbolic_operand (XEXP (xops[0], 0), VOIDmode));
-  xops[0] = XEXP (xops[0], 0);
+      /* Set ADDR to a legitimate address for *THIS + VCALL_OFFSET.  */
+      addr = plus_constant (static_chain_rtx, vcall_offset);
+      if (!m68k_legitimate_address_p (Pmode, addr, true))
+       {
+         emit_insn (gen_rtx_SET (VOIDmode, static_chain_rtx, addr));
+         addr = static_chain_rtx;
+       }
 
-  fmt = m68k_symbolic_jump;
-  if (m68k_symbolic_jump == NULL)
-    fmt = "move.l %%a1@GOT(%%a5), %%a1\n\tjmp (%%a1)";
+      /* Load the offset into %d0 and add it to THIS.  */
+      emit_move_insn (gen_rtx_REG (Pmode, D0_REG),
+                     gen_rtx_MEM (Pmode, addr));
+      emit_insn (gen_add3_insn (copy_rtx (this_slot),
+                               copy_rtx (this_slot),
+                               gen_rtx_REG (Pmode, D0_REG)));
+    }
 
-  output_asm_insn (fmt, xops);
+  /* Jump to the target function.  Use a sibcall if direct jumps are
+     allowed, otherwise load the address into a register first.  */
+  mem = DECL_RTL (function);
+  if (!sibcall_operand (XEXP (mem, 0), VOIDmode))
+    {
+      gcc_assert (flag_pic);
+
+      if (!TARGET_SEP_DATA)
+       {
+         /* Use the static chain register as a temporary (call-clobbered)
+            GOT pointer for this function.  We can use the static chain
+            register because it isn't live on entry to the thunk.  */
+         REGNO (pic_offset_table_rtx) = STATIC_CHAIN_REGNUM;
+         emit_insn (gen_load_got (pic_offset_table_rtx));
+       }
+      legitimize_pic_address (XEXP (mem, 0), Pmode, static_chain_rtx);
+      mem = replace_equiv_address (mem, static_chain_rtx);
+    }
+  insn = emit_call_insn (gen_sibcall (mem, const0_rtx));
+  SIBLING_CALL_P (insn) = 1;
+
+  /* Run just enough of rest_of_compilation.  */
+  insn = get_insns ();
+  split_all_insns_noflow ();
+  final_start_function (insn, file, 1);
+  final (insn, file, 1);
+  final_end_function ();
+
+  /* Clean up the vars set above.  */
+  reload_completed = 0;
+  no_new_pseudos = 0;
+
+  /* Restore the original PIC register.  */
+  if (flag_pic)
+    REGNO (pic_offset_table_rtx) = PIC_REG;
 }
 
 /* Worker function for TARGET_STRUCT_VALUE_RTX.  */
@@ -4157,7 +4242,8 @@ m68k_hard_regno_rename_ok (unsigned int old_reg ATTRIBUTE_UNUSED,
      saved by the prologue, even if they would normally be
      call-clobbered.  */
 
-  if (m68k_interrupt_function_p (current_function_decl)
+  if ((m68k_get_function_kind (current_function_decl)
+       == m68k_fk_interrupt_handler)
       && !regs_ever_live[new_reg])
     return 0;
 
@@ -4268,12 +4354,12 @@ m68k_libcall_value (enum machine_mode mode)
   case DFmode:
   case XFmode:
     if (TARGET_68881)
-      return gen_rtx_REG (mode, 16);
+      return gen_rtx_REG (mode, FP0_REG);
     break;
   default:
     break;
   }
-  return gen_rtx_REG (mode, 0);
+  return gen_rtx_REG (mode, D0_REG);
 }
 
 rtx
@@ -4287,7 +4373,7 @@ m68k_function_value (tree valtype, tree func ATTRIBUTE_UNUSED)
   case DFmode:
   case XFmode:
     if (TARGET_68881)
-      return gen_rtx_REG (mode, 16);
+      return gen_rtx_REG (mode, FP0_REG);
     break;
   default:
     break;