utils.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /* GCC-StarPU
  2. Copyright (C) 2012 INRIA
  3. GCC-StarPU is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. GCC-StarPU is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with GCC-StarPU. If not, see <http://www.gnu.org/licenses/>. */
  13. #include <starpu-gcc/config.h>
  14. /* We must include starpu.h here, otherwise gcc will complain about a poisoned
  15. malloc in xmmintrin.h. */
  16. #include <starpu.h>
  17. #include <gcc-plugin.h>
  18. #include <plugin-version.h>
  19. #include <plugin.h>
  20. #include <cpplib.h>
  21. #include <tree.h>
  22. #include <tree-iterator.h>
  23. #include <gimple.h>
  24. #ifdef HAVE_C_FAMILY_C_COMMON_H
  25. # include <c-family/c-common.h>
  26. #elif HAVE_C_COMMON_H
  27. # include <c-common.h>
  28. #endif
  29. #include <starpu-gcc/utils.h>
  30. /* Whether to enable verbose output. */
  31. bool verbose_output_p = false;
  32. /* Various helpers. */
  33. /* Return a TYPE_DECL for the RECORD_TYPE with tag name TAG. */
  34. tree
  35. type_decl_for_struct_tag (const char *tag)
  36. {
  37. tree type_decl = xref_tag (RECORD_TYPE, get_identifier (tag));
  38. gcc_assert (type_decl != NULL_TREE
  39. && TREE_CODE (type_decl) == RECORD_TYPE);
  40. /* `build_decl' expects a TYPE_DECL, so give it what it wants. */
  41. type_decl = TYPE_STUB_DECL (type_decl);
  42. gcc_assert (type_decl != NULL && TREE_CODE (type_decl) == TYPE_DECL);
  43. return type_decl;
  44. }
  45. /* Given ERROR_VAR, an integer variable holding a StarPU error code, return
  46. statements that print out the error message returned by
  47. BUILD_ERROR_MESSAGE (ERROR_VAR) and abort. */
  48. tree
  49. build_error_statements (location_t loc, tree error_var,
  50. function_parm (tree, build_error_message, (tree)),
  51. const char *fmt, ...)
  52. {
  53. expanded_location xloc = expand_location (loc);
  54. tree print;
  55. char *str, *fmt_long;
  56. va_list args;
  57. va_start (args, fmt);
  58. /* Build a longer format. Since FMT itself contains % escapes, this needs
  59. to be done in two steps. */
  60. vasprintf (&str, fmt, args);
  61. if (error_var != NULL_TREE)
  62. {
  63. /* ERROR_VAR is an error code. */
  64. gcc_assert (TREE_CODE (error_var) == VAR_DECL
  65. && TREE_TYPE (error_var) == integer_type_node);
  66. asprintf (&fmt_long, "%s:%d: error: %s: %%s\n",
  67. xloc.file, xloc.line, str);
  68. print =
  69. build_call_expr (builtin_decl_explicit (BUILT_IN_PRINTF), 2,
  70. build_string_literal (strlen (fmt_long) + 1,
  71. fmt_long),
  72. build_error_message (error_var));
  73. }
  74. else
  75. {
  76. /* No error code provided. */
  77. asprintf (&fmt_long, "%s:%d: error: %s\n",
  78. xloc.file, xloc.line, str);
  79. print =
  80. build_call_expr (builtin_decl_explicit (BUILT_IN_PUTS), 1,
  81. build_string_literal (strlen (fmt_long) + 1,
  82. fmt_long));
  83. }
  84. free (fmt_long);
  85. free (str);
  86. va_end (args);
  87. tree stmts = NULL;
  88. append_to_statement_list (print, &stmts);
  89. append_to_statement_list (build_call_expr
  90. (builtin_decl_explicit (BUILT_IN_ABORT), 0),
  91. &stmts);
  92. return stmts;
  93. }
  94. /* Return a fresh argument list for FN. */
  95. tree
  96. build_function_arguments (tree fn)
  97. {
  98. gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
  99. && DECL_ARGUMENTS (fn) == NULL_TREE);
  100. local_define (tree, build_argument, (const_tree lst))
  101. {
  102. tree param, type;
  103. type = TREE_VALUE (lst);
  104. param = build_decl (DECL_SOURCE_LOCATION (fn), PARM_DECL,
  105. create_tmp_var_name ("argument"),
  106. type);
  107. DECL_ARG_TYPE (param) = type;
  108. DECL_CONTEXT (param) = fn;
  109. return param;
  110. };
  111. return map (build_argument,
  112. list_remove (void_type_p,
  113. TYPE_ARG_TYPES (TREE_TYPE (fn))));
  114. }
  115. /* Return true if LST holds the void type. */
  116. bool
  117. void_type_p (const_tree lst)
  118. {
  119. gcc_assert (TREE_CODE (lst) == TREE_LIST);
  120. return VOID_TYPE_P (TREE_VALUE (lst));
  121. }
  122. /* Return true if LST holds a pointer type. */
  123. bool
  124. pointer_type_p (const_tree lst)
  125. {
  126. gcc_assert (TREE_CODE (lst) == TREE_LIST);
  127. return POINTER_TYPE_P (TREE_VALUE (lst));
  128. }
  129. /* C expression parser, possibly with C++ linkage. */
  130. extern int yyparse (location_t, const char *, tree *);
  131. extern int yydebug;
  132. /* Parse expressions from the CPP reader for PRAGMA, which is located at LOC.
  133. Return a TREE_LIST of C expressions. */
  134. tree
  135. read_pragma_expressions (const char *pragma, location_t loc)
  136. {
  137. tree expr = NULL_TREE;
  138. if (yyparse (loc, pragma, &expr))
  139. /* Parse error or memory exhaustion. */
  140. expr = NULL_TREE;
  141. return expr;
  142. }
  143. /* List and vector utilities, à la SRFI-1. */
  144. tree
  145. chain_trees (tree t, ...)
  146. {
  147. va_list args;
  148. va_start (args, t);
  149. tree next, prev = t;
  150. for (prev = t, next = va_arg (args, tree);
  151. next != NULL_TREE;
  152. prev = next, next = va_arg (args, tree))
  153. TREE_CHAIN (prev) = next;
  154. va_end (args);
  155. return t;
  156. }
  157. tree
  158. filter (function_parm (bool, pred, (const_tree)), tree t)
  159. {
  160. tree result, lst;
  161. gcc_assert (TREE_CODE (t) == TREE_LIST);
  162. result = NULL_TREE;
  163. for (lst = t; lst != NULL_TREE; lst = TREE_CHAIN (lst))
  164. {
  165. if (pred (lst))
  166. result = tree_cons (TREE_PURPOSE (lst), TREE_VALUE (lst),
  167. result);
  168. }
  169. return nreverse (result);
  170. }
  171. tree
  172. list_remove (function_parm (bool, pred, (const_tree)), tree t)
  173. {
  174. local_define (bool, opposite, (const_tree t))
  175. {
  176. return !pred (t);
  177. };
  178. return filter (opposite, t);
  179. }
  180. /* Map FUNC over chain T. T does not have to be `TREE_LIST'; it can be a
  181. chain of arbitrary tree objects. */
  182. tree
  183. map (function_parm (tree, func, (const_tree)), tree t)
  184. {
  185. tree result, tail, lst;
  186. result = tail = NULL_TREE;
  187. for (lst = t; lst != NULL_TREE; lst = TREE_CHAIN (lst))
  188. {
  189. tree r = func (lst);
  190. if (tail != NULL_TREE)
  191. TREE_CHAIN (tail) = r;
  192. else
  193. result = r;
  194. tail = r;
  195. }
  196. return result;
  197. }
  198. void
  199. for_each (function_parm (void, func, (tree)), tree t)
  200. {
  201. tree lst;
  202. gcc_assert (TREE_CODE (t) == TREE_LIST);
  203. for (lst = t; lst != NULL_TREE; lst = TREE_CHAIN (lst))
  204. func (TREE_VALUE (lst));
  205. }
  206. size_t
  207. count (function_parm (bool, pred, (const_tree)), const_tree t)
  208. {
  209. size_t result;
  210. const_tree lst;
  211. for (lst = t, result = 0; lst != NULL_TREE; lst = TREE_CHAIN (lst))
  212. if (pred (lst))
  213. result++;
  214. return result;
  215. }
  216. /* Useful code backported from GCC 4.6. */
  217. #if !HAVE_DECL_BUILD_CALL_EXPR_LOC_ARRAY
  218. tree
  219. build_call_expr_loc_array (location_t loc, tree fndecl, int n, tree *argarray)
  220. {
  221. tree fntype = TREE_TYPE (fndecl);
  222. tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
  223. return fold_builtin_call_array (loc, TREE_TYPE (fntype), fn, n, argarray);
  224. }
  225. #endif
  226. #if !HAVE_DECL_BUILD_CALL_EXPR_LOC_VEC
  227. tree
  228. build_call_expr_loc_vec (location_t loc, tree fndecl, VEC(tree,gc) *vec)
  229. {
  230. return build_call_expr_loc_array (loc, fndecl, VEC_length (tree, vec),
  231. VEC_address (tree, vec));
  232. }
  233. #endif
  234. #if !HAVE_DECL_BUILD_ZERO_CST
  235. tree
  236. build_zero_cst (tree type)
  237. {
  238. switch (TREE_CODE (type))
  239. {
  240. case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
  241. case POINTER_TYPE: case REFERENCE_TYPE:
  242. case OFFSET_TYPE:
  243. return build_int_cst (type, 0);
  244. default:
  245. abort ();
  246. }
  247. }
  248. #endif
  249. /* Build a "conversion" from a raw C pointer to its data handle. The
  250. assumption is that the programmer should have already registered the
  251. pointer by themselves. */
  252. tree
  253. build_pointer_lookup (tree pointer)
  254. {
  255. static tree data_lookup_fn;
  256. /* Make sure DATA_LOOKUP_FN is valid. */
  257. LOOKUP_STARPU_FUNCTION (data_lookup_fn, "starpu_data_lookup");
  258. location_t loc;
  259. if (DECL_P (pointer))
  260. loc = DECL_SOURCE_LOCATION (pointer);
  261. else
  262. loc = UNKNOWN_LOCATION;
  263. /* Introduce a local variable to hold the handle. */
  264. tree result_var = build_decl (loc, VAR_DECL,
  265. create_tmp_var_name (".data_lookup_result"),
  266. ptr_type_node);
  267. DECL_CONTEXT (result_var) = current_function_decl;
  268. DECL_ARTIFICIAL (result_var) = true;
  269. DECL_SOURCE_LOCATION (result_var) = loc;
  270. tree call = build_call_expr (data_lookup_fn, 1, pointer);
  271. tree assignment = build2 (INIT_EXPR, TREE_TYPE (result_var),
  272. result_var, call);
  273. /* Build `if (RESULT_VAR == NULL) error ();'. */
  274. tree cond = build3 (COND_EXPR, void_type_node,
  275. build2 (EQ_EXPR, boolean_type_node,
  276. result_var, null_pointer_node),
  277. build_error_statements (loc, NULL_TREE,
  278. build_starpu_error_string,
  279. "attempt to use unregistered "
  280. "pointer"),
  281. NULL_TREE);
  282. tree stmts = NULL;
  283. append_to_statement_list (assignment, &stmts);
  284. append_to_statement_list (cond, &stmts);
  285. append_to_statement_list (result_var, &stmts);
  286. return build4 (TARGET_EXPR, ptr_type_node, result_var, stmts, NULL_TREE, NULL_TREE);
  287. }
  288. /* Build an error string for the StarPU return value in ERROR_VAR. */
  289. tree
  290. build_starpu_error_string (tree error_var)
  291. {
  292. static tree strerror_fn;
  293. LOOKUP_STARPU_FUNCTION (strerror_fn, "strerror");
  294. tree error_code =
  295. build1 (NEGATE_EXPR, TREE_TYPE (error_var), error_var);
  296. return build_call_expr (strerror_fn, 1, error_code);
  297. }
  298. /* Like `build_constructor_from_list', but sort VALS according to their
  299. offset in struct TYPE. Inspired by `gnat_build_constructor'. */
  300. tree
  301. build_constructor_from_unsorted_list (tree type, tree vals)
  302. {
  303. local_define (int, compare_elmt_bitpos, (const void *rt1, const void *rt2))
  304. {
  305. const constructor_elt *elmt1 = (constructor_elt *) rt1;
  306. const constructor_elt *elmt2 = (constructor_elt *) rt2;
  307. const_tree field1 = elmt1->index;
  308. const_tree field2 = elmt2->index;
  309. int ret
  310. = tree_int_cst_compare (bit_position (field1), bit_position (field2));
  311. return ret ? ret : (int) (DECL_UID (field1) - DECL_UID (field2));
  312. };
  313. tree t;
  314. VEC(constructor_elt,gc) *v = NULL;
  315. if (vals)
  316. {
  317. v = VEC_alloc (constructor_elt, gc, list_length (vals));
  318. for (t = vals; t; t = TREE_CHAIN (t))
  319. CONSTRUCTOR_APPEND_ELT (v, TREE_PURPOSE (t), TREE_VALUE (t));
  320. }
  321. /* Sort field initializers by field offset. */
  322. VEC_qsort (constructor_elt, v, compare_elmt_bitpos);
  323. return build_constructor (type, v);
  324. }