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