| 1 |
/* PRU target specific passes |
| 2 |
Copyright (C) 2017-2025 Free Software Foundation, Inc. |
| 3 |
Dimitar Dimitrov <[email protected]> |
| 4 |
|
| 5 |
This file is part of GCC. |
| 6 |
|
| 7 |
GCC is free software; you can redistribute it and/or modify it |
| 8 |
under the terms of the GNU General Public License as published |
| 9 |
by the Free Software Foundation; either version 3, or (at your |
| 10 |
option) any later version. |
| 11 |
|
| 12 |
GCC is distributed in the hope that it will be useful, but WITHOUT |
| 13 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 14 |
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public |
| 15 |
License for more details. |
| 16 |
|
| 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/>. */ |
| 20 |
|
| 21 |
#define IN_TARGET_CODE 1 |
| 22 |
|
| 23 |
#include "config.h" |
| 24 |
#include "system.h" |
| 25 |
#include "coretypes.h" |
| 26 |
#include "backend.h" |
| 27 |
#include "context.h" |
| 28 |
#include "tm.h" |
| 29 |
#include "alias.h" |
| 30 |
#include "symtab.h" |
| 31 |
#include "tree.h" |
| 32 |
#include "diagnostic-core.h" |
| 33 |
#include "function.h" |
| 34 |
#include "gimple.h" |
| 35 |
#include "gimple-iterator.h" |
| 36 |
#include "gimple-walk.h" |
| 37 |
#include "gimple-expr.h" |
| 38 |
#include "tree-pass.h" |
| 39 |
|
| 40 |
#include "pru-protos.h" |
| 41 |
|
| 42 |
namespace { |
| 43 |
|
| 44 |
/* Scan the tree to ensure that the compiled code by GCC |
| 45 |
conforms to the TI ABI specification. If GCC cannot |
| 46 |
output a conforming code, raise an error. */ |
| 47 |
const pass_data pass_data_pru_tiabi_check = |
| 48 |
{ |
| 49 |
GIMPLE_PASS, /* type */ |
| 50 |
"*pru_tiabi_check", /* name */ |
| 51 |
OPTGROUP_NONE, /* optinfo_flags */ |
| 52 |
TV_NONE, /* tv_id */ |
| 53 |
PROP_gimple_any, /* properties_required */ |
| 54 |
0, /* properties_provided */ |
| 55 |
0, /* properties_destroyed */ |
| 56 |
0, /* todo_flags_start */ |
| 57 |
0, /* todo_flags_finish */ |
| 58 |
}; |
| 59 |
|
| 60 |
/* Implementation class for the TI ABI compliance-check pass. */ |
| 61 |
class pass_pru_tiabi_check : public gimple_opt_pass |
| 62 |
{ |
| 63 |
public: |
| 64 |
pass_pru_tiabi_check (gcc::context *ctxt) |
| 65 |
: gimple_opt_pass (pass_data_pru_tiabi_check, ctxt) |
| 66 |
{} |
| 67 |
|
| 68 |
/* opt_pass methods: */ |
| 69 |
virtual unsigned int execute (function *); |
| 70 |
|
| 71 |
virtual bool gate (function *) |
| 72 |
{ |
| 73 |
return pru_current_abi == PRU_ABI_TI; |
| 74 |
} |
| 75 |
|
| 76 |
}; // class pass_pru_tiabi_check |
| 77 |
|
| 78 |
/* Return 1 if type TYPE is a pointer to function type or a |
| 79 |
structure having a pointer to function type as one of its fields. |
| 80 |
Otherwise return 0. */ |
| 81 |
static bool |
| 82 |
chkp_type_has_function_pointer (const_tree type) |
| 83 |
{ |
| 84 |
bool res = false; |
| 85 |
|
| 86 |
if (POINTER_TYPE_P (type) && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (type))) |
| 87 |
res = true; |
| 88 |
else if (RECORD_OR_UNION_TYPE_P (type)) |
| 89 |
{ |
| 90 |
tree field; |
| 91 |
|
| 92 |
for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) |
| 93 |
if (TREE_CODE (field) == FIELD_DECL) |
| 94 |
res = res || chkp_type_has_function_pointer (TREE_TYPE (field)); |
| 95 |
} |
| 96 |
else if (TREE_CODE (type) == ARRAY_TYPE) |
| 97 |
res = chkp_type_has_function_pointer (TREE_TYPE (type)); |
| 98 |
|
| 99 |
return res; |
| 100 |
} |
| 101 |
|
| 102 |
/* Check the function declaration FNTYPE for TI ABI compatibility. */ |
| 103 |
static void |
| 104 |
chk_function_decl (const_tree fntype, location_t call_location) |
| 105 |
{ |
| 106 |
/* GCC does not check if the RETURN VALUE pointer is NULL, |
| 107 |
so do not allow GCC functions with large return values. */ |
| 108 |
if (!VOID_TYPE_P (TREE_TYPE (fntype)) |
| 109 |
&& pru_return_in_memory (TREE_TYPE (fntype), fntype)) |
| 110 |
error_at (call_location, |
| 111 |
"large return values not supported with %<-mabi=ti%> option"); |
| 112 |
|
| 113 |
/* Check this function's arguments. */ |
| 114 |
for (tree p = TYPE_ARG_TYPES (fntype); p; p = TREE_CHAIN (p)) |
| 115 |
{ |
| 116 |
tree arg_type = TREE_VALUE (p); |
| 117 |
if (chkp_type_has_function_pointer (arg_type)) |
| 118 |
error_at (call_location, |
| 119 |
"function pointers not supported with %<-mabi=ti%> option"); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
/* Callback for walk_gimple_seq that checks TP tree for TI ABI compliance. */ |
| 124 |
static tree |
| 125 |
check_op_callback (tree *tp, int *walk_subtrees, void *data) |
| 126 |
{ |
| 127 |
struct walk_stmt_info *wi = (struct walk_stmt_info *) data; |
| 128 |
|
| 129 |
if (RECORD_OR_UNION_TYPE_P (*tp) || TREE_CODE (*tp) == ENUMERAL_TYPE) |
| 130 |
{ |
| 131 |
/* Forward declarations have NULL tree type. Skip them. */ |
| 132 |
if (TREE_TYPE (*tp) == NULL) |
| 133 |
return NULL; |
| 134 |
} |
| 135 |
|
| 136 |
/* TODO - why C++ leaves INTEGER_TYPE forward declarations around? */ |
| 137 |
if (TREE_TYPE (*tp) == NULL) |
| 138 |
return NULL; |
| 139 |
|
| 140 |
const tree type = TREE_TYPE (*tp); |
| 141 |
|
| 142 |
/* Direct function calls are allowed, obviously. */ |
| 143 |
gcall *call = dyn_cast <gcall *> (gsi_stmt (wi->gsi)); |
| 144 |
if (call |
| 145 |
&& tp == gimple_call_fn_ptr (call) |
| 146 |
&& gimple_call_fndecl (call)) |
| 147 |
return NULL; |
| 148 |
|
| 149 |
switch (TREE_CODE (type)) |
| 150 |
{ |
| 151 |
case FUNCTION_TYPE: |
| 152 |
case METHOD_TYPE: |
| 153 |
{ |
| 154 |
/* Note: Do not enforce a small return value. It is safe to |
| 155 |
call any TI ABI function from GCC, since GCC will |
| 156 |
never pass NULL. */ |
| 157 |
|
| 158 |
/* Check arguments for function pointers. */ |
| 159 |
for (tree p = TYPE_ARG_TYPES (type); p; p = TREE_CHAIN (p)) |
| 160 |
{ |
| 161 |
tree arg_type = TREE_VALUE (p); |
| 162 |
if (chkp_type_has_function_pointer (arg_type)) |
| 163 |
error_at (gimple_location (wi->stmt), "function pointers " |
| 164 |
"not supported with %<-mabi=ti%> option"); |
| 165 |
} |
| 166 |
break; |
| 167 |
} |
| 168 |
case RECORD_TYPE: |
| 169 |
case UNION_TYPE: |
| 170 |
case QUAL_UNION_TYPE: |
| 171 |
case POINTER_TYPE: |
| 172 |
{ |
| 173 |
if (chkp_type_has_function_pointer (type)) |
| 174 |
{ |
| 175 |
error_at (gimple_location (wi->stmt), |
| 176 |
"function pointers not supported with " |
| 177 |
"%<-mabi=ti%> option"); |
| 178 |
*walk_subtrees = false; |
| 179 |
} |
| 180 |
break; |
| 181 |
} |
| 182 |
default: |
| 183 |
break; |
| 184 |
} |
| 185 |
return NULL; |
| 186 |
} |
| 187 |
|
| 188 |
/* Pass implementation. */ |
| 189 |
unsigned |
| 190 |
pass_pru_tiabi_check::execute (function *fun) |
| 191 |
{ |
| 192 |
struct walk_stmt_info wi; |
| 193 |
const_tree fntype = TREE_TYPE (fun->decl); |
| 194 |
|
| 195 |
gimple_seq body = gimple_body (current_function_decl); |
| 196 |
|
| 197 |
memset (&wi, 0, sizeof (wi)); |
| 198 |
wi.info = NULL; |
| 199 |
wi.want_locations = true; |
| 200 |
|
| 201 |
/* Check the function body. */ |
| 202 |
walk_gimple_seq (body, NULL, check_op_callback, &wi); |
| 203 |
|
| 204 |
/* Check the function declaration. */ |
| 205 |
chk_function_decl (fntype, fun->function_start_locus); |
| 206 |
|
| 207 |
return 0; |
| 208 |
} |
| 209 |
|
| 210 |
} // anon namespace |
| 211 |
|
| 212 |
gimple_opt_pass * |
| 213 |
make_pru_tiabi_check (gcc::context *ctxt) |
| 214 |
{ |
| 215 |
return new pass_pru_tiabi_check (ctxt); |
| 216 |
} |
| 217 |
|
| 218 |
namespace { |
| 219 |
|
| 220 |
/* Scan the tree to ensure that the compiled code by GCC |
| 221 |
conforms to the non-standard minimal runtime. */ |
| 222 |
const pass_data pass_data_pru_minrt_check = |
| 223 |
{ |
| 224 |
GIMPLE_PASS, /* type */ |
| 225 |
"*pru_minrt_check", /* name */ |
| 226 |
OPTGROUP_NONE, /* optinfo_flags */ |
| 227 |
TV_NONE, /* tv_id */ |
| 228 |
PROP_gimple_any, /* properties_required */ |
| 229 |
0, /* properties_provided */ |
| 230 |
0, /* properties_destroyed */ |
| 231 |
0, /* todo_flags_start */ |
| 232 |
0, /* todo_flags_finish */ |
| 233 |
}; |
| 234 |
|
| 235 |
/* Implementation class for the minrt compliance-check pass. */ |
| 236 |
class pass_pru_minrt_check : public gimple_opt_pass |
| 237 |
{ |
| 238 |
public: |
| 239 |
pass_pru_minrt_check (gcc::context *ctxt) |
| 240 |
: gimple_opt_pass (pass_data_pru_minrt_check, ctxt) |
| 241 |
{} |
| 242 |
|
| 243 |
/* opt_pass methods: */ |
| 244 |
virtual unsigned int execute (function *); |
| 245 |
|
| 246 |
virtual bool gate (function *) |
| 247 |
{ |
| 248 |
return TARGET_MINRT; |
| 249 |
} |
| 250 |
|
| 251 |
}; // class pass_pru_minrt_check |
| 252 |
|
| 253 |
/* Pass implementation. */ |
| 254 |
unsigned |
| 255 |
pass_pru_minrt_check::execute (function *fun) |
| 256 |
{ |
| 257 |
const_tree fntype = TREE_TYPE (fun->decl); |
| 258 |
|
| 259 |
if (id_equal (DECL_NAME (fun->decl), "main")) |
| 260 |
{ |
| 261 |
/* Argument list always ends with VOID_TYPE, so subtract one |
| 262 |
to get the number of function arguments. */ |
| 263 |
const unsigned num_args = list_length (TYPE_ARG_TYPES (fntype)) - 1; |
| 264 |
|
| 265 |
if (num_args != 0) |
| 266 |
error_at (DECL_SOURCE_LOCATION (fun->decl), "function %<main%> " |
| 267 |
"must have no arguments when using the " |
| 268 |
"%<-minrt%> option"); |
| 269 |
|
| 270 |
/* The required CFG analysis to detect when a functions would never |
| 271 |
return is available only with -O1 and higher. */ |
| 272 |
if (optimize >= 1 && !TREE_THIS_VOLATILE (fun->decl)) |
| 273 |
error_at (DECL_SOURCE_LOCATION (fun->decl), "function %<main%> " |
| 274 |
"must never return when using the " |
| 275 |
"%<-minrt%> option"); |
| 276 |
} |
| 277 |
return 0; |
| 278 |
} |
| 279 |
|
| 280 |
} // anon namespace |
| 281 |
|
| 282 |
gimple_opt_pass * |
| 283 |
make_pru_minrt_check (gcc::context *ctxt) |
| 284 |
{ |
| 285 |
return new pass_pru_minrt_check (ctxt); |
| 286 |
} |