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_to_hir.cpp 34KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  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. /**
  24. * \file ast_to_hir.c
  25. * Convert abstract syntax to to high-level intermediate reprensentation (HIR).
  26. *
  27. * During the conversion to HIR, the majority of the symantic checking is
  28. * preformed on the program. This includes:
  29. *
  30. * * Symbol table management
  31. * * Type checking
  32. * * Function binding
  33. *
  34. * The majority of this work could be done during parsing, and the parser could
  35. * probably generate HIR directly. However, this results in frequent changes
  36. * to the parser code. Since we do not assume that every system this complier
  37. * is built on will have Flex and Bison installed, we have to store the code
  38. * generated by these tools in our version control system. In other parts of
  39. * the system we've seen problems where a parser was changed but the generated
  40. * code was not committed, merge conflicts where created because two developers
  41. * had slightly different versions of Bison installed, etc.
  42. *
  43. * I have also noticed that running Bison generated parsers in GDB is very
  44. * irritating. When you get a segfault on '$$ = $1->foo', you can't very
  45. * well 'print $1' in GDB.
  46. *
  47. * As a result, my preference is to put as little C code as possible in the
  48. * parser (and lexer) sources.
  49. */
  50. #include <stdio.h>
  51. #include "main/imports.h"
  52. #include "glsl_symbol_table.h"
  53. #include "glsl_parser_extras.h"
  54. #include "ast.h"
  55. #include "glsl_types.h"
  56. #include "ir.h"
  57. void
  58. _mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state)
  59. {
  60. struct simple_node *ptr;
  61. _mesa_glsl_initialize_variables(instructions, state);
  62. foreach (ptr, & state->translation_unit) {
  63. ((ast_node *)ptr)->hir(instructions, state);
  64. }
  65. }
  66. static const struct glsl_type *
  67. arithmetic_result_type(const struct glsl_type *type_a,
  68. const struct glsl_type *type_b,
  69. bool multiply,
  70. struct _mesa_glsl_parse_state *state)
  71. {
  72. /* From GLSL 1.50 spec, page 56:
  73. *
  74. * "The arithmetic binary operators add (+), subtract (-),
  75. * multiply (*), and divide (/) operate on integer and
  76. * floating-point scalars, vectors, and matrices."
  77. */
  78. if (! is_numeric_base_type(type_a->base_type)
  79. || ! is_numeric_base_type(type_b->base_type)) {
  80. return glsl_error_type;
  81. }
  82. /* "If one operand is floating-point based and the other is
  83. * not, then the conversions from Section 4.1.10 "Implicit
  84. * Conversions" are applied to the non-floating-point-based operand."
  85. *
  86. * This conversion was added in GLSL 1.20. If the compilation mode is
  87. * GLSL 1.10, the conversion is skipped.
  88. */
  89. if (state->language_version >= 120) {
  90. if ((type_a->base_type == GLSL_TYPE_FLOAT)
  91. && (type_b->base_type != GLSL_TYPE_FLOAT)) {
  92. } else if ((type_a->base_type != GLSL_TYPE_FLOAT)
  93. && (type_b->base_type == GLSL_TYPE_FLOAT)) {
  94. }
  95. }
  96. /* "If the operands are integer types, they must both be signed or
  97. * both be unsigned."
  98. *
  99. * From this rule and the preceeding conversion it can be inferred that
  100. * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
  101. * The is_numeric_base_type check above already filtered out the case
  102. * where either type is not one of these, so now the base types need only
  103. * be tested for equality.
  104. */
  105. if (type_a->base_type != type_b->base_type) {
  106. return glsl_error_type;
  107. }
  108. /* "All arithmetic binary operators result in the same fundamental type
  109. * (signed integer, unsigned integer, or floating-point) as the
  110. * operands they operate on, after operand type conversion. After
  111. * conversion, the following cases are valid
  112. *
  113. * * The two operands are scalars. In this case the operation is
  114. * applied, resulting in a scalar."
  115. */
  116. if (type_a->is_scalar() && type_b->is_scalar())
  117. return type_a;
  118. /* "* One operand is a scalar, and the other is a vector or matrix.
  119. * In this case, the scalar operation is applied independently to each
  120. * component of the vector or matrix, resulting in the same size
  121. * vector or matrix."
  122. */
  123. if (type_a->is_scalar()) {
  124. if (!type_b->is_scalar())
  125. return type_b;
  126. } else if (type_b->is_scalar()) {
  127. return type_a;
  128. }
  129. /* All of the combinations of <scalar, scalar>, <vector, scalar>,
  130. * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
  131. * handled.
  132. */
  133. assert(type_a->vector_elements > 1);
  134. assert(type_b->vector_elements > 1);
  135. /* "* The two operands are vectors of the same size. In this case, the
  136. * operation is done component-wise resulting in the same size
  137. * vector."
  138. */
  139. if (type_a->is_vector() && type_b->is_vector()) {
  140. if (type_a->vector_elements == type_b->vector_elements)
  141. return type_a;
  142. else
  143. return glsl_error_type;
  144. }
  145. /* All of the combinations of <scalar, scalar>, <vector, scalar>,
  146. * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
  147. * <vector, vector> have been handled. At least one of the operands must
  148. * be matrix. Further, since there are no integer matrix types, the base
  149. * type of both operands must be float.
  150. */
  151. assert((type_a->matrix_rows > 1) || (type_b->matrix_rows > 1));
  152. assert(type_a->base_type == GLSL_TYPE_FLOAT);
  153. assert(type_b->base_type == GLSL_TYPE_FLOAT);
  154. /* "* The operator is add (+), subtract (-), or divide (/), and the
  155. * operands are matrices with the same number of rows and the same
  156. * number of columns. In this case, the operation is done component-
  157. * wise resulting in the same size matrix."
  158. * * The operator is multiply (*), where both operands are matrices or
  159. * one operand is a vector and the other a matrix. A right vector
  160. * operand is treated as a column vector and a left vector operand as a
  161. * row vector. In all these cases, it is required that the number of
  162. * columns of the left operand is equal to the number of rows of the
  163. * right operand. Then, the multiply (*) operation does a linear
  164. * algebraic multiply, yielding an object that has the same number of
  165. * rows as the left operand and the same number of columns as the right
  166. * operand. Section 5.10 "Vector and Matrix Operations" explains in
  167. * more detail how vectors and matrices are operated on."
  168. */
  169. if (! multiply) {
  170. if (type_a->is_matrix() && type_b->is_matrix()
  171. && (type_a->vector_elements == type_b->vector_elements)
  172. && (type_a->matrix_rows == type_b->matrix_rows))
  173. return type_a;
  174. else
  175. return glsl_error_type;
  176. } else {
  177. if (type_a->is_matrix() && type_b->is_matrix()) {
  178. if (type_a->vector_elements == type_b->matrix_rows) {
  179. char type_name[7];
  180. const struct glsl_type *t;
  181. type_name[0] = 'm';
  182. type_name[1] = 'a';
  183. type_name[2] = 't';
  184. if (type_a->matrix_rows == type_b->vector_elements) {
  185. type_name[3] = '0' + type_a->matrix_rows;
  186. type_name[4] = '\0';
  187. } else {
  188. type_name[3] = '0' + type_a->matrix_rows;
  189. type_name[4] = 'x';
  190. type_name[5] = '0' + type_b->vector_elements;
  191. type_name[6] = '\0';
  192. }
  193. t = state->symbols->get_type(type_name);
  194. return (t != NULL) ? t : glsl_error_type;
  195. }
  196. } else if (type_a->is_matrix()) {
  197. /* A is a matrix and B is a column vector. Columns of A must match
  198. * rows of B.
  199. */
  200. if (type_a->vector_elements == type_b->vector_elements)
  201. return type_b;
  202. } else {
  203. assert(type_b->is_matrix());
  204. /* A is a row vector and B is a matrix. Columns of A must match
  205. * rows of B.
  206. */
  207. if (type_a->vector_elements == type_b->matrix_rows)
  208. return type_a;
  209. }
  210. }
  211. /* "All other cases are illegal."
  212. */
  213. return glsl_error_type;
  214. }
  215. static const struct glsl_type *
  216. unary_arithmetic_result_type(const struct glsl_type *type)
  217. {
  218. /* From GLSL 1.50 spec, page 57:
  219. *
  220. * "The arithmetic unary operators negate (-), post- and pre-increment
  221. * and decrement (-- and ++) operate on integer or floating-point
  222. * values (including vectors and matrices). All unary operators work
  223. * component-wise on their operands. These result with the same type
  224. * they operated on."
  225. */
  226. if (!is_numeric_base_type(type->base_type))
  227. return glsl_error_type;
  228. return type;
  229. }
  230. static const struct glsl_type *
  231. modulus_result_type(const struct glsl_type *type_a,
  232. const struct glsl_type *type_b)
  233. {
  234. /* From GLSL 1.50 spec, page 56:
  235. * "The operator modulus (%) operates on signed or unsigned integers or
  236. * integer vectors. The operand types must both be signed or both be
  237. * unsigned."
  238. */
  239. if (! is_integer_base_type(type_a->base_type)
  240. || ! is_integer_base_type(type_b->base_type)
  241. || (type_a->base_type != type_b->base_type)) {
  242. return glsl_error_type;
  243. }
  244. /* "The operands cannot be vectors of differing size. If one operand is
  245. * a scalar and the other vector, then the scalar is applied component-
  246. * wise to the vector, resulting in the same type as the vector. If both
  247. * are vectors of the same size, the result is computed component-wise."
  248. */
  249. if (type_a->is_vector()) {
  250. if (!type_b->is_vector()
  251. || (type_a->vector_elements == type_b->vector_elements))
  252. return type_a;
  253. } else
  254. return type_b;
  255. /* "The operator modulus (%) is not defined for any other data types
  256. * (non-integer types)."
  257. */
  258. return glsl_error_type;
  259. }
  260. static const struct glsl_type *
  261. relational_result_type(const struct glsl_type *type_a,
  262. const struct glsl_type *type_b,
  263. struct _mesa_glsl_parse_state *state)
  264. {
  265. /* From GLSL 1.50 spec, page 56:
  266. * "The relational operators greater than (>), less than (<), greater
  267. * than or equal (>=), and less than or equal (<=) operate only on
  268. * scalar integer and scalar floating-point expressions."
  269. */
  270. if (! is_numeric_base_type(type_a->base_type)
  271. || ! is_numeric_base_type(type_b->base_type)
  272. || !type_a->is_scalar()
  273. || !type_b->is_scalar())
  274. return glsl_error_type;
  275. /* "Either the operands' types must match, or the conversions from
  276. * Section 4.1.10 "Implicit Conversions" will be applied to the integer
  277. * operand, after which the types must match."
  278. *
  279. * This conversion was added in GLSL 1.20. If the compilation mode is
  280. * GLSL 1.10, the conversion is skipped.
  281. */
  282. if (state->language_version >= 120) {
  283. if ((type_a->base_type == GLSL_TYPE_FLOAT)
  284. && (type_b->base_type != GLSL_TYPE_FLOAT)) {
  285. /* FINISHME: Generate the implicit type conversion. */
  286. } else if ((type_a->base_type != GLSL_TYPE_FLOAT)
  287. && (type_b->base_type == GLSL_TYPE_FLOAT)) {
  288. /* FINISHME: Generate the implicit type conversion. */
  289. }
  290. }
  291. if (type_a->base_type != type_b->base_type)
  292. return glsl_error_type;
  293. /* "The result is scalar Boolean."
  294. */
  295. return glsl_bool_type;
  296. }
  297. ir_instruction *
  298. ast_node::hir(exec_list *instructions,
  299. struct _mesa_glsl_parse_state *state)
  300. {
  301. (void) instructions;
  302. (void) state;
  303. return NULL;
  304. }
  305. ir_instruction *
  306. ast_expression::hir(exec_list *instructions,
  307. struct _mesa_glsl_parse_state *state)
  308. {
  309. static const int operations[AST_NUM_OPERATORS] = {
  310. -1, /* ast_assign doesn't convert to ir_expression. */
  311. -1, /* ast_plus doesn't convert to ir_expression. */
  312. ir_unop_neg,
  313. ir_binop_add,
  314. ir_binop_sub,
  315. ir_binop_mul,
  316. ir_binop_div,
  317. ir_binop_mod,
  318. ir_binop_lshift,
  319. ir_binop_rshift,
  320. ir_binop_less,
  321. ir_binop_greater,
  322. ir_binop_lequal,
  323. ir_binop_gequal,
  324. ir_binop_equal,
  325. ir_binop_nequal,
  326. ir_binop_bit_and,
  327. ir_binop_bit_xor,
  328. ir_binop_bit_or,
  329. ir_unop_bit_not,
  330. ir_binop_logic_and,
  331. ir_binop_logic_xor,
  332. ir_binop_logic_or,
  333. ir_unop_logic_not,
  334. /* Note: The following block of expression types actually convert
  335. * to multiple IR instructions.
  336. */
  337. ir_binop_mul, /* ast_mul_assign */
  338. ir_binop_div, /* ast_div_assign */
  339. ir_binop_mod, /* ast_mod_assign */
  340. ir_binop_add, /* ast_add_assign */
  341. ir_binop_sub, /* ast_sub_assign */
  342. ir_binop_lshift, /* ast_ls_assign */
  343. ir_binop_rshift, /* ast_rs_assign */
  344. ir_binop_bit_and, /* ast_and_assign */
  345. ir_binop_bit_xor, /* ast_xor_assign */
  346. ir_binop_bit_or, /* ast_or_assign */
  347. -1, /* ast_conditional doesn't convert to ir_expression. */
  348. -1, /* ast_pre_inc doesn't convert to ir_expression. */
  349. -1, /* ast_pre_dec doesn't convert to ir_expression. */
  350. -1, /* ast_post_inc doesn't convert to ir_expression. */
  351. -1, /* ast_post_dec doesn't convert to ir_expression. */
  352. -1, /* ast_field_selection doesn't conv to ir_expression. */
  353. -1, /* ast_array_index doesn't convert to ir_expression. */
  354. -1, /* ast_function_call doesn't conv to ir_expression. */
  355. -1, /* ast_identifier doesn't convert to ir_expression. */
  356. -1, /* ast_int_constant doesn't convert to ir_expression. */
  357. -1, /* ast_uint_constant doesn't conv to ir_expression. */
  358. -1, /* ast_float_constant doesn't conv to ir_expression. */
  359. -1, /* ast_bool_constant doesn't conv to ir_expression. */
  360. -1, /* ast_sequence doesn't convert to ir_expression. */
  361. };
  362. ir_instruction *result = NULL;
  363. ir_instruction *op[2];
  364. struct simple_node op_list;
  365. const struct glsl_type *type = glsl_error_type;
  366. bool error_emitted = false;
  367. YYLTYPE loc;
  368. loc = this->get_location();
  369. make_empty_list(& op_list);
  370. switch (this->oper) {
  371. case ast_assign: {
  372. op[0] = this->subexpressions[0]->hir(instructions, state);
  373. op[1] = this->subexpressions[1]->hir(instructions, state);
  374. error_emitted = ((op[0]->type == glsl_error_type)
  375. || (op[1]->type == glsl_error_type));
  376. type = op[0]->type;
  377. if (!error_emitted) {
  378. YYLTYPE loc;
  379. /* FINISHME: This does not handle 'foo.bar.a.b.c[5].d = 5' */
  380. loc = this->subexpressions[0]->get_location();
  381. if (op[0]->mode != ir_op_dereference) {
  382. _mesa_glsl_error(& loc, state, "invalid lvalue in assignment");
  383. error_emitted = true;
  384. type = glsl_error_type;
  385. } else {
  386. const struct ir_dereference *const ref =
  387. (struct ir_dereference *) op[0];
  388. const struct ir_variable *const var =
  389. (struct ir_variable *) ref->var;
  390. if ((var != NULL)
  391. && (var->mode == ir_op_var_decl)
  392. && (var->read_only)) {
  393. _mesa_glsl_error(& loc, state, "cannot assign to read-only "
  394. "variable `%s'", var->name);
  395. error_emitted = true;
  396. type = glsl_error_type;
  397. }
  398. }
  399. }
  400. /* FINISHME: Check that the LHS and RHS have matching types. */
  401. /* FINISHME: For GLSL 1.10, check that the types are not arrays. */
  402. ir_instruction *tmp = new ir_assignment(op[0], op[1], NULL);
  403. instructions->push_tail(tmp);
  404. result = op[0];
  405. break;
  406. }
  407. case ast_plus:
  408. op[0] = this->subexpressions[0]->hir(instructions, state);
  409. error_emitted = (op[0]->type == glsl_error_type);
  410. if (type == glsl_error_type)
  411. op[0]->type = type;
  412. result = op[0];
  413. break;
  414. case ast_neg:
  415. op[0] = this->subexpressions[0]->hir(instructions, state);
  416. type = unary_arithmetic_result_type(op[0]->type);
  417. error_emitted = (op[0]->type == glsl_error_type);
  418. result = new ir_expression(operations[this->oper], type,
  419. op[0], NULL);
  420. break;
  421. case ast_add:
  422. case ast_sub:
  423. case ast_mul:
  424. case ast_div:
  425. op[0] = this->subexpressions[0]->hir(instructions, state);
  426. op[1] = this->subexpressions[1]->hir(instructions, state);
  427. type = arithmetic_result_type(op[0]->type, op[1]->type,
  428. (this->oper == ast_mul),
  429. state);
  430. result = new ir_expression(operations[this->oper], type,
  431. op[0], op[1]);
  432. break;
  433. case ast_mod:
  434. op[0] = this->subexpressions[0]->hir(instructions, state);
  435. op[1] = this->subexpressions[1]->hir(instructions, state);
  436. error_emitted = ((op[0]->type == glsl_error_type)
  437. || (op[1]->type == glsl_error_type));
  438. type = modulus_result_type(op[0]->type, op[1]->type);
  439. assert(operations[this->oper] == ir_binop_mod);
  440. result = new ir_expression(operations[this->oper], type,
  441. op[0], op[1]);
  442. break;
  443. case ast_lshift:
  444. case ast_rshift:
  445. /* FINISHME: Implement bit-shift operators. */
  446. break;
  447. case ast_less:
  448. case ast_greater:
  449. case ast_lequal:
  450. case ast_gequal:
  451. op[0] = this->subexpressions[0]->hir(instructions, state);
  452. op[1] = this->subexpressions[1]->hir(instructions, state);
  453. error_emitted = ((op[0]->type == glsl_error_type)
  454. || (op[1]->type == glsl_error_type));
  455. type = relational_result_type(op[0]->type, op[1]->type, state);
  456. /* The relational operators must either generate an error or result
  457. * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
  458. */
  459. assert((type == glsl_error_type)
  460. || ((type->base_type == GLSL_TYPE_BOOL)
  461. && type->is_scalar()));
  462. result = new ir_expression(operations[this->oper], type,
  463. op[0], op[1]);
  464. break;
  465. case ast_nequal:
  466. case ast_equal:
  467. /* FINISHME: Implement equality operators. */
  468. break;
  469. case ast_bit_and:
  470. case ast_bit_xor:
  471. case ast_bit_or:
  472. case ast_bit_not:
  473. /* FINISHME: Implement bit-wise operators. */
  474. break;
  475. case ast_logic_and:
  476. case ast_logic_xor:
  477. case ast_logic_or:
  478. case ast_logic_not:
  479. /* FINISHME: Implement logical operators. */
  480. break;
  481. case ast_mul_assign:
  482. case ast_div_assign:
  483. case ast_add_assign:
  484. case ast_sub_assign: {
  485. struct ir_instruction *temp_rhs;
  486. op[0] = this->subexpressions[0]->hir(instructions, state);
  487. op[1] = this->subexpressions[1]->hir(instructions, state);
  488. error_emitted = ((op[0]->type == glsl_error_type)
  489. || (op[1]->type == glsl_error_type));
  490. type = arithmetic_result_type(op[0]->type, op[1]->type,
  491. (this->oper == ast_mul_assign),
  492. state);
  493. temp_rhs = new ir_expression(operations[this->oper], type,
  494. op[0], op[1]);
  495. /* FINISHME: Check that the LHS is assignable. */
  496. /* We still have to test that the LHS and RHS have matching type. For
  497. * example, the following GLSL code should generate a type error:
  498. *
  499. * mat4 m; vec4 v; m *= v;
  500. *
  501. * The type of (m*v) is a vec4, but the type of m is a mat4.
  502. *
  503. * FINISHME: Is multiplication between a matrix and a vector the only
  504. * FINISHME: case that resuls in mismatched types?
  505. */
  506. /* FINISHME: Check that the LHS and RHS have matching types. */
  507. /* GLSL 1.10 does not allow array assignment. However, we don't have to
  508. * explicitly test for this because none of the binary expression
  509. * operators allow array operands either.
  510. */
  511. /* FINISHME: This is wrong. The operation should assign to a new
  512. * FINISHME: temporary. This assignment should then be added to the
  513. * FINISHME: instruction list. Another assignment to the real
  514. * FINISHME: destination should be generated. The temporary should then
  515. * FINISHME: be returned as the r-value.
  516. */
  517. result = new ir_assignment(op[0], temp_rhs, NULL);
  518. break;
  519. }
  520. case ast_mod_assign:
  521. case ast_ls_assign:
  522. case ast_rs_assign:
  523. case ast_and_assign:
  524. case ast_xor_assign:
  525. case ast_or_assign:
  526. case ast_conditional:
  527. case ast_pre_inc:
  528. case ast_pre_dec:
  529. case ast_post_inc:
  530. case ast_post_dec:
  531. break;
  532. case ast_field_selection:
  533. result = _mesa_ast_field_selection_to_hir(this, instructions, state);
  534. type = result->type;
  535. break;
  536. case ast_array_index:
  537. break;
  538. case ast_function_call:
  539. /* Should *NEVER* get here. ast_function_call should always be handled
  540. * by ast_function_expression::hir.
  541. */
  542. assert(0);
  543. break;
  544. case ast_identifier: {
  545. /* ast_identifier can appear several places in a full abstract syntax
  546. * tree. This particular use must be at location specified in the grammar
  547. * as 'variable_identifier'.
  548. */
  549. ir_variable *var =
  550. state->symbols->get_variable(this->primary_expression.identifier);
  551. result = new ir_dereference(var);
  552. if (var != NULL) {
  553. type = result->type;
  554. } else {
  555. _mesa_glsl_error(& loc, NULL, "`%s' undeclared",
  556. this->primary_expression.identifier);
  557. error_emitted = true;
  558. }
  559. break;
  560. }
  561. case ast_int_constant:
  562. type = glsl_int_type;
  563. result = new ir_constant(type, & this->primary_expression);
  564. break;
  565. case ast_uint_constant:
  566. type = glsl_uint_type;
  567. result = new ir_constant(type, & this->primary_expression);
  568. break;
  569. case ast_float_constant:
  570. type = glsl_float_type;
  571. result = new ir_constant(type, & this->primary_expression);
  572. break;
  573. case ast_bool_constant:
  574. type = glsl_bool_type;
  575. result = new ir_constant(type, & this->primary_expression);
  576. break;
  577. case ast_sequence: {
  578. struct simple_node *ptr;
  579. /* It should not be possible to generate a sequence in the AST without
  580. * any expressions in it.
  581. */
  582. assert(!is_empty_list(&this->expressions));
  583. /* The r-value of a sequence is the last expression in the sequence. If
  584. * the other expressions in the sequence do not have side-effects (and
  585. * therefore add instructions to the instruction list), they get dropped
  586. * on the floor.
  587. */
  588. foreach (ptr, &this->expressions)
  589. result = ((ast_node *)ptr)->hir(instructions, state);
  590. type = result->type;
  591. /* Any errors should have already been emitted in the loop above.
  592. */
  593. error_emitted = true;
  594. break;
  595. }
  596. }
  597. if (is_error_type(type) && !error_emitted)
  598. _mesa_glsl_error(& loc, NULL, "type mismatch");
  599. return result;
  600. }
  601. ir_instruction *
  602. ast_expression_statement::hir(exec_list *instructions,
  603. struct _mesa_glsl_parse_state *state)
  604. {
  605. /* It is possible to have expression statements that don't have an
  606. * expression. This is the solitary semicolon:
  607. *
  608. * for (i = 0; i < 5; i++)
  609. * ;
  610. *
  611. * In this case the expression will be NULL. Test for NULL and don't do
  612. * anything in that case.
  613. */
  614. if (expression != NULL)
  615. expression->hir(instructions, state);
  616. /* Statements do not have r-values.
  617. */
  618. return NULL;
  619. }
  620. ir_instruction *
  621. ast_compound_statement::hir(exec_list *instructions,
  622. struct _mesa_glsl_parse_state *state)
  623. {
  624. struct simple_node *ptr;
  625. if (new_scope)
  626. state->symbols->push_scope();
  627. foreach (ptr, &statements)
  628. ((ast_node *)ptr)->hir(instructions, state);
  629. if (new_scope)
  630. state->symbols->pop_scope();
  631. /* Compound statements do not have r-values.
  632. */
  633. return NULL;
  634. }
  635. static const struct glsl_type *
  636. type_specifier_to_glsl_type(const struct ast_type_specifier *spec,
  637. const char **name,
  638. struct _mesa_glsl_parse_state *state)
  639. {
  640. struct glsl_type *type;
  641. if (spec->type_specifier == ast_struct) {
  642. /* FINISHME: Handle annonymous structures. */
  643. type = NULL;
  644. } else {
  645. type = state->symbols->get_type(spec->type_name);
  646. *name = spec->type_name;
  647. /* FINISHME: Handle array declarations. Note that this requires complete
  648. * FINSIHME: handling of constant expressions.
  649. */
  650. }
  651. return type;
  652. }
  653. static void
  654. apply_type_qualifier_to_variable(const struct ast_type_qualifier *qual,
  655. struct ir_variable *var,
  656. struct _mesa_glsl_parse_state *state)
  657. {
  658. if (qual->invariant)
  659. var->invariant = 1;
  660. /* FINISHME: Mark 'in' variables at global scope as read-only. */
  661. if (qual->constant || qual->attribute || qual->uniform
  662. || (qual->varying && (state->target == fragment_shader)))
  663. var->read_only = 1;
  664. if (qual->centroid)
  665. var->centroid = 1;
  666. if (qual->in && qual->out)
  667. var->mode = ir_var_inout;
  668. else if (qual->attribute || qual->in
  669. || (qual->varying && (state->target == fragment_shader)))
  670. var->mode = ir_var_in;
  671. else if (qual->out || (qual->varying && (state->target == vertex_shader)))
  672. var->mode = ir_var_out;
  673. else if (qual->uniform)
  674. var->mode = ir_var_uniform;
  675. else
  676. var->mode = ir_var_auto;
  677. if (qual->flat)
  678. var->interpolation = ir_var_flat;
  679. else if (qual->noperspective)
  680. var->interpolation = ir_var_noperspective;
  681. else
  682. var->interpolation = ir_var_smooth;
  683. }
  684. ir_instruction *
  685. ast_declarator_list::hir(exec_list *instructions,
  686. struct _mesa_glsl_parse_state *state)
  687. {
  688. struct simple_node *ptr;
  689. const struct glsl_type *decl_type;
  690. const char *type_name = NULL;
  691. /* FINISHME: Handle vertex shader "invariant" declarations that do not
  692. * FINISHME: include a type. These re-declare built-in variables to be
  693. * FINISHME: invariant.
  694. */
  695. decl_type = type_specifier_to_glsl_type(this->type->specifier,
  696. & type_name, state);
  697. foreach (ptr, &this->declarations) {
  698. struct ast_declaration *const decl = (struct ast_declaration * )ptr;
  699. const struct glsl_type *var_type;
  700. struct ir_variable *var;
  701. /* FINISHME: Emit a warning if a variable declaration shadows a
  702. * FINISHME: declaration at a higher scope.
  703. */
  704. if (decl_type == NULL) {
  705. YYLTYPE loc;
  706. loc = this->get_location();
  707. if (type_name != NULL) {
  708. _mesa_glsl_error(& loc, state,
  709. "invalid type `%s' in declaration of `%s'",
  710. type_name, decl->identifier);
  711. } else {
  712. _mesa_glsl_error(& loc, state,
  713. "invalid type in declaration of `%s'",
  714. decl->identifier);
  715. }
  716. continue;
  717. }
  718. if (decl->is_array) {
  719. /* FINISHME: Handle array declarations. Note that this requires
  720. * FINISHME: complete handling of constant expressions.
  721. */
  722. /* FINISHME: Reject delcarations of multidimensional arrays. */
  723. } else {
  724. var_type = decl_type;
  725. }
  726. var = new ir_variable(var_type, decl->identifier);
  727. /* FINSIHME: Variables that are attribute, uniform, varying, in, or
  728. * FINISHME: out varibles must be declared either at global scope or
  729. * FINISHME: in a parameter list (in and out only).
  730. */
  731. apply_type_qualifier_to_variable(& this->type->qualifier, var, state);
  732. /* Attempt to add the variable to the symbol table. If this fails, it
  733. * means the variable has already been declared at this scope.
  734. */
  735. if (!state->symbols->add_variable(decl->identifier, var)) {
  736. YYLTYPE loc = this->get_location();
  737. _mesa_glsl_error(& loc, state, "`%s' redeclared",
  738. decl->identifier);
  739. continue;
  740. }
  741. instructions->push_tail(var);
  742. /* FINISHME: Process the declaration initializer. */
  743. }
  744. /* Variable declarations do not have r-values.
  745. */
  746. return NULL;
  747. }
  748. ir_instruction *
  749. ast_parameter_declarator::hir(exec_list *instructions,
  750. struct _mesa_glsl_parse_state *state)
  751. {
  752. const struct glsl_type *type;
  753. const char *name = NULL;
  754. type = type_specifier_to_glsl_type(this->type->specifier, & name, state);
  755. if (type == NULL) {
  756. YYLTYPE loc = this->get_location();
  757. if (name != NULL) {
  758. _mesa_glsl_error(& loc, state,
  759. "invalid type `%s' in declaration of `%s'",
  760. name, this->identifier);
  761. } else {
  762. _mesa_glsl_error(& loc, state,
  763. "invalid type in declaration of `%s'",
  764. this->identifier);
  765. }
  766. type = glsl_error_type;
  767. }
  768. ir_variable *var = new ir_variable(type, this->identifier);
  769. /* FINISHME: Handle array declarations. Note that this requires
  770. * FINISHME: complete handling of constant expressions.
  771. */
  772. /* Apply any specified qualifiers to the parameter declaration. Note that
  773. * for function parameters the default mode is 'in'.
  774. */
  775. apply_type_qualifier_to_variable(& this->type->qualifier, var, state);
  776. if (var->mode == ir_var_auto)
  777. var->mode = ir_var_in;
  778. instructions->push_tail(var);
  779. /* Parameter declarations do not have r-values.
  780. */
  781. return NULL;
  782. }
  783. static void
  784. ast_function_parameters_to_hir(struct simple_node *ast_parameters,
  785. exec_list *ir_parameters,
  786. struct _mesa_glsl_parse_state *state)
  787. {
  788. struct simple_node *ptr;
  789. foreach (ptr, ast_parameters) {
  790. ((ast_node *)ptr)->hir(ir_parameters, state);
  791. }
  792. }
  793. static bool
  794. parameter_lists_match(exec_list *list_a, exec_list *list_b)
  795. {
  796. exec_list_iterator iter_a = list_a->iterator();
  797. exec_list_iterator iter_b = list_b->iterator();
  798. while (iter_a.has_next()) {
  799. /* If all of the parameters from the other parameter list have been
  800. * exhausted, the lists have different length and, by definition,
  801. * do not match.
  802. */
  803. if (!iter_b.has_next())
  804. return false;
  805. /* If the types of the parameters do not match, the parameters lists
  806. * are different.
  807. */
  808. /* FINISHME */
  809. iter_a.next();
  810. iter_b.next();
  811. }
  812. return true;
  813. }
  814. ir_instruction *
  815. ast_function_definition::hir(exec_list *instructions,
  816. struct _mesa_glsl_parse_state *state)
  817. {
  818. ir_label *label;
  819. ir_function_signature *signature = NULL;
  820. ir_function *f = NULL;
  821. exec_list parameters;
  822. /* Convert the list of function parameters to HIR now so that they can be
  823. * used below to compare this function's signature with previously seen
  824. * signatures for functions with the same name.
  825. */
  826. ast_function_parameters_to_hir(& this->prototype->parameters, & parameters,
  827. state);
  828. /* Verify that this function's signature either doesn't match a previously
  829. * seen signature for a function with the same name, or, if a match is found,
  830. * that the previously seen signature does not have an associated definition.
  831. */
  832. f = state->symbols->get_function(this->prototype->identifier);
  833. if (f != NULL) {
  834. foreach_iter(exec_list_iterator, iter, f->signatures) {
  835. signature = (struct ir_function_signature *) iter.get();
  836. /* Compare the parameter list of the function being defined to the
  837. * existing function. If the parameter lists match, then the return
  838. * type must also match and the existing function must not have a
  839. * definition.
  840. */
  841. if (parameter_lists_match(& parameters, & signature->parameters)) {
  842. /* FINISHME: Compare return types. */
  843. if (signature->definition != NULL) {
  844. YYLTYPE loc = this->get_location();
  845. _mesa_glsl_error(& loc, state, "function `%s' redefined",
  846. this->prototype->identifier);
  847. signature = NULL;
  848. break;
  849. }
  850. }
  851. signature = NULL;
  852. }
  853. } else {
  854. f = new ir_function();
  855. f->name = this->prototype->identifier;
  856. state->symbols->add_function(f->name, f);
  857. }
  858. /* Finish storing the information about this new function in its signature.
  859. */
  860. if (signature == NULL) {
  861. signature = new ir_function_signature();
  862. f->signatures.push_tail(signature);
  863. } else {
  864. /* Destroy all of the previous parameter information. The previous
  865. * parameter information comes from the function prototype, and it can
  866. * either include invalid parameter names or may not have names at all.
  867. */
  868. foreach_iter(exec_list_iterator, iter, signature->parameters) {
  869. assert(((struct ir_instruction *)iter.get())->mode == ir_op_var_decl);
  870. iter.remove();
  871. delete iter.get();
  872. }
  873. }
  874. ast_function_parameters_to_hir(& this->prototype->parameters,
  875. & signature->parameters,
  876. state);
  877. /* FINISHME: Set signature->return_type */
  878. label = new ir_label(this->prototype->identifier);
  879. if (signature->definition == NULL) {
  880. signature->definition = label;
  881. }
  882. instructions->push_tail(label);
  883. /* Add the function parameters to the symbol table. During this step the
  884. * parameter declarations are also moved from the temporary "parameters" list
  885. * to the instruction list. There are other more efficient ways to do this,
  886. * but they involve ugly linked-list gymnastics.
  887. */
  888. state->symbols->push_scope();
  889. foreach_iter(exec_list_iterator, iter, parameters) {
  890. ir_variable *const var = (ir_variable *) iter.get();
  891. assert(((ir_instruction *)var)->mode == ir_op_var_decl);
  892. iter.remove();
  893. instructions->push_tail(var);
  894. state->symbols->add_variable(var->name, var);
  895. }
  896. /* Convert the body of the function to HIR, and append the resulting
  897. * instructions to the list that currently consists of the function label
  898. * and the function parameters.
  899. */
  900. this->body->hir(instructions, state);
  901. state->symbols->pop_scope();
  902. /* Function definitions do not have r-values.
  903. */
  904. return NULL;
  905. }