Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ast_function.cpp 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /*
  2. * Copyright © 2010 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <cstdio>
  24. #include "glsl_symbol_table.h"
  25. #include "ast.h"
  26. #include "glsl_types.h"
  27. #include "ir.h"
  28. static unsigned
  29. process_parameters(exec_list *instructions, exec_list *actual_parameters,
  30. simple_node *parameters,
  31. struct _mesa_glsl_parse_state *state)
  32. {
  33. simple_node *const first = parameters;
  34. unsigned count = 0;
  35. if (first != NULL) {
  36. simple_node *ptr = first;
  37. do {
  38. ir_rvalue *const result =
  39. ((ast_node *) ptr)->hir(instructions, state);
  40. ptr = ptr->next;
  41. actual_parameters->push_tail(result);
  42. count++;
  43. } while (ptr != first);
  44. }
  45. return count;
  46. }
  47. static ir_rvalue *
  48. process_call(exec_list *instructions, ir_function *f,
  49. YYLTYPE *loc, exec_list *actual_parameters,
  50. struct _mesa_glsl_parse_state *state)
  51. {
  52. const ir_function_signature *sig =
  53. f->matching_signature(actual_parameters);
  54. /* The instructions param will be used when the FINISHMEs below are done */
  55. (void) instructions;
  56. if (sig != NULL) {
  57. /* Verify that 'out' and 'inout' actual parameters are lvalues. This
  58. * isn't done in ir_function::matching_signature because that function
  59. * cannot generate the necessary diagnostics.
  60. */
  61. exec_list_iterator actual_iter = actual_parameters->iterator();
  62. exec_list_iterator formal_iter = sig->parameters.iterator();
  63. while (actual_iter.has_next()) {
  64. ir_rvalue *actual = (ir_rvalue *) actual_iter.get();
  65. ir_variable *formal = (ir_variable *) formal_iter.get();
  66. assert(actual != NULL);
  67. assert(formal != NULL);
  68. if ((formal->mode == ir_var_out)
  69. || (formal->mode == ir_var_inout)) {
  70. if (! actual->is_lvalue()) {
  71. /* FINISHME: Log a better diagnostic here. There is no way
  72. * FINISHME: to tell the user which parameter is invalid.
  73. */
  74. _mesa_glsl_error(loc, state, "`%s' parameter is not lvalue",
  75. (formal->mode == ir_var_out) ? "out" : "inout");
  76. }
  77. }
  78. actual_iter.next();
  79. formal_iter.next();
  80. }
  81. /* FINISHME: The list of actual parameters needs to be modified to
  82. * FINISHME: include any necessary conversions.
  83. */
  84. return new ir_call(sig, actual_parameters);
  85. } else {
  86. /* FINISHME: Log a better error message here. G++ will show the types
  87. * FINISHME: of the actual parameters and the set of candidate
  88. * FINISHME: functions. A different error should also be logged when
  89. * FINISHME: multiple functions match.
  90. */
  91. _mesa_glsl_error(loc, state, "no matching function for call to `%s'",
  92. f->name);
  93. return ir_call::get_error_instruction();
  94. }
  95. }
  96. static ir_rvalue *
  97. match_function_by_name(exec_list *instructions, const char *name,
  98. YYLTYPE *loc, simple_node *parameters,
  99. struct _mesa_glsl_parse_state *state)
  100. {
  101. ir_function *f = state->symbols->get_function(name);
  102. if (f == NULL) {
  103. _mesa_glsl_error(loc, state, "function `%s' undeclared", name);
  104. return ir_call::get_error_instruction();
  105. }
  106. /* Once we've determined that the function being called might exist,
  107. * process the parameters.
  108. */
  109. exec_list actual_parameters;
  110. process_parameters(instructions, &actual_parameters, parameters, state);
  111. /* After processing the function's actual parameters, try to find an
  112. * overload of the function that matches.
  113. */
  114. return process_call(instructions, f, loc, &actual_parameters, state);
  115. }
  116. /**
  117. * Perform automatic type conversion of constructor parameters
  118. */
  119. static ir_rvalue *
  120. convert_component(ir_rvalue *src, const glsl_type *desired_type)
  121. {
  122. const unsigned a = desired_type->base_type;
  123. const unsigned b = src->type->base_type;
  124. if (src->type->is_error())
  125. return src;
  126. assert(a <= GLSL_TYPE_BOOL);
  127. assert(b <= GLSL_TYPE_BOOL);
  128. if ((a == b) || (src->type->is_integer() && desired_type->is_integer()))
  129. return src;
  130. switch (a) {
  131. case GLSL_TYPE_UINT:
  132. case GLSL_TYPE_INT:
  133. if (b == GLSL_TYPE_FLOAT)
  134. return new ir_expression(ir_unop_f2i, desired_type, src, NULL);
  135. else {
  136. assert(b == GLSL_TYPE_BOOL);
  137. return new ir_expression(ir_unop_f2b, desired_type, src, NULL);
  138. }
  139. case GLSL_TYPE_FLOAT:
  140. switch (b) {
  141. case GLSL_TYPE_UINT:
  142. return new ir_expression(ir_unop_u2f, desired_type, src, NULL);
  143. case GLSL_TYPE_INT:
  144. return new ir_expression(ir_unop_i2f, desired_type, src, NULL);
  145. case GLSL_TYPE_BOOL:
  146. return new ir_expression(ir_unop_b2f, desired_type, src, NULL);
  147. }
  148. break;
  149. case GLSL_TYPE_BOOL: {
  150. int z = 0;
  151. ir_constant *const zero = new ir_constant(src->type, &z);
  152. return new ir_expression(ir_binop_nequal, desired_type, src, zero);
  153. }
  154. }
  155. assert(!"Should not get here.");
  156. return NULL;
  157. }
  158. /**
  159. * Dereference a specific component from a scalar, vector, or matrix
  160. */
  161. static ir_rvalue *
  162. dereference_component(ir_rvalue *src, unsigned component)
  163. {
  164. assert(component < src->type->components());
  165. if (src->type->is_scalar()) {
  166. return src;
  167. } else if (src->type->is_vector()) {
  168. return new ir_swizzle(src, component, 0, 0, 0, 1);
  169. } else {
  170. assert(src->type->is_matrix());
  171. /* Dereference a row of the matrix, then call this function again to get
  172. * a specific element from that row.
  173. */
  174. const int c = component / src->type->column_type()->vector_elements;
  175. const int r = component % src->type->column_type()->vector_elements;
  176. ir_constant *const col_index = new ir_constant(glsl_type::int_type, &c);
  177. ir_dereference *const col = new ir_dereference(src, col_index);
  178. col->type = src->type->column_type();
  179. return dereference_component(col, r);
  180. }
  181. assert(!"Should not get here.");
  182. return NULL;
  183. }
  184. static ir_rvalue *
  185. process_array_constructor(exec_list *instructions,
  186. const glsl_type *constructor_type,
  187. YYLTYPE *loc, simple_node *parameters,
  188. struct _mesa_glsl_parse_state *state)
  189. {
  190. /* Array constructors come in two forms: sized and unsized. Sized array
  191. * constructors look like 'vec4[2](a, b)', where 'a' and 'b' are vec4
  192. * variables. In this case the number of parameters must exactly match the
  193. * specified size of the array.
  194. *
  195. * Unsized array constructors look like 'vec4[](a, b)', where 'a' and 'b'
  196. * are vec4 variables. In this case the size of the array being constructed
  197. * is determined by the number of parameters.
  198. *
  199. * From page 52 (page 58 of the PDF) of the GLSL 1.50 spec:
  200. *
  201. * "There must be exactly the same number of arguments as the size of
  202. * the array being constructed. If no size is present in the
  203. * constructor, then the array is explicitly sized to the number of
  204. * arguments provided. The arguments are assigned in order, starting at
  205. * element 0, to the elements of the constructed array. Each argument
  206. * must be the same type as the element type of the array, or be a type
  207. * that can be converted to the element type of the array according to
  208. * Section 4.1.10 "Implicit Conversions.""
  209. */
  210. exec_list actual_parameters;
  211. const unsigned parameter_count =
  212. process_parameters(instructions, &actual_parameters, parameters, state);
  213. if ((parameter_count == 0)
  214. || ((constructor_type->length != 0)
  215. && (constructor_type->length != parameter_count))) {
  216. const unsigned min_param = (constructor_type->length == 0)
  217. ? 1 : constructor_type->length;
  218. _mesa_glsl_error(loc, state, "array constructor must have %s %u "
  219. "parameter%s",
  220. (constructor_type->length != 0) ? "at least" : "exactly",
  221. min_param, (min_param <= 1) ? "" : "s");
  222. return ir_call::get_error_instruction();
  223. }
  224. if (constructor_type->length == 0) {
  225. constructor_type =
  226. glsl_type::get_array_instance(constructor_type->element_type(),
  227. parameter_count);
  228. assert(constructor_type != NULL);
  229. assert(constructor_type->length == parameter_count);
  230. }
  231. ir_function *f = state->symbols->get_function(constructor_type->name);
  232. /* If the constructor for this type of array does not exist, generate the
  233. * prototype and add it to the symbol table. The code will be generated
  234. * later.
  235. */
  236. if (f == NULL) {
  237. f = constructor_type->generate_constructor_prototype(state->symbols);
  238. }
  239. ir_rvalue *const r =
  240. process_call(instructions, f, loc, &actual_parameters, state);
  241. assert(r != NULL);
  242. assert(r->type->is_error() || (r->type == constructor_type));
  243. return r;
  244. }
  245. ir_rvalue *
  246. ast_function_expression::hir(exec_list *instructions,
  247. struct _mesa_glsl_parse_state *state)
  248. {
  249. /* There are three sorts of function calls.
  250. *
  251. * 1. contstructors - The first subexpression is an ast_type_specifier.
  252. * 2. methods - Only the .length() method of array types.
  253. * 3. functions - Calls to regular old functions.
  254. *
  255. * Method calls are actually detected when the ast_field_selection
  256. * expression is handled.
  257. */
  258. if (is_constructor()) {
  259. const ast_type_specifier *type = (ast_type_specifier *) subexpressions[0];
  260. YYLTYPE loc = type->get_location();
  261. const char *name;
  262. const glsl_type *const constructor_type = type->glsl_type(& name, state);
  263. /* Constructors for samplers are illegal.
  264. */
  265. if (constructor_type->is_sampler()) {
  266. _mesa_glsl_error(& loc, state, "cannot construct sampler type `%s'",
  267. constructor_type->name);
  268. return ir_call::get_error_instruction();
  269. }
  270. if (constructor_type->is_array()) {
  271. if (state->language_version <= 110) {
  272. _mesa_glsl_error(& loc, state,
  273. "array constructors forbidden in GLSL 1.10");
  274. return ir_call::get_error_instruction();
  275. }
  276. return process_array_constructor(instructions, constructor_type,
  277. & loc, subexpressions[1], state);
  278. }
  279. /* There are two kinds of constructor call. Constructors for built-in
  280. * language types, such as mat4 and vec2, are free form. The only
  281. * requirement is that the parameters must provide enough values of the
  282. * correct scalar type. Constructors for arrays and structures must
  283. * have the exact number of parameters with matching types in the
  284. * correct order. These constructors follow essentially the same type
  285. * matching rules as functions.
  286. */
  287. if (constructor_type->is_numeric() || constructor_type->is_boolean()) {
  288. /* Constructing a numeric type has a couple steps. First all values
  289. * passed to the constructor are broken into individual parameters
  290. * and type converted to the base type of the thing being constructed.
  291. *
  292. * At that point we have some number of values that match the base
  293. * type of the thing being constructed. Now the constructor can be
  294. * treated like a function call. Each numeric type has a small set
  295. * of constructor functions. The set of new parameters will either
  296. * match one of those functions or the original constructor is
  297. * invalid.
  298. */
  299. const glsl_type *const base_type = constructor_type->get_base_type();
  300. /* Total number of components of the type being constructed.
  301. */
  302. const unsigned type_components = constructor_type->components();
  303. /* Number of components from parameters that have actually been
  304. * consumed. This is used to perform several kinds of error checking.
  305. */
  306. unsigned components_used = 0;
  307. unsigned matrix_parameters = 0;
  308. unsigned nonmatrix_parameters = 0;
  309. exec_list actual_parameters;
  310. simple_node *const first = subexpressions[1];
  311. assert(first != NULL);
  312. if (first != NULL) {
  313. simple_node *ptr = first;
  314. do {
  315. ir_rvalue *const result =
  316. ((ast_node *) ptr)->hir(instructions, state)->as_rvalue();
  317. ptr = ptr->next;
  318. /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
  319. *
  320. * "It is an error to provide extra arguments beyond this
  321. * last used argument."
  322. */
  323. if (components_used >= type_components) {
  324. _mesa_glsl_error(& loc, state, "too many parameters to `%s' "
  325. "constructor",
  326. constructor_type->name);
  327. return ir_call::get_error_instruction();
  328. }
  329. if (!result->type->is_numeric() && !result->type->is_boolean()) {
  330. _mesa_glsl_error(& loc, state, "cannot construct `%s' from a "
  331. "non-numeric data type",
  332. constructor_type->name);
  333. return ir_call::get_error_instruction();
  334. }
  335. /* Count the number of matrix and nonmatrix parameters. This
  336. * is used below to enforce some of the constructor rules.
  337. */
  338. if (result->type->is_matrix())
  339. matrix_parameters++;
  340. else
  341. nonmatrix_parameters++;
  342. /* Process each of the components of the parameter. Dereference
  343. * each component individually, perform any type conversions, and
  344. * add it to the parameter list for the constructor.
  345. */
  346. for (unsigned i = 0; i < result->type->components(); i++) {
  347. if (components_used >= type_components)
  348. break;
  349. ir_rvalue *const component =
  350. convert_component(dereference_component(result, i),
  351. base_type);
  352. /* All cases that could result in component->type being the
  353. * error type should have already been caught above.
  354. */
  355. assert(component->type == base_type);
  356. /* Don't actually generate constructor calls for scalars.
  357. * Instead, do the usual component selection and conversion,
  358. * and return the single component.
  359. */
  360. if (constructor_type->is_scalar())
  361. return component;
  362. actual_parameters.push_tail(component);
  363. components_used++;
  364. }
  365. } while (ptr != first);
  366. }
  367. /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
  368. *
  369. * "It is an error to construct matrices from other matrices. This
  370. * is reserved for future use."
  371. */
  372. if ((state->language_version <= 110) && (matrix_parameters > 0)
  373. && constructor_type->is_matrix()) {
  374. _mesa_glsl_error(& loc, state, "cannot construct `%s' from a "
  375. "matrix in GLSL 1.10",
  376. constructor_type->name);
  377. return ir_call::get_error_instruction();
  378. }
  379. /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
  380. *
  381. * "If a matrix argument is given to a matrix constructor, it is
  382. * an error to have any other arguments."
  383. */
  384. if ((matrix_parameters > 0)
  385. && ((matrix_parameters + nonmatrix_parameters) > 1)
  386. && constructor_type->is_matrix()) {
  387. _mesa_glsl_error(& loc, state, "for matrix `%s' constructor, "
  388. "matrix must be only parameter",
  389. constructor_type->name);
  390. return ir_call::get_error_instruction();
  391. }
  392. /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
  393. *
  394. * "In these cases, there must be enough components provided in the
  395. * arguments to provide an initializer for every component in the
  396. * constructed value."
  397. */
  398. if ((components_used < type_components) && (components_used != 1)) {
  399. _mesa_glsl_error(& loc, state, "too few components to construct "
  400. "`%s'",
  401. constructor_type->name);
  402. return ir_call::get_error_instruction();
  403. }
  404. ir_function *f = state->symbols->get_function(constructor_type->name);
  405. if (f == NULL) {
  406. _mesa_glsl_error(& loc, state, "no constructor for type `%s'",
  407. constructor_type->name);
  408. return ir_call::get_error_instruction();
  409. }
  410. const ir_function_signature *sig =
  411. f->matching_signature(& actual_parameters);
  412. if (sig != NULL) {
  413. return new ir_call(sig, & actual_parameters);
  414. } else {
  415. /* FINISHME: Log a better error message here. G++ will show the
  416. * FINSIHME: types of the actual parameters and the set of
  417. * FINSIHME: candidate functions. A different error should also be
  418. * FINSIHME: logged when multiple functions match.
  419. */
  420. _mesa_glsl_error(& loc, state, "no matching constructor for `%s'",
  421. constructor_type->name);
  422. return ir_call::get_error_instruction();
  423. }
  424. }
  425. return ir_call::get_error_instruction();
  426. } else {
  427. const ast_expression *id = subexpressions[0];
  428. YYLTYPE loc = id->get_location();
  429. return match_function_by_name(instructions,
  430. id->primary_expression.identifier, & loc,
  431. subexpressions[1], state);
  432. }
  433. return ir_call::get_error_instruction();
  434. }