Home | History | Annotate | Line # | Download | only in spu
spu-c.c revision 1.8
      1 /* Copyright (C) 2006-2017 Free Software Foundation, Inc.
      2 
      3    This file is free software; you can redistribute it and/or modify it under
      4    the terms of the GNU General Public License as published by the Free
      5    Software Foundation; either version 3 of the License, or (at your option)
      6    any later version.
      7 
      8    This file is distributed in the hope that it will be useful, but WITHOUT
      9    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
     10    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
     11    for more details.
     12 
     13    You should have received a copy of the GNU General Public License
     14    along with GCC; see the file COPYING3.  If not see
     15    <http://www.gnu.org/licenses/>.  */
     16 
     17 #include "config.h"
     18 #include "system.h"
     19 #include "coretypes.h"
     20 #include "target.h"
     21 #include "c-family/c-common.h"
     22 #include "stringpool.h"
     23 #include "langhooks.h"
     24 
     25 
     27 /* Keep the vector keywords handy for fast comparisons.  */
     28 static GTY(()) tree __vector_keyword;
     29 static GTY(()) tree vector_keyword;
     30 
     31 static cpp_hashnode *
     32 spu_categorize_keyword (const cpp_token *tok)
     33 {
     34   if (tok->type == CPP_NAME)
     35     {
     36       cpp_hashnode *ident = tok->val.node.node;
     37 
     38       if (ident == C_CPP_HASHNODE (vector_keyword)
     39 	  || ident == C_CPP_HASHNODE (__vector_keyword))
     40 	return C_CPP_HASHNODE (__vector_keyword);
     41       else
     42 	return ident;
     43     }
     44   return 0;
     45 }
     46 
     47 /* Called to decide whether a conditional macro should be expanded.
     48    Since we have exactly one such macro (i.e, 'vector'), we do not
     49    need to examine the 'tok' parameter.  */
     50 
     51 static cpp_hashnode *
     52 spu_macro_to_expand (cpp_reader *pfile, const cpp_token *tok)
     53 {
     54   cpp_hashnode *expand_this = tok->val.node.node;
     55   cpp_hashnode *ident;
     56 
     57   ident = spu_categorize_keyword (tok);
     58   if (ident == C_CPP_HASHNODE (__vector_keyword))
     59     {
     60       tok = cpp_peek_token (pfile, 0);
     61       ident = spu_categorize_keyword (tok);
     62 
     63       if (ident)
     64 	{
     65 	  enum rid rid_code = (enum rid)(ident->rid_code);
     66 	  if (ident->type == NT_MACRO)
     67 	    {
     68 	      (void) cpp_get_token (pfile);
     69 	      tok = cpp_peek_token (pfile, 0);
     70 	      ident = spu_categorize_keyword (tok);
     71 	      if (ident)
     72 		rid_code = (enum rid)(ident->rid_code);
     73 	    }
     74 
     75 	  if (rid_code == RID_UNSIGNED || rid_code == RID_LONG
     76 	      || rid_code == RID_SHORT || rid_code == RID_SIGNED
     77 	      || rid_code == RID_INT || rid_code == RID_CHAR
     78 	      || rid_code == RID_FLOAT || rid_code == RID_DOUBLE)
     79 	    expand_this = C_CPP_HASHNODE (__vector_keyword);
     80 	}
     81     }
     82   return expand_this;
     83 }
     84 
     85 /* target hook for resolve_overloaded_builtin(). Returns a function call
     86    RTX if we can resolve the overloaded builtin */
     87 tree
     88 spu_resolve_overloaded_builtin (location_t loc, tree fndecl, void *passed_args)
     89 {
     90 #define SCALAR_TYPE_P(t) (INTEGRAL_TYPE_P (t) \
     91 			  || SCALAR_FLOAT_TYPE_P (t) \
     92 			  || POINTER_TYPE_P (t))
     93   vec<tree, va_gc> *fnargs = static_cast <vec<tree, va_gc> *> (passed_args);
     94   unsigned int nargs = vec_safe_length (fnargs);
     95   int new_fcode, fcode = DECL_FUNCTION_CODE (fndecl);
     96   struct spu_builtin_description *desc;
     97   tree match = NULL_TREE;
     98 
     99   /* The vector types are not available if the backend is not initialized.  */
    100   gcc_assert (!flag_preprocess_only);
    101 
    102   desc = &spu_builtins[fcode];
    103   if (desc->type != B_OVERLOAD)
    104     return NULL_TREE;
    105 
    106   /* Compare the signature of each internal builtin function with the
    107      function arguments until a match is found. */
    108 
    109   for (new_fcode = fcode + 1; spu_builtins[new_fcode].type == B_INTERNAL;
    110        new_fcode++)
    111     {
    112       tree decl = targetm.builtin_decl (new_fcode, true);
    113       tree params = TYPE_ARG_TYPES (TREE_TYPE (decl));
    114       tree param;
    115       bool all_scalar;
    116       unsigned int p;
    117 
    118       /* Check whether all parameters are scalar.  */
    119       all_scalar = true;
    120       for (param = params; param != void_list_node; param = TREE_CHAIN (param))
    121       if (!SCALAR_TYPE_P (TREE_VALUE (param)))
    122 	all_scalar = false;
    123 
    124       for (param = params, p = 0;
    125 	   param != void_list_node;
    126 	   param = TREE_CHAIN (param), p++)
    127 	{
    128 	  tree var, arg_type, param_type = TREE_VALUE (param);
    129 
    130 	  if (p >= nargs)
    131 	    {
    132 	      error ("insufficient arguments to overloaded function %s",
    133 		     desc->name);
    134 	      return error_mark_node;
    135 	    }
    136 
    137 	  var = (*fnargs)[p];
    138 
    139 	  if (TREE_CODE (var) == NON_LVALUE_EXPR)
    140 	    var = TREE_OPERAND (var, 0);
    141 
    142 	  if (TREE_CODE (var) == ERROR_MARK)
    143 	    return NULL_TREE;	/* Let somebody else deal with the problem. */
    144 
    145 	  arg_type = TREE_TYPE (var);
    146 
    147 	  /* The intrinsics spec does not specify precisely how to
    148 	     resolve generic intrinsics.  We require an exact match
    149 	     for vector types and let C do it's usual parameter type
    150 	     checking/promotions for scalar arguments, except for the
    151 	     first argument of intrinsics which don't have a vector
    152 	     parameter. */
    153 	  if ((!SCALAR_TYPE_P (param_type)
    154 	       || !SCALAR_TYPE_P (arg_type)
    155 	       || (all_scalar && p == 0))
    156 	      && !lang_hooks.types_compatible_p (param_type, arg_type))
    157 	    break;
    158 	}
    159       if (param == void_list_node)
    160 	{
    161 	  if (p != nargs)
    162 	    {
    163 	      error ("too many arguments to overloaded function %s",
    164 		     desc->name);
    165 	      return error_mark_node;
    166 	    }
    167 
    168 	  match = decl;
    169 	  break;
    170 	}
    171     }
    172 
    173   if (match == NULL_TREE)
    174     {
    175       error ("parameter list does not match a valid signature for %s()",
    176 	     desc->name);
    177       return error_mark_node;
    178     }
    179 
    180   return build_function_call_vec (loc, vNULL, match, fnargs, NULL);
    181 #undef SCALAR_TYPE_P
    182 }
    183 
    184 
    185 void
    186 spu_cpu_cpp_builtins (struct cpp_reader *pfile)
    187 {
    188   cpp_define (pfile, "__SPU__");
    189   cpp_assert (pfile, "cpu=spu");
    190   cpp_assert (pfile, "machine=spu");
    191   if (spu_arch == PROCESSOR_CELLEDP)
    192     cpp_define (pfile, "__SPU_EDP__");
    193   if (cpp_get_options (pfile)->lang != CLK_ASM)
    194     cpp_define (pfile, "__vector=__attribute__((__spu_vector__))");
    195   switch (spu_ea_model)
    196     {
    197     case 32:
    198       cpp_define (pfile, "__EA32__");
    199       break;
    200     case 64:
    201       cpp_define (pfile, "__EA64__");
    202       break;
    203     default:
    204        gcc_unreachable ();
    205     }
    206 
    207   if (!flag_iso && cpp_get_options (pfile)->lang != CLK_ASM)
    208     {
    209       /* Define this when supporting context-sensitive keywords.  */
    210       cpp_define (pfile, "__VECTOR_KEYWORD_SUPPORTED__");
    211       cpp_define (pfile, "vector=vector");
    212 
    213       /* Initialize vector keywords.  */
    214       __vector_keyword = get_identifier ("__vector");
    215       C_CPP_HASHNODE (__vector_keyword)->flags |= NODE_CONDITIONAL;
    216       vector_keyword = get_identifier ("vector");
    217       C_CPP_HASHNODE (vector_keyword)->flags |= NODE_CONDITIONAL;
    218 
    219       /* Enable context-sensitive macros.  */
    220       cpp_get_callbacks (pfile)->macro_to_expand = spu_macro_to_expand;
    221     }
    222 }
    223 
    224 void
    225 spu_c_common_override_options (void)
    226 {
    227   if (!TARGET_STD_MAIN)
    228     {
    229       /* Don't give warnings about the main() function.  */
    230       warn_main = 0;
    231     }
    232 }
    233