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 67KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184
  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. _mesa_glsl_initialize_constructors(instructions, state);
  63. _mesa_glsl_initialize_functions(instructions, state);
  64. state->current_function = NULL;
  65. foreach (ptr, & state->translation_unit) {
  66. ((ast_node *)ptr)->hir(instructions, state);
  67. }
  68. }
  69. /**
  70. * If a conversion is available, convert one operand to a different type
  71. *
  72. * The \c from \c ir_rvalue is converted "in place".
  73. *
  74. * \param to Type that the operand it to be converted to
  75. * \param from Operand that is being converted
  76. * \param state GLSL compiler state
  77. *
  78. * \return
  79. * If a conversion is possible (or unnecessary), \c true is returned.
  80. * Otherwise \c false is returned.
  81. */
  82. static bool
  83. apply_implicit_conversion(const glsl_type *to, ir_rvalue * &from,
  84. struct _mesa_glsl_parse_state *state)
  85. {
  86. if (to->base_type == from->type->base_type)
  87. return true;
  88. /* This conversion was added in GLSL 1.20. If the compilation mode is
  89. * GLSL 1.10, the conversion is skipped.
  90. */
  91. if (state->language_version < 120)
  92. return false;
  93. /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec:
  94. *
  95. * "There are no implicit array or structure conversions. For
  96. * example, an array of int cannot be implicitly converted to an
  97. * array of float. There are no implicit conversions between
  98. * signed and unsigned integers."
  99. */
  100. /* FINISHME: The above comment is partially a lie. There is int/uint
  101. * FINISHME: conversion for immediate constants.
  102. */
  103. if (!to->is_float() || !from->type->is_numeric())
  104. return false;
  105. switch (from->type->base_type) {
  106. case GLSL_TYPE_INT:
  107. from = new ir_expression(ir_unop_i2f, to, from, NULL);
  108. break;
  109. case GLSL_TYPE_UINT:
  110. from = new ir_expression(ir_unop_u2f, to, from, NULL);
  111. break;
  112. case GLSL_TYPE_BOOL:
  113. from = new ir_expression(ir_unop_b2f, to, from, NULL);
  114. break;
  115. default:
  116. assert(0);
  117. }
  118. return true;
  119. }
  120. static const struct glsl_type *
  121. arithmetic_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
  122. bool multiply,
  123. struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
  124. {
  125. const glsl_type *const type_a = value_a->type;
  126. const glsl_type *const type_b = value_b->type;
  127. /* From GLSL 1.50 spec, page 56:
  128. *
  129. * "The arithmetic binary operators add (+), subtract (-),
  130. * multiply (*), and divide (/) operate on integer and
  131. * floating-point scalars, vectors, and matrices."
  132. */
  133. if (!type_a->is_numeric() || !type_b->is_numeric()) {
  134. _mesa_glsl_error(loc, state,
  135. "Operands to arithmetic operators must be numeric");
  136. return glsl_type::error_type;
  137. }
  138. /* "If one operand is floating-point based and the other is
  139. * not, then the conversions from Section 4.1.10 "Implicit
  140. * Conversions" are applied to the non-floating-point-based operand."
  141. */
  142. if (!apply_implicit_conversion(type_a, value_b, state)
  143. && !apply_implicit_conversion(type_b, value_a, state)) {
  144. _mesa_glsl_error(loc, state,
  145. "Could not implicitly convert operands to "
  146. "arithmetic operator");
  147. return glsl_type::error_type;
  148. }
  149. /* "If the operands are integer types, they must both be signed or
  150. * both be unsigned."
  151. *
  152. * From this rule and the preceeding conversion it can be inferred that
  153. * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
  154. * The is_numeric check above already filtered out the case where either
  155. * type is not one of these, so now the base types need only be tested for
  156. * equality.
  157. */
  158. if (type_a->base_type != type_b->base_type) {
  159. _mesa_glsl_error(loc, state,
  160. "base type mismatch for arithmetic operator");
  161. return glsl_type::error_type;
  162. }
  163. /* "All arithmetic binary operators result in the same fundamental type
  164. * (signed integer, unsigned integer, or floating-point) as the
  165. * operands they operate on, after operand type conversion. After
  166. * conversion, the following cases are valid
  167. *
  168. * * The two operands are scalars. In this case the operation is
  169. * applied, resulting in a scalar."
  170. */
  171. if (type_a->is_scalar() && type_b->is_scalar())
  172. return type_a;
  173. /* "* One operand is a scalar, and the other is a vector or matrix.
  174. * In this case, the scalar operation is applied independently to each
  175. * component of the vector or matrix, resulting in the same size
  176. * vector or matrix."
  177. */
  178. if (type_a->is_scalar()) {
  179. if (!type_b->is_scalar())
  180. return type_b;
  181. } else if (type_b->is_scalar()) {
  182. return type_a;
  183. }
  184. /* All of the combinations of <scalar, scalar>, <vector, scalar>,
  185. * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
  186. * handled.
  187. */
  188. assert(!type_a->is_scalar());
  189. assert(!type_b->is_scalar());
  190. /* "* The two operands are vectors of the same size. In this case, the
  191. * operation is done component-wise resulting in the same size
  192. * vector."
  193. */
  194. if (type_a->is_vector() && type_b->is_vector()) {
  195. if (type_a == type_b) {
  196. return type_a;
  197. } else {
  198. _mesa_glsl_error(loc, state,
  199. "vector size mismatch for arithmetic operator");
  200. return glsl_type::error_type;
  201. }
  202. }
  203. /* All of the combinations of <scalar, scalar>, <vector, scalar>,
  204. * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
  205. * <vector, vector> have been handled. At least one of the operands must
  206. * be matrix. Further, since there are no integer matrix types, the base
  207. * type of both operands must be float.
  208. */
  209. assert(type_a->is_matrix() || type_b->is_matrix());
  210. assert(type_a->base_type == GLSL_TYPE_FLOAT);
  211. assert(type_b->base_type == GLSL_TYPE_FLOAT);
  212. /* "* The operator is add (+), subtract (-), or divide (/), and the
  213. * operands are matrices with the same number of rows and the same
  214. * number of columns. In this case, the operation is done component-
  215. * wise resulting in the same size matrix."
  216. * * The operator is multiply (*), where both operands are matrices or
  217. * one operand is a vector and the other a matrix. A right vector
  218. * operand is treated as a column vector and a left vector operand as a
  219. * row vector. In all these cases, it is required that the number of
  220. * columns of the left operand is equal to the number of rows of the
  221. * right operand. Then, the multiply (*) operation does a linear
  222. * algebraic multiply, yielding an object that has the same number of
  223. * rows as the left operand and the same number of columns as the right
  224. * operand. Section 5.10 "Vector and Matrix Operations" explains in
  225. * more detail how vectors and matrices are operated on."
  226. */
  227. if (! multiply) {
  228. if (type_a == type_b)
  229. return type_a;
  230. } else {
  231. if (type_a->is_matrix() && type_b->is_matrix()) {
  232. /* Matrix multiply. The columns of A must match the rows of B. Given
  233. * the other previously tested constraints, this means the vector type
  234. * of a row from A must be the same as the vector type of a column from
  235. * B.
  236. */
  237. if (type_a->row_type() == type_b->column_type()) {
  238. /* The resulting matrix has the number of columns of matrix B and
  239. * the number of rows of matrix A. We get the row count of A by
  240. * looking at the size of a vector that makes up a column. The
  241. * transpose (size of a row) is done for B.
  242. */
  243. const glsl_type *const type =
  244. glsl_type::get_instance(type_a->base_type,
  245. type_a->column_type()->vector_elements,
  246. type_b->row_type()->vector_elements);
  247. assert(type != glsl_type::error_type);
  248. return type;
  249. }
  250. } else if (type_a->is_matrix()) {
  251. /* A is a matrix and B is a column vector. Columns of A must match
  252. * rows of B. Given the other previously tested constraints, this
  253. * means the vector type of a row from A must be the same as the
  254. * vector the type of B.
  255. */
  256. if (type_a->row_type() == type_b)
  257. return type_b;
  258. } else {
  259. assert(type_b->is_matrix());
  260. /* A is a row vector and B is a matrix. Columns of A must match rows
  261. * of B. Given the other previously tested constraints, this means
  262. * the type of A must be the same as the vector type of a column from
  263. * B.
  264. */
  265. if (type_a == type_b->column_type())
  266. return type_a;
  267. }
  268. _mesa_glsl_error(loc, state, "size mismatch for matrix multiplication");
  269. return glsl_type::error_type;
  270. }
  271. /* "All other cases are illegal."
  272. */
  273. _mesa_glsl_error(loc, state, "type mismatch");
  274. return glsl_type::error_type;
  275. }
  276. static const struct glsl_type *
  277. unary_arithmetic_result_type(const struct glsl_type *type,
  278. struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
  279. {
  280. /* From GLSL 1.50 spec, page 57:
  281. *
  282. * "The arithmetic unary operators negate (-), post- and pre-increment
  283. * and decrement (-- and ++) operate on integer or floating-point
  284. * values (including vectors and matrices). All unary operators work
  285. * component-wise on their operands. These result with the same type
  286. * they operated on."
  287. */
  288. if (!type->is_numeric()) {
  289. _mesa_glsl_error(loc, state,
  290. "Operands to arithmetic operators must be numeric");
  291. return glsl_type::error_type;
  292. }
  293. return type;
  294. }
  295. static const struct glsl_type *
  296. modulus_result_type(const struct glsl_type *type_a,
  297. const struct glsl_type *type_b,
  298. struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
  299. {
  300. /* From GLSL 1.50 spec, page 56:
  301. * "The operator modulus (%) operates on signed or unsigned integers or
  302. * integer vectors. The operand types must both be signed or both be
  303. * unsigned."
  304. */
  305. if (!type_a->is_integer() || !type_b->is_integer()
  306. || (type_a->base_type != type_b->base_type)) {
  307. _mesa_glsl_error(loc, state, "type mismatch");
  308. return glsl_type::error_type;
  309. }
  310. /* "The operands cannot be vectors of differing size. If one operand is
  311. * a scalar and the other vector, then the scalar is applied component-
  312. * wise to the vector, resulting in the same type as the vector. If both
  313. * are vectors of the same size, the result is computed component-wise."
  314. */
  315. if (type_a->is_vector()) {
  316. if (!type_b->is_vector()
  317. || (type_a->vector_elements == type_b->vector_elements))
  318. return type_a;
  319. } else
  320. return type_b;
  321. /* "The operator modulus (%) is not defined for any other data types
  322. * (non-integer types)."
  323. */
  324. _mesa_glsl_error(loc, state, "type mismatch");
  325. return glsl_type::error_type;
  326. }
  327. static const struct glsl_type *
  328. relational_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
  329. struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
  330. {
  331. const glsl_type *const type_a = value_a->type;
  332. const glsl_type *const type_b = value_b->type;
  333. /* From GLSL 1.50 spec, page 56:
  334. * "The relational operators greater than (>), less than (<), greater
  335. * than or equal (>=), and less than or equal (<=) operate only on
  336. * scalar integer and scalar floating-point expressions."
  337. */
  338. if (!type_a->is_numeric()
  339. || !type_b->is_numeric()
  340. || !type_a->is_scalar()
  341. || !type_b->is_scalar()) {
  342. _mesa_glsl_error(loc, state,
  343. "Operands to relational operators must be scalar and "
  344. "numeric");
  345. return glsl_type::error_type;
  346. }
  347. /* "Either the operands' types must match, or the conversions from
  348. * Section 4.1.10 "Implicit Conversions" will be applied to the integer
  349. * operand, after which the types must match."
  350. */
  351. if (!apply_implicit_conversion(type_a, value_b, state)
  352. && !apply_implicit_conversion(type_b, value_a, state)) {
  353. _mesa_glsl_error(loc, state,
  354. "Could not implicitly convert operands to "
  355. "relational operator");
  356. return glsl_type::error_type;
  357. }
  358. if (type_a->base_type != type_b->base_type) {
  359. _mesa_glsl_error(loc, state, "base type mismatch");
  360. return glsl_type::error_type;
  361. }
  362. /* "The result is scalar Boolean."
  363. */
  364. return glsl_type::bool_type;
  365. }
  366. /**
  367. * Validates that a value can be assigned to a location with a specified type
  368. *
  369. * Validates that \c rhs can be assigned to some location. If the types are
  370. * not an exact match but an automatic conversion is possible, \c rhs will be
  371. * converted.
  372. *
  373. * \return
  374. * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
  375. * Otherwise the actual RHS to be assigned will be returned. This may be
  376. * \c rhs, or it may be \c rhs after some type conversion.
  377. *
  378. * \note
  379. * In addition to being used for assignments, this function is used to
  380. * type-check return values.
  381. */
  382. ir_rvalue *
  383. validate_assignment(const glsl_type *lhs_type, ir_rvalue *rhs)
  384. {
  385. const glsl_type *const rhs_type = rhs->type;
  386. /* If there is already some error in the RHS, just return it. Anything
  387. * else will lead to an avalanche of error message back to the user.
  388. */
  389. if (rhs_type->is_error())
  390. return rhs;
  391. /* If the types are identical, the assignment can trivially proceed.
  392. */
  393. if (rhs_type == lhs_type)
  394. return rhs;
  395. /* If the array element types are the same and the size of the LHS is zero,
  396. * the assignment is okay.
  397. *
  398. * Note: Whole-array assignments are not permitted in GLSL 1.10, but this
  399. * is handled by ir_dereference::is_lvalue.
  400. */
  401. if (lhs_type->is_array() && rhs->type->is_array()
  402. && (lhs_type->element_type() == rhs->type->element_type())
  403. && (lhs_type->array_size() == 0)) {
  404. return rhs;
  405. }
  406. /* FINISHME: Check for and apply automatic conversions. */
  407. return NULL;
  408. }
  409. ir_rvalue *
  410. do_assignment(exec_list *instructions, struct _mesa_glsl_parse_state *state,
  411. ir_rvalue *lhs, ir_rvalue *rhs,
  412. YYLTYPE lhs_loc)
  413. {
  414. bool error_emitted = (lhs->type->is_error() || rhs->type->is_error());
  415. if (!error_emitted) {
  416. /* FINISHME: This does not handle 'foo.bar.a.b.c[5].d = 5' */
  417. if (!lhs->is_lvalue()) {
  418. _mesa_glsl_error(& lhs_loc, state, "non-lvalue in assignment");
  419. error_emitted = true;
  420. }
  421. }
  422. ir_rvalue *new_rhs = validate_assignment(lhs->type, rhs);
  423. if (new_rhs == NULL) {
  424. _mesa_glsl_error(& lhs_loc, state, "type mismatch");
  425. } else {
  426. rhs = new_rhs;
  427. /* If the LHS array was not declared with a size, it takes it size from
  428. * the RHS. If the LHS is an l-value and a whole array, it must be a
  429. * dereference of a variable. Any other case would require that the LHS
  430. * is either not an l-value or not a whole array.
  431. */
  432. if (lhs->type->array_size() == 0) {
  433. ir_dereference *const d = lhs->as_dereference();
  434. assert(d != NULL);
  435. ir_variable *const var = d->var->as_variable();
  436. assert(var != NULL);
  437. if (var->max_array_access >= unsigned(rhs->type->array_size())) {
  438. /* FINISHME: This should actually log the location of the RHS. */
  439. _mesa_glsl_error(& lhs_loc, state, "array size must be > %u due to "
  440. "previous access",
  441. var->max_array_access);
  442. }
  443. var->type = glsl_type::get_array_instance(lhs->type->element_type(),
  444. rhs->type->array_size());
  445. }
  446. }
  447. ir_instruction *tmp = new ir_assignment(lhs, rhs, NULL);
  448. instructions->push_tail(tmp);
  449. return rhs;
  450. }
  451. /**
  452. * Generate a new temporary and add its declaration to the instruction stream
  453. */
  454. static ir_variable *
  455. generate_temporary(const glsl_type *type, exec_list *instructions,
  456. struct _mesa_glsl_parse_state *state)
  457. {
  458. char *name = (char *) malloc(sizeof(char) * 13);
  459. snprintf(name, 13, "tmp_%08X", state->temp_index);
  460. state->temp_index++;
  461. ir_variable *const var = new ir_variable(type, name);
  462. instructions->push_tail(var);
  463. return var;
  464. }
  465. static ir_rvalue *
  466. get_lvalue_copy(exec_list *instructions, struct _mesa_glsl_parse_state *state,
  467. ir_rvalue *lvalue, YYLTYPE loc)
  468. {
  469. ir_variable *var;
  470. ir_rvalue *var_deref;
  471. /* FINISHME: Give unique names to the temporaries. */
  472. var = new ir_variable(lvalue->type, "_internal_tmp");
  473. var->mode = ir_var_auto;
  474. var_deref = new ir_dereference(var);
  475. do_assignment(instructions, state, var_deref, lvalue, loc);
  476. /* Once we've created this temporary, mark it read only so it's no
  477. * longer considered an lvalue.
  478. */
  479. var->read_only = true;
  480. return var_deref;
  481. }
  482. ir_rvalue *
  483. ast_node::hir(exec_list *instructions,
  484. struct _mesa_glsl_parse_state *state)
  485. {
  486. (void) instructions;
  487. (void) state;
  488. return NULL;
  489. }
  490. ir_rvalue *
  491. ast_expression::hir(exec_list *instructions,
  492. struct _mesa_glsl_parse_state *state)
  493. {
  494. static const int operations[AST_NUM_OPERATORS] = {
  495. -1, /* ast_assign doesn't convert to ir_expression. */
  496. -1, /* ast_plus doesn't convert to ir_expression. */
  497. ir_unop_neg,
  498. ir_binop_add,
  499. ir_binop_sub,
  500. ir_binop_mul,
  501. ir_binop_div,
  502. ir_binop_mod,
  503. ir_binop_lshift,
  504. ir_binop_rshift,
  505. ir_binop_less,
  506. ir_binop_greater,
  507. ir_binop_lequal,
  508. ir_binop_gequal,
  509. ir_binop_equal,
  510. ir_binop_nequal,
  511. ir_binop_bit_and,
  512. ir_binop_bit_xor,
  513. ir_binop_bit_or,
  514. ir_unop_bit_not,
  515. ir_binop_logic_and,
  516. ir_binop_logic_xor,
  517. ir_binop_logic_or,
  518. ir_unop_logic_not,
  519. /* Note: The following block of expression types actually convert
  520. * to multiple IR instructions.
  521. */
  522. ir_binop_mul, /* ast_mul_assign */
  523. ir_binop_div, /* ast_div_assign */
  524. ir_binop_mod, /* ast_mod_assign */
  525. ir_binop_add, /* ast_add_assign */
  526. ir_binop_sub, /* ast_sub_assign */
  527. ir_binop_lshift, /* ast_ls_assign */
  528. ir_binop_rshift, /* ast_rs_assign */
  529. ir_binop_bit_and, /* ast_and_assign */
  530. ir_binop_bit_xor, /* ast_xor_assign */
  531. ir_binop_bit_or, /* ast_or_assign */
  532. -1, /* ast_conditional doesn't convert to ir_expression. */
  533. ir_binop_add, /* ast_pre_inc. */
  534. ir_binop_sub, /* ast_pre_dec. */
  535. ir_binop_add, /* ast_post_inc. */
  536. ir_binop_sub, /* ast_post_dec. */
  537. -1, /* ast_field_selection doesn't conv to ir_expression. */
  538. -1, /* ast_array_index doesn't convert to ir_expression. */
  539. -1, /* ast_function_call doesn't conv to ir_expression. */
  540. -1, /* ast_identifier doesn't convert to ir_expression. */
  541. -1, /* ast_int_constant doesn't convert to ir_expression. */
  542. -1, /* ast_uint_constant doesn't conv to ir_expression. */
  543. -1, /* ast_float_constant doesn't conv to ir_expression. */
  544. -1, /* ast_bool_constant doesn't conv to ir_expression. */
  545. -1, /* ast_sequence doesn't convert to ir_expression. */
  546. };
  547. ir_rvalue *result = NULL;
  548. ir_rvalue *op[2];
  549. struct simple_node op_list;
  550. const struct glsl_type *type = glsl_type::error_type;
  551. bool error_emitted = false;
  552. YYLTYPE loc;
  553. loc = this->get_location();
  554. make_empty_list(& op_list);
  555. switch (this->oper) {
  556. case ast_assign: {
  557. op[0] = this->subexpressions[0]->hir(instructions, state);
  558. op[1] = this->subexpressions[1]->hir(instructions, state);
  559. result = do_assignment(instructions, state, op[0], op[1],
  560. this->subexpressions[0]->get_location());
  561. error_emitted = result->type->is_error();
  562. type = result->type;
  563. break;
  564. }
  565. case ast_plus:
  566. op[0] = this->subexpressions[0]->hir(instructions, state);
  567. error_emitted = op[0]->type->is_error();
  568. if (type->is_error())
  569. op[0]->type = type;
  570. result = op[0];
  571. break;
  572. case ast_neg:
  573. op[0] = this->subexpressions[0]->hir(instructions, state);
  574. type = unary_arithmetic_result_type(op[0]->type, state, & loc);
  575. error_emitted = type->is_error();
  576. result = new ir_expression(operations[this->oper], type,
  577. op[0], NULL);
  578. break;
  579. case ast_add:
  580. case ast_sub:
  581. case ast_mul:
  582. case ast_div:
  583. op[0] = this->subexpressions[0]->hir(instructions, state);
  584. op[1] = this->subexpressions[1]->hir(instructions, state);
  585. type = arithmetic_result_type(op[0], op[1],
  586. (this->oper == ast_mul),
  587. state, & loc);
  588. error_emitted = type->is_error();
  589. result = new ir_expression(operations[this->oper], type,
  590. op[0], op[1]);
  591. break;
  592. case ast_mod:
  593. op[0] = this->subexpressions[0]->hir(instructions, state);
  594. op[1] = this->subexpressions[1]->hir(instructions, state);
  595. type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
  596. assert(operations[this->oper] == ir_binop_mod);
  597. result = new ir_expression(operations[this->oper], type,
  598. op[0], op[1]);
  599. error_emitted = type->is_error();
  600. break;
  601. case ast_lshift:
  602. case ast_rshift:
  603. _mesa_glsl_error(& loc, state, "FINISHME: implement bit-shift operators");
  604. error_emitted = true;
  605. break;
  606. case ast_less:
  607. case ast_greater:
  608. case ast_lequal:
  609. case ast_gequal:
  610. op[0] = this->subexpressions[0]->hir(instructions, state);
  611. op[1] = this->subexpressions[1]->hir(instructions, state);
  612. type = relational_result_type(op[0], op[1], state, & loc);
  613. /* The relational operators must either generate an error or result
  614. * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
  615. */
  616. assert(type->is_error()
  617. || ((type->base_type == GLSL_TYPE_BOOL)
  618. && type->is_scalar()));
  619. result = new ir_expression(operations[this->oper], type,
  620. op[0], op[1]);
  621. error_emitted = type->is_error();
  622. break;
  623. case ast_nequal:
  624. case ast_equal:
  625. op[0] = this->subexpressions[0]->hir(instructions, state);
  626. op[1] = this->subexpressions[1]->hir(instructions, state);
  627. /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
  628. *
  629. * "The equality operators equal (==), and not equal (!=)
  630. * operate on all types. They result in a scalar Boolean. If
  631. * the operand types do not match, then there must be a
  632. * conversion from Section 4.1.10 "Implicit Conversions"
  633. * applied to one operand that can make them match, in which
  634. * case this conversion is done."
  635. */
  636. if ((!apply_implicit_conversion(op[0]->type, op[1], state)
  637. && !apply_implicit_conversion(op[1]->type, op[0], state))
  638. || (op[0]->type != op[1]->type)) {
  639. _mesa_glsl_error(& loc, state, "operands of `%s' must have the same "
  640. "type", (this->oper == ast_equal) ? "==" : "!=");
  641. error_emitted = true;
  642. } else if ((state->language_version <= 110)
  643. && (op[0]->type->is_array() || op[1]->type->is_array())) {
  644. _mesa_glsl_error(& loc, state, "array comparisons forbidden in "
  645. "GLSL 1.10");
  646. error_emitted = true;
  647. }
  648. result = new ir_expression(operations[this->oper], glsl_type::bool_type,
  649. op[0], op[1]);
  650. type = glsl_type::bool_type;
  651. assert(result->type == glsl_type::bool_type);
  652. break;
  653. case ast_bit_and:
  654. case ast_bit_xor:
  655. case ast_bit_or:
  656. case ast_bit_not:
  657. _mesa_glsl_error(& loc, state, "FINISHME: implement bit-wise operators");
  658. error_emitted = true;
  659. break;
  660. case ast_logic_and:
  661. case ast_logic_xor:
  662. case ast_logic_or:
  663. op[0] = this->subexpressions[0]->hir(instructions, state);
  664. op[1] = this->subexpressions[1]->hir(instructions, state);
  665. if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
  666. YYLTYPE loc = this->subexpressions[0]->get_location();
  667. _mesa_glsl_error(& loc, state, "LHS of `%s' must be scalar boolean",
  668. operator_string(this->oper));
  669. error_emitted = true;
  670. }
  671. if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
  672. YYLTYPE loc = this->subexpressions[1]->get_location();
  673. _mesa_glsl_error(& loc, state, "RHS of `%s' must be scalar boolean",
  674. operator_string(this->oper));
  675. error_emitted = true;
  676. }
  677. result = new ir_expression(operations[this->oper], glsl_type::bool_type,
  678. op[0], op[1]);
  679. type = glsl_type::bool_type;
  680. break;
  681. case ast_logic_not:
  682. op[0] = this->subexpressions[0]->hir(instructions, state);
  683. if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
  684. YYLTYPE loc = this->subexpressions[0]->get_location();
  685. _mesa_glsl_error(& loc, state,
  686. "operand of `!' must be scalar boolean");
  687. error_emitted = true;
  688. }
  689. result = new ir_expression(operations[this->oper], glsl_type::bool_type,
  690. op[0], NULL);
  691. type = glsl_type::bool_type;
  692. break;
  693. case ast_mul_assign:
  694. case ast_div_assign:
  695. case ast_add_assign:
  696. case ast_sub_assign: {
  697. op[0] = this->subexpressions[0]->hir(instructions, state);
  698. op[1] = this->subexpressions[1]->hir(instructions, state);
  699. type = arithmetic_result_type(op[0], op[1],
  700. (this->oper == ast_mul_assign),
  701. state, & loc);
  702. ir_rvalue *temp_rhs = new ir_expression(operations[this->oper], type,
  703. op[0], op[1]);
  704. result = do_assignment(instructions, state, op[0], temp_rhs,
  705. this->subexpressions[0]->get_location());
  706. type = result->type;
  707. error_emitted = (op[0]->type->is_error());
  708. /* GLSL 1.10 does not allow array assignment. However, we don't have to
  709. * explicitly test for this because none of the binary expression
  710. * operators allow array operands either.
  711. */
  712. break;
  713. }
  714. case ast_mod_assign: {
  715. op[0] = this->subexpressions[0]->hir(instructions, state);
  716. op[1] = this->subexpressions[1]->hir(instructions, state);
  717. type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
  718. assert(operations[this->oper] == ir_binop_mod);
  719. struct ir_rvalue *temp_rhs;
  720. temp_rhs = new ir_expression(operations[this->oper], type,
  721. op[0], op[1]);
  722. result = do_assignment(instructions, state, op[0], temp_rhs,
  723. this->subexpressions[0]->get_location());
  724. type = result->type;
  725. error_emitted = type->is_error();
  726. break;
  727. }
  728. case ast_ls_assign:
  729. case ast_rs_assign:
  730. _mesa_glsl_error(& loc, state,
  731. "FINISHME: implement bit-shift assignment operators");
  732. error_emitted = true;
  733. break;
  734. case ast_and_assign:
  735. case ast_xor_assign:
  736. case ast_or_assign:
  737. _mesa_glsl_error(& loc, state,
  738. "FINISHME: implement logic assignment operators");
  739. error_emitted = true;
  740. break;
  741. case ast_conditional: {
  742. op[0] = this->subexpressions[0]->hir(instructions, state);
  743. /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
  744. *
  745. * "The ternary selection operator (?:). It operates on three
  746. * expressions (exp1 ? exp2 : exp3). This operator evaluates the
  747. * first expression, which must result in a scalar Boolean."
  748. */
  749. if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
  750. YYLTYPE loc = this->subexpressions[0]->get_location();
  751. _mesa_glsl_error(& loc, state, "?: condition must be scalar boolean");
  752. error_emitted = true;
  753. }
  754. /* The :? operator is implemented by generating an anonymous temporary
  755. * followed by an if-statement. The last instruction in each branch of
  756. * the if-statement assigns a value to the anonymous temporary. This
  757. * temporary is the r-value of the expression.
  758. */
  759. ir_variable *const tmp = generate_temporary(glsl_type::error_type,
  760. instructions, state);
  761. ir_if *const stmt = new ir_if(op[0]);
  762. instructions->push_tail(stmt);
  763. op[1] = this->subexpressions[1]->hir(& stmt->then_instructions, state);
  764. ir_dereference *const then_deref = new ir_dereference(tmp);
  765. ir_assignment *const then_assign =
  766. new ir_assignment(then_deref, op[1], NULL);
  767. stmt->then_instructions.push_tail(then_assign);
  768. op[2] = this->subexpressions[2]->hir(& stmt->else_instructions, state);
  769. ir_dereference *const else_deref = new ir_dereference(tmp);
  770. ir_assignment *const else_assign =
  771. new ir_assignment(else_deref, op[2], NULL);
  772. stmt->else_instructions.push_tail(else_assign);
  773. /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
  774. *
  775. * "The second and third expressions can be any type, as
  776. * long their types match, or there is a conversion in
  777. * Section 4.1.10 "Implicit Conversions" that can be applied
  778. * to one of the expressions to make their types match. This
  779. * resulting matching type is the type of the entire
  780. * expression."
  781. */
  782. if ((!apply_implicit_conversion(op[1]->type, op[2], state)
  783. && !apply_implicit_conversion(op[2]->type, op[1], state))
  784. || (op[1]->type != op[2]->type)) {
  785. YYLTYPE loc = this->subexpressions[1]->get_location();
  786. _mesa_glsl_error(& loc, state, "Second and third operands of ?: "
  787. "operator must have matching types.");
  788. error_emitted = true;
  789. } else {
  790. tmp->type = op[1]->type;
  791. }
  792. result = new ir_dereference(tmp);
  793. type = tmp->type;
  794. break;
  795. }
  796. case ast_pre_inc:
  797. case ast_pre_dec: {
  798. op[0] = this->subexpressions[0]->hir(instructions, state);
  799. if (op[0]->type->base_type == GLSL_TYPE_FLOAT)
  800. op[1] = new ir_constant(1.0f);
  801. else
  802. op[1] = new ir_constant(1);
  803. type = arithmetic_result_type(op[0], op[1], false, state, & loc);
  804. struct ir_rvalue *temp_rhs;
  805. temp_rhs = new ir_expression(operations[this->oper], type,
  806. op[0], op[1]);
  807. result = do_assignment(instructions, state, op[0], temp_rhs,
  808. this->subexpressions[0]->get_location());
  809. type = result->type;
  810. error_emitted = op[0]->type->is_error();
  811. break;
  812. }
  813. case ast_post_inc:
  814. case ast_post_dec: {
  815. op[0] = this->subexpressions[0]->hir(instructions, state);
  816. if (op[0]->type->base_type == GLSL_TYPE_FLOAT)
  817. op[1] = new ir_constant(1.0f);
  818. else
  819. op[1] = new ir_constant(1);
  820. error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
  821. type = arithmetic_result_type(op[0], op[1], false, state, & loc);
  822. struct ir_rvalue *temp_rhs;
  823. temp_rhs = new ir_expression(operations[this->oper], type,
  824. op[0], op[1]);
  825. /* Get a temporary of a copy of the lvalue before it's modified.
  826. * This may get thrown away later.
  827. */
  828. result = get_lvalue_copy(instructions, state, op[0],
  829. this->subexpressions[0]->get_location());
  830. (void)do_assignment(instructions, state, op[0], temp_rhs,
  831. this->subexpressions[0]->get_location());
  832. type = result->type;
  833. error_emitted = op[0]->type->is_error();
  834. break;
  835. }
  836. case ast_field_selection:
  837. result = _mesa_ast_field_selection_to_hir(this, instructions, state);
  838. type = result->type;
  839. break;
  840. case ast_array_index: {
  841. YYLTYPE index_loc = subexpressions[1]->get_location();
  842. op[0] = subexpressions[0]->hir(instructions, state);
  843. op[1] = subexpressions[1]->hir(instructions, state);
  844. error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
  845. ir_dereference *const lhs = op[0]->as_dereference();
  846. ir_instruction *array;
  847. if ((lhs != NULL)
  848. && (lhs->mode == ir_dereference::ir_reference_variable)) {
  849. result = new ir_dereference(lhs->var, op[1]);
  850. delete op[0];
  851. array = lhs->var;
  852. } else {
  853. result = new ir_dereference(op[0], op[1]);
  854. array = op[0];
  855. }
  856. /* Do not use op[0] after this point. Use array.
  857. */
  858. op[0] = NULL;
  859. if (error_emitted)
  860. break;
  861. if (!array->type->is_array()
  862. && !array->type->is_matrix()
  863. && !array->type->is_vector()) {
  864. _mesa_glsl_error(& index_loc, state,
  865. "cannot dereference non-array / non-matrix / "
  866. "non-vector");
  867. error_emitted = true;
  868. }
  869. if (!op[1]->type->is_integer()) {
  870. _mesa_glsl_error(& index_loc, state,
  871. "array index must be integer type");
  872. error_emitted = true;
  873. } else if (!op[1]->type->is_scalar()) {
  874. _mesa_glsl_error(& index_loc, state,
  875. "array index must be scalar");
  876. error_emitted = true;
  877. }
  878. /* If the array index is a constant expression and the array has a
  879. * declared size, ensure that the access is in-bounds. If the array
  880. * index is not a constant expression, ensure that the array has a
  881. * declared size.
  882. */
  883. ir_constant *const const_index = op[1]->constant_expression_value();
  884. if (const_index != NULL) {
  885. const int idx = const_index->value.i[0];
  886. const char *type_name;
  887. unsigned bound = 0;
  888. if (array->type->is_matrix()) {
  889. type_name = "matrix";
  890. } else if (array->type->is_vector()) {
  891. type_name = "vector";
  892. } else {
  893. type_name = "array";
  894. }
  895. /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec:
  896. *
  897. * "It is illegal to declare an array with a size, and then
  898. * later (in the same shader) index the same array with an
  899. * integral constant expression greater than or equal to the
  900. * declared size. It is also illegal to index an array with a
  901. * negative constant expression."
  902. */
  903. if (array->type->is_matrix()) {
  904. if (array->type->row_type()->vector_elements <= idx) {
  905. bound = array->type->row_type()->vector_elements;
  906. }
  907. } else if (array->type->is_vector()) {
  908. if (array->type->vector_elements <= idx) {
  909. bound = array->type->vector_elements;
  910. }
  911. } else {
  912. if ((array->type->array_size() > 0)
  913. && (array->type->array_size() <= idx)) {
  914. bound = array->type->array_size();
  915. }
  916. }
  917. if (bound > 0) {
  918. _mesa_glsl_error(& loc, state, "%s index must be < %u",
  919. type_name, bound);
  920. error_emitted = true;
  921. } else if (idx < 0) {
  922. _mesa_glsl_error(& loc, state, "%s index must be >= 0",
  923. type_name);
  924. error_emitted = true;
  925. }
  926. if (array->type->is_array()) {
  927. ir_variable *const v = array->as_variable();
  928. if ((v != NULL) && (unsigned(idx) > v->max_array_access))
  929. v->max_array_access = idx;
  930. }
  931. }
  932. if (error_emitted)
  933. result->type = glsl_type::error_type;
  934. type = result->type;
  935. break;
  936. }
  937. case ast_function_call:
  938. /* Should *NEVER* get here. ast_function_call should always be handled
  939. * by ast_function_expression::hir.
  940. */
  941. assert(0);
  942. break;
  943. case ast_identifier: {
  944. /* ast_identifier can appear several places in a full abstract syntax
  945. * tree. This particular use must be at location specified in the grammar
  946. * as 'variable_identifier'.
  947. */
  948. ir_variable *var =
  949. state->symbols->get_variable(this->primary_expression.identifier);
  950. result = new ir_dereference(var);
  951. if (var != NULL) {
  952. type = result->type;
  953. } else {
  954. _mesa_glsl_error(& loc, state, "`%s' undeclared",
  955. this->primary_expression.identifier);
  956. error_emitted = true;
  957. }
  958. break;
  959. }
  960. case ast_int_constant:
  961. type = glsl_type::int_type;
  962. result = new ir_constant(type, & this->primary_expression);
  963. break;
  964. case ast_uint_constant:
  965. type = glsl_type::uint_type;
  966. result = new ir_constant(type, & this->primary_expression);
  967. break;
  968. case ast_float_constant:
  969. type = glsl_type::float_type;
  970. result = new ir_constant(type, & this->primary_expression);
  971. break;
  972. case ast_bool_constant:
  973. type = glsl_type::bool_type;
  974. result = new ir_constant(type, & this->primary_expression);
  975. break;
  976. case ast_sequence: {
  977. struct simple_node *ptr;
  978. /* It should not be possible to generate a sequence in the AST without
  979. * any expressions in it.
  980. */
  981. assert(!is_empty_list(&this->expressions));
  982. /* The r-value of a sequence is the last expression in the sequence. If
  983. * the other expressions in the sequence do not have side-effects (and
  984. * therefore add instructions to the instruction list), they get dropped
  985. * on the floor.
  986. */
  987. foreach (ptr, &this->expressions)
  988. result = ((ast_node *)ptr)->hir(instructions, state);
  989. type = result->type;
  990. /* Any errors should have already been emitted in the loop above.
  991. */
  992. error_emitted = true;
  993. break;
  994. }
  995. }
  996. if (type->is_error() && !error_emitted)
  997. _mesa_glsl_error(& loc, state, "type mismatch");
  998. return result;
  999. }
  1000. ir_rvalue *
  1001. ast_expression_statement::hir(exec_list *instructions,
  1002. struct _mesa_glsl_parse_state *state)
  1003. {
  1004. /* It is possible to have expression statements that don't have an
  1005. * expression. This is the solitary semicolon:
  1006. *
  1007. * for (i = 0; i < 5; i++)
  1008. * ;
  1009. *
  1010. * In this case the expression will be NULL. Test for NULL and don't do
  1011. * anything in that case.
  1012. */
  1013. if (expression != NULL)
  1014. expression->hir(instructions, state);
  1015. /* Statements do not have r-values.
  1016. */
  1017. return NULL;
  1018. }
  1019. ir_rvalue *
  1020. ast_compound_statement::hir(exec_list *instructions,
  1021. struct _mesa_glsl_parse_state *state)
  1022. {
  1023. struct simple_node *ptr;
  1024. if (new_scope)
  1025. state->symbols->push_scope();
  1026. foreach (ptr, &statements)
  1027. ((ast_node *)ptr)->hir(instructions, state);
  1028. if (new_scope)
  1029. state->symbols->pop_scope();
  1030. /* Compound statements do not have r-values.
  1031. */
  1032. return NULL;
  1033. }
  1034. static const glsl_type *
  1035. process_array_type(const glsl_type *base, ast_node *array_size,
  1036. struct _mesa_glsl_parse_state *state)
  1037. {
  1038. unsigned length = 0;
  1039. /* FINISHME: Reject delcarations of multidimensional arrays. */
  1040. if (array_size != NULL) {
  1041. exec_list dummy_instructions;
  1042. ir_rvalue *const ir = array_size->hir(& dummy_instructions, state);
  1043. YYLTYPE loc = array_size->get_location();
  1044. /* FINISHME: Verify that the grammar forbids side-effects in array
  1045. * FINISHME: sizes. i.e., 'vec4 [x = 12] data'
  1046. */
  1047. assert(dummy_instructions.is_empty());
  1048. if (ir != NULL) {
  1049. if (!ir->type->is_integer()) {
  1050. _mesa_glsl_error(& loc, state, "array size must be integer type");
  1051. } else if (!ir->type->is_scalar()) {
  1052. _mesa_glsl_error(& loc, state, "array size must be scalar type");
  1053. } else {
  1054. ir_constant *const size = ir->constant_expression_value();
  1055. if (size == NULL) {
  1056. _mesa_glsl_error(& loc, state, "array size must be a "
  1057. "constant valued expression");
  1058. } else if (size->value.i[0] <= 0) {
  1059. _mesa_glsl_error(& loc, state, "array size must be > 0");
  1060. } else {
  1061. assert(size->type == ir->type);
  1062. length = size->value.u[0];
  1063. }
  1064. }
  1065. }
  1066. }
  1067. return glsl_type::get_array_instance(base, length);
  1068. }
  1069. const glsl_type *
  1070. ast_type_specifier::glsl_type(const char **name,
  1071. struct _mesa_glsl_parse_state *state) const
  1072. {
  1073. const struct glsl_type *type;
  1074. if (this->type_specifier == ast_struct) {
  1075. /* FINISHME: Handle annonymous structures. */
  1076. type = NULL;
  1077. } else {
  1078. type = state->symbols->get_type(this->type_name);
  1079. *name = this->type_name;
  1080. if (this->is_array) {
  1081. type = process_array_type(type, this->array_size, state);
  1082. }
  1083. }
  1084. return type;
  1085. }
  1086. static void
  1087. apply_type_qualifier_to_variable(const struct ast_type_qualifier *qual,
  1088. struct ir_variable *var,
  1089. struct _mesa_glsl_parse_state *state,
  1090. YYLTYPE *loc)
  1091. {
  1092. if (qual->invariant)
  1093. var->invariant = 1;
  1094. /* FINISHME: Mark 'in' variables at global scope as read-only. */
  1095. if (qual->constant || qual->attribute || qual->uniform
  1096. || (qual->varying && (state->target == fragment_shader)))
  1097. var->read_only = 1;
  1098. if (qual->centroid)
  1099. var->centroid = 1;
  1100. if (qual->attribute && state->target != vertex_shader) {
  1101. var->type = glsl_type::error_type;
  1102. _mesa_glsl_error(loc, state,
  1103. "`attribute' variables may not be declared in the "
  1104. "%s shader",
  1105. _mesa_glsl_shader_target_name(state->target));
  1106. }
  1107. /* From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
  1108. *
  1109. * "The varying qualifier can be used only with the data types
  1110. * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
  1111. * these."
  1112. */
  1113. if (qual->varying && var->type->base_type != GLSL_TYPE_FLOAT) {
  1114. var->type = glsl_type::error_type;
  1115. _mesa_glsl_error(loc, state,
  1116. "varying variables must be of base type float");
  1117. }
  1118. if (qual->in && qual->out)
  1119. var->mode = ir_var_inout;
  1120. else if (qual->attribute || qual->in
  1121. || (qual->varying && (state->target == fragment_shader)))
  1122. var->mode = ir_var_in;
  1123. else if (qual->out || (qual->varying && (state->target == vertex_shader)))
  1124. var->mode = ir_var_out;
  1125. else if (qual->uniform)
  1126. var->mode = ir_var_uniform;
  1127. else
  1128. var->mode = ir_var_auto;
  1129. if (qual->flat)
  1130. var->interpolation = ir_var_flat;
  1131. else if (qual->noperspective)
  1132. var->interpolation = ir_var_noperspective;
  1133. else
  1134. var->interpolation = ir_var_smooth;
  1135. if (var->type->is_array() && (state->language_version >= 120)) {
  1136. var->array_lvalue = true;
  1137. }
  1138. }
  1139. ir_rvalue *
  1140. ast_declarator_list::hir(exec_list *instructions,
  1141. struct _mesa_glsl_parse_state *state)
  1142. {
  1143. struct simple_node *ptr;
  1144. const struct glsl_type *decl_type;
  1145. const char *type_name = NULL;
  1146. /* FINISHME: Handle vertex shader "invariant" declarations that do not
  1147. * FINISHME: include a type. These re-declare built-in variables to be
  1148. * FINISHME: invariant.
  1149. */
  1150. decl_type = this->type->specifier->glsl_type(& type_name, state);
  1151. foreach (ptr, &this->declarations) {
  1152. struct ast_declaration *const decl = (struct ast_declaration * )ptr;
  1153. const struct glsl_type *var_type;
  1154. struct ir_variable *var;
  1155. YYLTYPE loc = this->get_location();
  1156. /* FINISHME: Emit a warning if a variable declaration shadows a
  1157. * FINISHME: declaration at a higher scope.
  1158. */
  1159. if ((decl_type == NULL) || decl_type->is_void()) {
  1160. if (type_name != NULL) {
  1161. _mesa_glsl_error(& loc, state,
  1162. "invalid type `%s' in declaration of `%s'",
  1163. type_name, decl->identifier);
  1164. } else {
  1165. _mesa_glsl_error(& loc, state,
  1166. "invalid type in declaration of `%s'",
  1167. decl->identifier);
  1168. }
  1169. continue;
  1170. }
  1171. if (decl->is_array) {
  1172. var_type = process_array_type(decl_type, decl->array_size, state);
  1173. } else {
  1174. var_type = decl_type;
  1175. }
  1176. var = new ir_variable(var_type, decl->identifier);
  1177. /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
  1178. *
  1179. * "Global variables can only use the qualifiers const,
  1180. * attribute, uni form, or varying. Only one may be
  1181. * specified.
  1182. *
  1183. * Local variables can only use the qualifier const."
  1184. *
  1185. * This is relaxed in GLSL 1.30.
  1186. */
  1187. if (state->language_version < 120) {
  1188. if (this->type->qualifier.out) {
  1189. _mesa_glsl_error(& loc, state,
  1190. "`out' qualifier in declaration of `%s' "
  1191. "only valid for function parameters in GLSL 1.10.",
  1192. decl->identifier);
  1193. }
  1194. if (this->type->qualifier.in) {
  1195. _mesa_glsl_error(& loc, state,
  1196. "`in' qualifier in declaration of `%s' "
  1197. "only valid for function parameters in GLSL 1.10.",
  1198. decl->identifier);
  1199. }
  1200. /* FINISHME: Test for other invalid qualifiers. */
  1201. }
  1202. apply_type_qualifier_to_variable(& this->type->qualifier, var, state,
  1203. & loc);
  1204. /* Attempt to add the variable to the symbol table. If this fails, it
  1205. * means the variable has already been declared at this scope. Arrays
  1206. * fudge this rule a little bit.
  1207. *
  1208. * From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
  1209. *
  1210. * "It is legal to declare an array without a size and then
  1211. * later re-declare the same name as an array of the same
  1212. * type and specify a size."
  1213. */
  1214. if (state->symbols->name_declared_this_scope(decl->identifier)) {
  1215. ir_variable *const earlier =
  1216. state->symbols->get_variable(decl->identifier);
  1217. if ((earlier != NULL)
  1218. && (earlier->type->array_size() == 0)
  1219. && var->type->is_array()
  1220. && (var->type->element_type() == earlier->type->element_type())) {
  1221. /* FINISHME: This doesn't match the qualifiers on the two
  1222. * FINISHME: declarations. It's not 100% clear whether this is
  1223. * FINISHME: required or not.
  1224. */
  1225. if (var->type->array_size() <= (int)earlier->max_array_access) {
  1226. YYLTYPE loc = this->get_location();
  1227. _mesa_glsl_error(& loc, state, "array size must be > %u due to "
  1228. "previous access",
  1229. earlier->max_array_access);
  1230. }
  1231. earlier->type = var->type;
  1232. delete var;
  1233. var = NULL;
  1234. } else {
  1235. YYLTYPE loc = this->get_location();
  1236. _mesa_glsl_error(& loc, state, "`%s' redeclared",
  1237. decl->identifier);
  1238. }
  1239. continue;
  1240. }
  1241. /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
  1242. *
  1243. * "Identifiers starting with "gl_" are reserved for use by
  1244. * OpenGL, and may not be declared in a shader as either a
  1245. * variable or a function."
  1246. */
  1247. if (strncmp(decl->identifier, "gl_", 3) == 0) {
  1248. /* FINISHME: This should only trigger if we're not redefining
  1249. * FINISHME: a builtin (to add a qualifier, for example).
  1250. */
  1251. _mesa_glsl_error(& loc, state,
  1252. "identifier `%s' uses reserved `gl_' prefix",
  1253. decl->identifier);
  1254. }
  1255. instructions->push_tail(var);
  1256. if (state->current_function != NULL) {
  1257. const char *mode = NULL;
  1258. const char *extra = "";
  1259. /* There is no need to check for 'inout' here because the parser will
  1260. * only allow that in function parameter lists.
  1261. */
  1262. if (this->type->qualifier.attribute) {
  1263. mode = "attribute";
  1264. } else if (this->type->qualifier.uniform) {
  1265. mode = "uniform";
  1266. } else if (this->type->qualifier.varying) {
  1267. mode = "varying";
  1268. } else if (this->type->qualifier.in) {
  1269. mode = "in";
  1270. extra = " or in function parameter list";
  1271. } else if (this->type->qualifier.out) {
  1272. mode = "out";
  1273. extra = " or in function parameter list";
  1274. }
  1275. if (mode) {
  1276. _mesa_glsl_error(& loc, state,
  1277. "%s variable `%s' must be declared at "
  1278. "global scope%s",
  1279. mode, var->name, extra);
  1280. }
  1281. } else if (var->mode == ir_var_in) {
  1282. if (state->target == vertex_shader) {
  1283. bool error_emitted = false;
  1284. /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
  1285. *
  1286. * "Vertex shader inputs can only be float, floating-point
  1287. * vectors, matrices, signed and unsigned integers and integer
  1288. * vectors. Vertex shader inputs can also form arrays of these
  1289. * types, but not structures."
  1290. *
  1291. * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
  1292. *
  1293. * "Vertex shader inputs can only be float, floating-point
  1294. * vectors, matrices, signed and unsigned integers and integer
  1295. * vectors. They cannot be arrays or structures."
  1296. *
  1297. * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
  1298. *
  1299. * "The attribute qualifier can be used only with float,
  1300. * floating-point vectors, and matrices. Attribute variables
  1301. * cannot be declared as arrays or structures."
  1302. */
  1303. const glsl_type *check_type = var->type->is_array()
  1304. ? var->type->fields.array : var->type;
  1305. switch (check_type->base_type) {
  1306. case GLSL_TYPE_FLOAT:
  1307. break;
  1308. case GLSL_TYPE_UINT:
  1309. case GLSL_TYPE_INT:
  1310. if (state->language_version > 120)
  1311. break;
  1312. /* FALLTHROUGH */
  1313. default:
  1314. _mesa_glsl_error(& loc, state,
  1315. "vertex shader input / attribute cannot have "
  1316. "type %s`%s'",
  1317. var->type->is_array() ? "array of " : "",
  1318. check_type->name);
  1319. error_emitted = true;
  1320. }
  1321. if (!error_emitted && (state->language_version <= 130)
  1322. && var->type->is_array()) {
  1323. _mesa_glsl_error(& loc, state,
  1324. "vertex shader input / attribute cannot have "
  1325. "array type");
  1326. error_emitted = true;
  1327. }
  1328. }
  1329. }
  1330. if (decl->initializer != NULL) {
  1331. YYLTYPE initializer_loc = decl->initializer->get_location();
  1332. /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
  1333. *
  1334. * "All uniform variables are read-only and are initialized either
  1335. * directly by an application via API commands, or indirectly by
  1336. * OpenGL."
  1337. */
  1338. if ((state->language_version <= 110)
  1339. && (var->mode == ir_var_uniform)) {
  1340. _mesa_glsl_error(& initializer_loc, state,
  1341. "cannot initialize uniforms in GLSL 1.10");
  1342. }
  1343. if (var->type->is_sampler()) {
  1344. _mesa_glsl_error(& initializer_loc, state,
  1345. "cannot initialize samplers");
  1346. }
  1347. if ((var->mode == ir_var_in) && (state->current_function == NULL)) {
  1348. _mesa_glsl_error(& initializer_loc, state,
  1349. "cannot initialize %s shader input / %s",
  1350. _mesa_glsl_shader_target_name(state->target),
  1351. (state->target == vertex_shader)
  1352. ? "attribute" : "varying");
  1353. }
  1354. ir_dereference *const lhs = new ir_dereference(var);
  1355. ir_rvalue *rhs = decl->initializer->hir(instructions, state);
  1356. /* Calculate the constant value if this is a const
  1357. * declaration.
  1358. */
  1359. if (this->type->qualifier.constant) {
  1360. ir_constant *constant_value = rhs->constant_expression_value();
  1361. if (!constant_value) {
  1362. _mesa_glsl_error(& initializer_loc, state,
  1363. "initializer of const variable `%s' must be a "
  1364. "constant expression",
  1365. decl->identifier);
  1366. } else {
  1367. rhs = constant_value;
  1368. var->constant_value = constant_value;
  1369. }
  1370. }
  1371. if (rhs && !rhs->type->is_error()) {
  1372. bool temp = var->read_only;
  1373. if (this->type->qualifier.constant)
  1374. var->read_only = false;
  1375. (void) do_assignment(instructions, state, lhs, rhs,
  1376. this->get_location());
  1377. var->read_only = temp;
  1378. }
  1379. }
  1380. /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
  1381. *
  1382. * "It is an error to write to a const variable outside of
  1383. * its declaration, so they must be initialized when
  1384. * declared."
  1385. */
  1386. if (this->type->qualifier.constant && decl->initializer == NULL) {
  1387. _mesa_glsl_error(& loc, state,
  1388. "const declaration of `%s' must be initialized");
  1389. }
  1390. /* Add the vairable to the symbol table after processing the initializer.
  1391. * This differs from most C-like languages, but it follows the GLSL
  1392. * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50
  1393. * spec:
  1394. *
  1395. * "Within a declaration, the scope of a name starts immediately
  1396. * after the initializer if present or immediately after the name
  1397. * being declared if not."
  1398. */
  1399. const bool added_variable =
  1400. state->symbols->add_variable(decl->identifier, var);
  1401. assert(added_variable);
  1402. }
  1403. /* Variable declarations do not have r-values.
  1404. */
  1405. return NULL;
  1406. }
  1407. ir_rvalue *
  1408. ast_parameter_declarator::hir(exec_list *instructions,
  1409. struct _mesa_glsl_parse_state *state)
  1410. {
  1411. const struct glsl_type *type;
  1412. const char *name = NULL;
  1413. YYLTYPE loc = this->get_location();
  1414. type = this->type->specifier->glsl_type(& name, state);
  1415. if (type == NULL) {
  1416. if (name != NULL) {
  1417. _mesa_glsl_error(& loc, state,
  1418. "invalid type `%s' in declaration of `%s'",
  1419. name, this->identifier);
  1420. } else {
  1421. _mesa_glsl_error(& loc, state,
  1422. "invalid type in declaration of `%s'",
  1423. this->identifier);
  1424. }
  1425. type = glsl_type::error_type;
  1426. }
  1427. /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
  1428. *
  1429. * "Functions that accept no input arguments need not use void in the
  1430. * argument list because prototypes (or definitions) are required and
  1431. * therefore there is no ambiguity when an empty argument list "( )" is
  1432. * declared. The idiom "(void)" as a parameter list is provided for
  1433. * convenience."
  1434. *
  1435. * Placing this check here prevents a void parameter being set up
  1436. * for a function, which avoids tripping up checks for main taking
  1437. * parameters and lookups of an unnamed symbol.
  1438. */
  1439. if (type->is_void()) {
  1440. if (this->identifier != NULL)
  1441. _mesa_glsl_error(& loc, state,
  1442. "named parameter cannot have type `void'");
  1443. is_void = true;
  1444. return NULL;
  1445. }
  1446. if (formal_parameter && (this->identifier == NULL)) {
  1447. _mesa_glsl_error(& loc, state, "formal parameter lacks a name");
  1448. return NULL;
  1449. }
  1450. is_void = false;
  1451. ir_variable *var = new ir_variable(type, this->identifier);
  1452. /* FINISHME: Handle array declarations. Note that this requires
  1453. * FINISHME: complete handling of constant expressions.
  1454. */
  1455. /* Apply any specified qualifiers to the parameter declaration. Note that
  1456. * for function parameters the default mode is 'in'.
  1457. */
  1458. apply_type_qualifier_to_variable(& this->type->qualifier, var, state, & loc);
  1459. if (var->mode == ir_var_auto)
  1460. var->mode = ir_var_in;
  1461. instructions->push_tail(var);
  1462. /* Parameter declarations do not have r-values.
  1463. */
  1464. return NULL;
  1465. }
  1466. void
  1467. ast_parameter_declarator::parameters_to_hir(struct simple_node *ast_parameters,
  1468. bool formal,
  1469. exec_list *ir_parameters,
  1470. _mesa_glsl_parse_state *state)
  1471. {
  1472. struct simple_node *ptr;
  1473. ast_parameter_declarator *void_param = NULL;
  1474. unsigned count = 0;
  1475. foreach (ptr, ast_parameters) {
  1476. ast_parameter_declarator *param = (ast_parameter_declarator *)ptr;
  1477. param->formal_parameter = formal;
  1478. param->hir(ir_parameters, state);
  1479. if (param->is_void)
  1480. void_param = param;
  1481. count++;
  1482. }
  1483. if ((void_param != NULL) && (count > 1)) {
  1484. YYLTYPE loc = void_param->get_location();
  1485. _mesa_glsl_error(& loc, state,
  1486. "`void' parameter must be only parameter");
  1487. }
  1488. }
  1489. static bool
  1490. parameter_lists_match(exec_list *list_a, exec_list *list_b)
  1491. {
  1492. exec_list_iterator iter_a = list_a->iterator();
  1493. exec_list_iterator iter_b = list_b->iterator();
  1494. while (iter_a.has_next()) {
  1495. ir_variable *a = (ir_variable *)iter_a.get();
  1496. ir_variable *b = (ir_variable *)iter_b.get();
  1497. /* If all of the parameters from the other parameter list have been
  1498. * exhausted, the lists have different length and, by definition,
  1499. * do not match.
  1500. */
  1501. if (!iter_b.has_next())
  1502. return false;
  1503. /* If the types of the parameters do not match, the parameters lists
  1504. * are different.
  1505. */
  1506. if (a->type != b->type)
  1507. return false;
  1508. iter_a.next();
  1509. iter_b.next();
  1510. }
  1511. return true;
  1512. }
  1513. ir_rvalue *
  1514. ast_function::hir(exec_list *instructions,
  1515. struct _mesa_glsl_parse_state *state)
  1516. {
  1517. ir_function *f = NULL;
  1518. ir_function_signature *sig = NULL;
  1519. exec_list hir_parameters;
  1520. /* The prototype part of a function does not generate anything in the IR
  1521. * instruction stream.
  1522. */
  1523. (void) instructions;
  1524. /* Convert the list of function parameters to HIR now so that they can be
  1525. * used below to compare this function's signature with previously seen
  1526. * signatures for functions with the same name.
  1527. */
  1528. ast_parameter_declarator::parameters_to_hir(& this->parameters,
  1529. is_definition,
  1530. & hir_parameters, state);
  1531. const char *return_type_name;
  1532. const glsl_type *return_type =
  1533. this->return_type->specifier->glsl_type(& return_type_name, state);
  1534. assert(return_type != NULL);
  1535. /* Verify that this function's signature either doesn't match a previously
  1536. * seen signature for a function with the same name, or, if a match is found,
  1537. * that the previously seen signature does not have an associated definition.
  1538. */
  1539. const char *const name = identifier;
  1540. f = state->symbols->get_function(name);
  1541. if (f != NULL) {
  1542. foreach_iter(exec_list_iterator, iter, *f) {
  1543. sig = (struct ir_function_signature *) iter.get();
  1544. /* Compare the parameter list of the function being defined to the
  1545. * existing function. If the parameter lists match, then the return
  1546. * type must also match and the existing function must not have a
  1547. * definition.
  1548. */
  1549. if (parameter_lists_match(& hir_parameters, & sig->parameters)) {
  1550. /* FINISHME: Compare return types. */
  1551. if (is_definition && (sig->definition != NULL)) {
  1552. YYLTYPE loc = this->get_location();
  1553. _mesa_glsl_error(& loc, state, "function `%s' redefined", name);
  1554. sig = NULL;
  1555. break;
  1556. }
  1557. }
  1558. sig = NULL;
  1559. }
  1560. } else if (state->symbols->name_declared_this_scope(name)) {
  1561. /* This function name shadows a non-function use of the same name.
  1562. */
  1563. YYLTYPE loc = this->get_location();
  1564. _mesa_glsl_error(& loc, state, "function name `%s' conflicts with "
  1565. "non-function", name);
  1566. sig = NULL;
  1567. } else {
  1568. f = new ir_function(name);
  1569. state->symbols->add_function(f->name, f);
  1570. }
  1571. /* Verify the return type of main() */
  1572. if (strcmp(name, "main") == 0) {
  1573. if (! return_type->is_void()) {
  1574. YYLTYPE loc = this->get_location();
  1575. _mesa_glsl_error(& loc, state, "main() must return void");
  1576. }
  1577. if (!hir_parameters.is_empty()) {
  1578. YYLTYPE loc = this->get_location();
  1579. _mesa_glsl_error(& loc, state, "main() must not take any parameters");
  1580. }
  1581. }
  1582. /* Finish storing the information about this new function in its signature.
  1583. */
  1584. if (sig == NULL) {
  1585. sig = new ir_function_signature(return_type);
  1586. f->add_signature(sig);
  1587. } else if (is_definition) {
  1588. /* Destroy all of the previous parameter information. The previous
  1589. * parameter information comes from the function prototype, and it can
  1590. * either include invalid parameter names or may not have names at all.
  1591. */
  1592. foreach_iter(exec_list_iterator, iter, sig->parameters) {
  1593. assert(((ir_instruction *) iter.get())->as_variable() != NULL);
  1594. iter.remove();
  1595. delete iter.get();
  1596. }
  1597. }
  1598. hir_parameters.move_nodes_to(& sig->parameters);
  1599. signature = sig;
  1600. /* Function declarations (prototypes) do not have r-values.
  1601. */
  1602. return NULL;
  1603. }
  1604. ir_rvalue *
  1605. ast_function_definition::hir(exec_list *instructions,
  1606. struct _mesa_glsl_parse_state *state)
  1607. {
  1608. prototype->is_definition = true;
  1609. prototype->hir(instructions, state);
  1610. ir_function_signature *signature = prototype->signature;
  1611. assert(state->current_function == NULL);
  1612. state->current_function = signature;
  1613. ir_label *label = new ir_label(signature->function_name(), signature);
  1614. if (signature->definition == NULL) {
  1615. signature->definition = label;
  1616. }
  1617. instructions->push_tail(label);
  1618. /* Duplicate parameters declared in the prototype as concrete variables.
  1619. * Add these to the symbol table.
  1620. */
  1621. state->symbols->push_scope();
  1622. foreach_iter(exec_list_iterator, iter, signature->parameters) {
  1623. ir_variable *const var = ((ir_instruction *) iter.get())->as_variable();
  1624. assert(var != NULL);
  1625. /* The only way a parameter would "exist" is if two parameters have
  1626. * the same name.
  1627. */
  1628. if (state->symbols->name_declared_this_scope(var->name)) {
  1629. YYLTYPE loc = this->get_location();
  1630. _mesa_glsl_error(& loc, state, "parameter `%s' redeclared", var->name);
  1631. } else {
  1632. state->symbols->add_variable(var->name, var);
  1633. }
  1634. }
  1635. /* Convert the body of the function to HIR, and append the resulting
  1636. * instructions to the list that currently consists of the function label
  1637. * and the function parameters.
  1638. */
  1639. this->body->hir(&signature->body, state);
  1640. state->symbols->pop_scope();
  1641. assert(state->current_function == signature);
  1642. state->current_function = NULL;
  1643. /* Function definitions do not have r-values.
  1644. */
  1645. return NULL;
  1646. }
  1647. ir_rvalue *
  1648. ast_jump_statement::hir(exec_list *instructions,
  1649. struct _mesa_glsl_parse_state *state)
  1650. {
  1651. switch (mode) {
  1652. case ast_return: {
  1653. ir_return *inst;
  1654. assert(state->current_function);
  1655. if (opt_return_value) {
  1656. if (state->current_function->return_type->base_type ==
  1657. GLSL_TYPE_VOID) {
  1658. YYLTYPE loc = this->get_location();
  1659. _mesa_glsl_error(& loc, state,
  1660. "`return` with a value, in function `%s' "
  1661. "returning void",
  1662. state->current_function->definition->label);
  1663. }
  1664. ir_expression *const ret = (ir_expression *)
  1665. opt_return_value->hir(instructions, state);
  1666. assert(ret != NULL);
  1667. /* FINISHME: Make sure the type of the return value matches the return
  1668. * FINISHME: type of the enclosing function.
  1669. */
  1670. inst = new ir_return(ret);
  1671. } else {
  1672. if (state->current_function->return_type->base_type !=
  1673. GLSL_TYPE_VOID) {
  1674. YYLTYPE loc = this->get_location();
  1675. _mesa_glsl_error(& loc, state,
  1676. "`return' with no value, in function %s returning "
  1677. "non-void",
  1678. state->current_function->definition->label);
  1679. }
  1680. inst = new ir_return;
  1681. }
  1682. instructions->push_tail(inst);
  1683. break;
  1684. }
  1685. case ast_discard:
  1686. /* FINISHME: discard support */
  1687. if (state->target != fragment_shader) {
  1688. YYLTYPE loc = this->get_location();
  1689. _mesa_glsl_error(& loc, state,
  1690. "`discard' may only appear in a fragment shader");
  1691. }
  1692. break;
  1693. case ast_break:
  1694. case ast_continue:
  1695. /* FINISHME: Handle switch-statements. They cannot contain 'continue',
  1696. * FINISHME: and they use a different IR instruction for 'break'.
  1697. */
  1698. /* FINISHME: Correctly handle the nesting. If a switch-statement is
  1699. * FINISHME: inside a loop, a 'continue' is valid and will bind to the
  1700. * FINISHME: loop.
  1701. */
  1702. if (state->loop_or_switch_nesting == NULL) {
  1703. YYLTYPE loc = this->get_location();
  1704. _mesa_glsl_error(& loc, state,
  1705. "`%s' may only appear in a loop",
  1706. (mode == ast_break) ? "break" : "continue");
  1707. } else {
  1708. ir_loop *const loop = state->loop_or_switch_nesting->as_loop();
  1709. if (loop != NULL) {
  1710. ir_loop_jump *const jump =
  1711. new ir_loop_jump(loop,
  1712. (mode == ast_break)
  1713. ? ir_loop_jump::jump_break
  1714. : ir_loop_jump::jump_continue);
  1715. instructions->push_tail(jump);
  1716. }
  1717. }
  1718. break;
  1719. }
  1720. /* Jump instructions do not have r-values.
  1721. */
  1722. return NULL;
  1723. }
  1724. ir_rvalue *
  1725. ast_selection_statement::hir(exec_list *instructions,
  1726. struct _mesa_glsl_parse_state *state)
  1727. {
  1728. ir_rvalue *const condition = this->condition->hir(instructions, state);
  1729. /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
  1730. *
  1731. * "Any expression whose type evaluates to a Boolean can be used as the
  1732. * conditional expression bool-expression. Vector types are not accepted
  1733. * as the expression to if."
  1734. *
  1735. * The checks are separated so that higher quality diagnostics can be
  1736. * generated for cases where both rules are violated.
  1737. */
  1738. if (!condition->type->is_boolean() || !condition->type->is_scalar()) {
  1739. YYLTYPE loc = this->condition->get_location();
  1740. _mesa_glsl_error(& loc, state, "if-statement condition must be scalar "
  1741. "boolean");
  1742. }
  1743. ir_if *const stmt = new ir_if(condition);
  1744. if (then_statement != NULL) {
  1745. ast_node *node = (ast_node *) then_statement;
  1746. do {
  1747. node->hir(& stmt->then_instructions, state);
  1748. node = (ast_node *) node->next;
  1749. } while (node != then_statement);
  1750. }
  1751. if (else_statement != NULL) {
  1752. ast_node *node = (ast_node *) else_statement;
  1753. do {
  1754. node->hir(& stmt->else_instructions, state);
  1755. node = (ast_node *) node->next;
  1756. } while (node != else_statement);
  1757. }
  1758. instructions->push_tail(stmt);
  1759. /* if-statements do not have r-values.
  1760. */
  1761. return NULL;
  1762. }
  1763. void
  1764. ast_iteration_statement::condition_to_hir(ir_loop *stmt,
  1765. struct _mesa_glsl_parse_state *state)
  1766. {
  1767. if (condition != NULL) {
  1768. ir_rvalue *const cond =
  1769. condition->hir(& stmt->body_instructions, state);
  1770. if ((cond == NULL)
  1771. || !cond->type->is_boolean() || !cond->type->is_scalar()) {
  1772. YYLTYPE loc = condition->get_location();
  1773. _mesa_glsl_error(& loc, state,
  1774. "loop condition must be scalar boolean");
  1775. } else {
  1776. /* As the first code in the loop body, generate a block that looks
  1777. * like 'if (!condition) break;' as the loop termination condition.
  1778. */
  1779. ir_rvalue *const not_cond =
  1780. new ir_expression(ir_unop_logic_not, glsl_type::bool_type, cond,
  1781. NULL);
  1782. ir_if *const if_stmt = new ir_if(not_cond);
  1783. ir_jump *const break_stmt =
  1784. new ir_loop_jump(stmt, ir_loop_jump::jump_break);
  1785. if_stmt->then_instructions.push_tail(break_stmt);
  1786. stmt->body_instructions.push_tail(if_stmt);
  1787. }
  1788. }
  1789. }
  1790. ir_rvalue *
  1791. ast_iteration_statement::hir(exec_list *instructions,
  1792. struct _mesa_glsl_parse_state *state)
  1793. {
  1794. /* For loops start a new scope, but while and do-while loops do not.
  1795. */
  1796. if (mode == ast_for)
  1797. state->symbols->push_scope();
  1798. if (init_statement != NULL)
  1799. init_statement->hir(instructions, state);
  1800. ir_loop *const stmt = new ir_loop();
  1801. instructions->push_tail(stmt);
  1802. /* Track the current loop and / or switch-statement nesting.
  1803. */
  1804. ir_instruction *const nesting = state->loop_or_switch_nesting;
  1805. state->loop_or_switch_nesting = stmt;
  1806. if (mode != ast_do_while)
  1807. condition_to_hir(stmt, state);
  1808. if (body != NULL) {
  1809. ast_node *node = (ast_node *) body;
  1810. do {
  1811. node->hir(& stmt->body_instructions, state);
  1812. node = (ast_node *) node->next;
  1813. } while (node != body);
  1814. }
  1815. if (rest_expression != NULL)
  1816. rest_expression->hir(& stmt->body_instructions, state);
  1817. if (mode == ast_do_while)
  1818. condition_to_hir(stmt, state);
  1819. if (mode == ast_for)
  1820. state->symbols->pop_scope();
  1821. /* Restore previous nesting before returning.
  1822. */
  1823. state->loop_or_switch_nesting = nesting;
  1824. /* Loops do not have r-values.
  1825. */
  1826. return NULL;
  1827. }