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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243
  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 "main/core.h" /* for struct gl_extensions */
  51. #include "glsl_symbol_table.h"
  52. #include "glsl_parser_extras.h"
  53. #include "ast.h"
  54. #include "glsl_types.h"
  55. #include "program/hash_table.h"
  56. #include "ir.h"
  57. static void
  58. detect_conflicting_assignments(struct _mesa_glsl_parse_state *state,
  59. exec_list *instructions);
  60. void
  61. _mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state)
  62. {
  63. _mesa_glsl_initialize_variables(instructions, state);
  64. state->symbols->separate_function_namespace = state->language_version == 110;
  65. state->current_function = NULL;
  66. state->toplevel_ir = instructions;
  67. /* Section 4.2 of the GLSL 1.20 specification states:
  68. * "The built-in functions are scoped in a scope outside the global scope
  69. * users declare global variables in. That is, a shader's global scope,
  70. * available for user-defined functions and global variables, is nested
  71. * inside the scope containing the built-in functions."
  72. *
  73. * Since built-in functions like ftransform() access built-in variables,
  74. * it follows that those must be in the outer scope as well.
  75. *
  76. * We push scope here to create this nesting effect...but don't pop.
  77. * This way, a shader's globals are still in the symbol table for use
  78. * by the linker.
  79. */
  80. state->symbols->push_scope();
  81. foreach_list_typed (ast_node, ast, link, & state->translation_unit)
  82. ast->hir(instructions, state);
  83. detect_recursion_unlinked(state, instructions);
  84. detect_conflicting_assignments(state, instructions);
  85. state->toplevel_ir = NULL;
  86. }
  87. /**
  88. * If a conversion is available, convert one operand to a different type
  89. *
  90. * The \c from \c ir_rvalue is converted "in place".
  91. *
  92. * \param to Type that the operand it to be converted to
  93. * \param from Operand that is being converted
  94. * \param state GLSL compiler state
  95. *
  96. * \return
  97. * If a conversion is possible (or unnecessary), \c true is returned.
  98. * Otherwise \c false is returned.
  99. */
  100. bool
  101. apply_implicit_conversion(const glsl_type *to, ir_rvalue * &from,
  102. struct _mesa_glsl_parse_state *state)
  103. {
  104. void *ctx = state;
  105. if (to->base_type == from->type->base_type)
  106. return true;
  107. /* This conversion was added in GLSL 1.20. If the compilation mode is
  108. * GLSL 1.10, the conversion is skipped.
  109. */
  110. if (!state->is_version(120, 0))
  111. return false;
  112. /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec:
  113. *
  114. * "There are no implicit array or structure conversions. For
  115. * example, an array of int cannot be implicitly converted to an
  116. * array of float. There are no implicit conversions between
  117. * signed and unsigned integers."
  118. */
  119. /* FINISHME: The above comment is partially a lie. There is int/uint
  120. * FINISHME: conversion for immediate constants.
  121. */
  122. if (!to->is_float() || !from->type->is_numeric())
  123. return false;
  124. /* Convert to a floating point type with the same number of components
  125. * as the original type - i.e. int to float, not int to vec4.
  126. */
  127. to = glsl_type::get_instance(GLSL_TYPE_FLOAT, from->type->vector_elements,
  128. from->type->matrix_columns);
  129. switch (from->type->base_type) {
  130. case GLSL_TYPE_INT:
  131. from = new(ctx) ir_expression(ir_unop_i2f, to, from, NULL);
  132. break;
  133. case GLSL_TYPE_UINT:
  134. from = new(ctx) ir_expression(ir_unop_u2f, to, from, NULL);
  135. break;
  136. case GLSL_TYPE_BOOL:
  137. from = new(ctx) ir_expression(ir_unop_b2f, to, from, NULL);
  138. break;
  139. default:
  140. assert(0);
  141. }
  142. return true;
  143. }
  144. static const struct glsl_type *
  145. arithmetic_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
  146. bool multiply,
  147. struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
  148. {
  149. const glsl_type *type_a = value_a->type;
  150. const glsl_type *type_b = value_b->type;
  151. /* From GLSL 1.50 spec, page 56:
  152. *
  153. * "The arithmetic binary operators add (+), subtract (-),
  154. * multiply (*), and divide (/) operate on integer and
  155. * floating-point scalars, vectors, and matrices."
  156. */
  157. if (!type_a->is_numeric() || !type_b->is_numeric()) {
  158. _mesa_glsl_error(loc, state,
  159. "Operands to arithmetic operators must be numeric");
  160. return glsl_type::error_type;
  161. }
  162. /* "If one operand is floating-point based and the other is
  163. * not, then the conversions from Section 4.1.10 "Implicit
  164. * Conversions" are applied to the non-floating-point-based operand."
  165. */
  166. if (!apply_implicit_conversion(type_a, value_b, state)
  167. && !apply_implicit_conversion(type_b, value_a, state)) {
  168. _mesa_glsl_error(loc, state,
  169. "Could not implicitly convert operands to "
  170. "arithmetic operator");
  171. return glsl_type::error_type;
  172. }
  173. type_a = value_a->type;
  174. type_b = value_b->type;
  175. /* "If the operands are integer types, they must both be signed or
  176. * both be unsigned."
  177. *
  178. * From this rule and the preceeding conversion it can be inferred that
  179. * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
  180. * The is_numeric check above already filtered out the case where either
  181. * type is not one of these, so now the base types need only be tested for
  182. * equality.
  183. */
  184. if (type_a->base_type != type_b->base_type) {
  185. _mesa_glsl_error(loc, state,
  186. "base type mismatch for arithmetic operator");
  187. return glsl_type::error_type;
  188. }
  189. /* "All arithmetic binary operators result in the same fundamental type
  190. * (signed integer, unsigned integer, or floating-point) as the
  191. * operands they operate on, after operand type conversion. After
  192. * conversion, the following cases are valid
  193. *
  194. * * The two operands are scalars. In this case the operation is
  195. * applied, resulting in a scalar."
  196. */
  197. if (type_a->is_scalar() && type_b->is_scalar())
  198. return type_a;
  199. /* "* One operand is a scalar, and the other is a vector or matrix.
  200. * In this case, the scalar operation is applied independently to each
  201. * component of the vector or matrix, resulting in the same size
  202. * vector or matrix."
  203. */
  204. if (type_a->is_scalar()) {
  205. if (!type_b->is_scalar())
  206. return type_b;
  207. } else if (type_b->is_scalar()) {
  208. return type_a;
  209. }
  210. /* All of the combinations of <scalar, scalar>, <vector, scalar>,
  211. * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
  212. * handled.
  213. */
  214. assert(!type_a->is_scalar());
  215. assert(!type_b->is_scalar());
  216. /* "* The two operands are vectors of the same size. In this case, the
  217. * operation is done component-wise resulting in the same size
  218. * vector."
  219. */
  220. if (type_a->is_vector() && type_b->is_vector()) {
  221. if (type_a == type_b) {
  222. return type_a;
  223. } else {
  224. _mesa_glsl_error(loc, state,
  225. "vector size mismatch for arithmetic operator");
  226. return glsl_type::error_type;
  227. }
  228. }
  229. /* All of the combinations of <scalar, scalar>, <vector, scalar>,
  230. * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
  231. * <vector, vector> have been handled. At least one of the operands must
  232. * be matrix. Further, since there are no integer matrix types, the base
  233. * type of both operands must be float.
  234. */
  235. assert(type_a->is_matrix() || type_b->is_matrix());
  236. assert(type_a->base_type == GLSL_TYPE_FLOAT);
  237. assert(type_b->base_type == GLSL_TYPE_FLOAT);
  238. /* "* The operator is add (+), subtract (-), or divide (/), and the
  239. * operands are matrices with the same number of rows and the same
  240. * number of columns. In this case, the operation is done component-
  241. * wise resulting in the same size matrix."
  242. * * The operator is multiply (*), where both operands are matrices or
  243. * one operand is a vector and the other a matrix. A right vector
  244. * operand is treated as a column vector and a left vector operand as a
  245. * row vector. In all these cases, it is required that the number of
  246. * columns of the left operand is equal to the number of rows of the
  247. * right operand. Then, the multiply (*) operation does a linear
  248. * algebraic multiply, yielding an object that has the same number of
  249. * rows as the left operand and the same number of columns as the right
  250. * operand. Section 5.10 "Vector and Matrix Operations" explains in
  251. * more detail how vectors and matrices are operated on."
  252. */
  253. if (! multiply) {
  254. if (type_a == type_b)
  255. return type_a;
  256. } else {
  257. if (type_a->is_matrix() && type_b->is_matrix()) {
  258. /* Matrix multiply. The columns of A must match the rows of B. Given
  259. * the other previously tested constraints, this means the vector type
  260. * of a row from A must be the same as the vector type of a column from
  261. * B.
  262. */
  263. if (type_a->row_type() == type_b->column_type()) {
  264. /* The resulting matrix has the number of columns of matrix B and
  265. * the number of rows of matrix A. We get the row count of A by
  266. * looking at the size of a vector that makes up a column. The
  267. * transpose (size of a row) is done for B.
  268. */
  269. const glsl_type *const type =
  270. glsl_type::get_instance(type_a->base_type,
  271. type_a->column_type()->vector_elements,
  272. type_b->row_type()->vector_elements);
  273. assert(type != glsl_type::error_type);
  274. return type;
  275. }
  276. } else if (type_a->is_matrix()) {
  277. /* A is a matrix and B is a column vector. Columns of A must match
  278. * rows of B. Given the other previously tested constraints, this
  279. * means the vector type of a row from A must be the same as the
  280. * vector the type of B.
  281. */
  282. if (type_a->row_type() == type_b) {
  283. /* The resulting vector has a number of elements equal to
  284. * the number of rows of matrix A. */
  285. const glsl_type *const type =
  286. glsl_type::get_instance(type_a->base_type,
  287. type_a->column_type()->vector_elements,
  288. 1);
  289. assert(type != glsl_type::error_type);
  290. return type;
  291. }
  292. } else {
  293. assert(type_b->is_matrix());
  294. /* A is a row vector and B is a matrix. Columns of A must match rows
  295. * of B. Given the other previously tested constraints, this means
  296. * the type of A must be the same as the vector type of a column from
  297. * B.
  298. */
  299. if (type_a == type_b->column_type()) {
  300. /* The resulting vector has a number of elements equal to
  301. * the number of columns of matrix B. */
  302. const glsl_type *const type =
  303. glsl_type::get_instance(type_a->base_type,
  304. type_b->row_type()->vector_elements,
  305. 1);
  306. assert(type != glsl_type::error_type);
  307. return type;
  308. }
  309. }
  310. _mesa_glsl_error(loc, state, "size mismatch for matrix multiplication");
  311. return glsl_type::error_type;
  312. }
  313. /* "All other cases are illegal."
  314. */
  315. _mesa_glsl_error(loc, state, "type mismatch");
  316. return glsl_type::error_type;
  317. }
  318. static const struct glsl_type *
  319. unary_arithmetic_result_type(const struct glsl_type *type,
  320. struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
  321. {
  322. /* From GLSL 1.50 spec, page 57:
  323. *
  324. * "The arithmetic unary operators negate (-), post- and pre-increment
  325. * and decrement (-- and ++) operate on integer or floating-point
  326. * values (including vectors and matrices). All unary operators work
  327. * component-wise on their operands. These result with the same type
  328. * they operated on."
  329. */
  330. if (!type->is_numeric()) {
  331. _mesa_glsl_error(loc, state,
  332. "Operands to arithmetic operators must be numeric");
  333. return glsl_type::error_type;
  334. }
  335. return type;
  336. }
  337. /**
  338. * \brief Return the result type of a bit-logic operation.
  339. *
  340. * If the given types to the bit-logic operator are invalid, return
  341. * glsl_type::error_type.
  342. *
  343. * \param type_a Type of LHS of bit-logic op
  344. * \param type_b Type of RHS of bit-logic op
  345. */
  346. static const struct glsl_type *
  347. bit_logic_result_type(const struct glsl_type *type_a,
  348. const struct glsl_type *type_b,
  349. ast_operators op,
  350. struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
  351. {
  352. if (!state->check_bitwise_operations_allowed(loc)) {
  353. return glsl_type::error_type;
  354. }
  355. /* From page 50 (page 56 of PDF) of GLSL 1.30 spec:
  356. *
  357. * "The bitwise operators and (&), exclusive-or (^), and inclusive-or
  358. * (|). The operands must be of type signed or unsigned integers or
  359. * integer vectors."
  360. */
  361. if (!type_a->is_integer()) {
  362. _mesa_glsl_error(loc, state, "LHS of `%s' must be an integer",
  363. ast_expression::operator_string(op));
  364. return glsl_type::error_type;
  365. }
  366. if (!type_b->is_integer()) {
  367. _mesa_glsl_error(loc, state, "RHS of `%s' must be an integer",
  368. ast_expression::operator_string(op));
  369. return glsl_type::error_type;
  370. }
  371. /* "The fundamental types of the operands (signed or unsigned) must
  372. * match,"
  373. */
  374. if (type_a->base_type != type_b->base_type) {
  375. _mesa_glsl_error(loc, state, "operands of `%s' must have the same "
  376. "base type", ast_expression::operator_string(op));
  377. return glsl_type::error_type;
  378. }
  379. /* "The operands cannot be vectors of differing size." */
  380. if (type_a->is_vector() &&
  381. type_b->is_vector() &&
  382. type_a->vector_elements != type_b->vector_elements) {
  383. _mesa_glsl_error(loc, state, "operands of `%s' cannot be vectors of "
  384. "different sizes", ast_expression::operator_string(op));
  385. return glsl_type::error_type;
  386. }
  387. /* "If one operand is a scalar and the other a vector, the scalar is
  388. * applied component-wise to the vector, resulting in the same type as
  389. * the vector. The fundamental types of the operands [...] will be the
  390. * resulting fundamental type."
  391. */
  392. if (type_a->is_scalar())
  393. return type_b;
  394. else
  395. return type_a;
  396. }
  397. static const struct glsl_type *
  398. modulus_result_type(const struct glsl_type *type_a,
  399. const struct glsl_type *type_b,
  400. struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
  401. {
  402. if (!state->check_version(130, 300, loc, "operator '%%' is reserved")) {
  403. return glsl_type::error_type;
  404. }
  405. /* From GLSL 1.50 spec, page 56:
  406. * "The operator modulus (%) operates on signed or unsigned integers or
  407. * integer vectors. The operand types must both be signed or both be
  408. * unsigned."
  409. */
  410. if (!type_a->is_integer()) {
  411. _mesa_glsl_error(loc, state, "LHS of operator %% must be an integer.");
  412. return glsl_type::error_type;
  413. }
  414. if (!type_b->is_integer()) {
  415. _mesa_glsl_error(loc, state, "RHS of operator %% must be an integer.");
  416. return glsl_type::error_type;
  417. }
  418. if (type_a->base_type != type_b->base_type) {
  419. _mesa_glsl_error(loc, state,
  420. "operands of %% must have the same base type");
  421. return glsl_type::error_type;
  422. }
  423. /* "The operands cannot be vectors of differing size. If one operand is
  424. * a scalar and the other vector, then the scalar is applied component-
  425. * wise to the vector, resulting in the same type as the vector. If both
  426. * are vectors of the same size, the result is computed component-wise."
  427. */
  428. if (type_a->is_vector()) {
  429. if (!type_b->is_vector()
  430. || (type_a->vector_elements == type_b->vector_elements))
  431. return type_a;
  432. } else
  433. return type_b;
  434. /* "The operator modulus (%) is not defined for any other data types
  435. * (non-integer types)."
  436. */
  437. _mesa_glsl_error(loc, state, "type mismatch");
  438. return glsl_type::error_type;
  439. }
  440. static const struct glsl_type *
  441. relational_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
  442. struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
  443. {
  444. const glsl_type *type_a = value_a->type;
  445. const glsl_type *type_b = value_b->type;
  446. /* From GLSL 1.50 spec, page 56:
  447. * "The relational operators greater than (>), less than (<), greater
  448. * than or equal (>=), and less than or equal (<=) operate only on
  449. * scalar integer and scalar floating-point expressions."
  450. */
  451. if (!type_a->is_numeric()
  452. || !type_b->is_numeric()
  453. || !type_a->is_scalar()
  454. || !type_b->is_scalar()) {
  455. _mesa_glsl_error(loc, state,
  456. "Operands to relational operators must be scalar and "
  457. "numeric");
  458. return glsl_type::error_type;
  459. }
  460. /* "Either the operands' types must match, or the conversions from
  461. * Section 4.1.10 "Implicit Conversions" will be applied to the integer
  462. * operand, after which the types must match."
  463. */
  464. if (!apply_implicit_conversion(type_a, value_b, state)
  465. && !apply_implicit_conversion(type_b, value_a, state)) {
  466. _mesa_glsl_error(loc, state,
  467. "Could not implicitly convert operands to "
  468. "relational operator");
  469. return glsl_type::error_type;
  470. }
  471. type_a = value_a->type;
  472. type_b = value_b->type;
  473. if (type_a->base_type != type_b->base_type) {
  474. _mesa_glsl_error(loc, state, "base type mismatch");
  475. return glsl_type::error_type;
  476. }
  477. /* "The result is scalar Boolean."
  478. */
  479. return glsl_type::bool_type;
  480. }
  481. /**
  482. * \brief Return the result type of a bit-shift operation.
  483. *
  484. * If the given types to the bit-shift operator are invalid, return
  485. * glsl_type::error_type.
  486. *
  487. * \param type_a Type of LHS of bit-shift op
  488. * \param type_b Type of RHS of bit-shift op
  489. */
  490. static const struct glsl_type *
  491. shift_result_type(const struct glsl_type *type_a,
  492. const struct glsl_type *type_b,
  493. ast_operators op,
  494. struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
  495. {
  496. if (!state->check_bitwise_operations_allowed(loc)) {
  497. return glsl_type::error_type;
  498. }
  499. /* From page 50 (page 56 of the PDF) of the GLSL 1.30 spec:
  500. *
  501. * "The shift operators (<<) and (>>). For both operators, the operands
  502. * must be signed or unsigned integers or integer vectors. One operand
  503. * can be signed while the other is unsigned."
  504. */
  505. if (!type_a->is_integer()) {
  506. _mesa_glsl_error(loc, state, "LHS of operator %s must be an integer or "
  507. "integer vector", ast_expression::operator_string(op));
  508. return glsl_type::error_type;
  509. }
  510. if (!type_b->is_integer()) {
  511. _mesa_glsl_error(loc, state, "RHS of operator %s must be an integer or "
  512. "integer vector", ast_expression::operator_string(op));
  513. return glsl_type::error_type;
  514. }
  515. /* "If the first operand is a scalar, the second operand has to be
  516. * a scalar as well."
  517. */
  518. if (type_a->is_scalar() && !type_b->is_scalar()) {
  519. _mesa_glsl_error(loc, state, "If the first operand of %s is scalar, the "
  520. "second must be scalar as well",
  521. ast_expression::operator_string(op));
  522. return glsl_type::error_type;
  523. }
  524. /* If both operands are vectors, check that they have same number of
  525. * elements.
  526. */
  527. if (type_a->is_vector() &&
  528. type_b->is_vector() &&
  529. type_a->vector_elements != type_b->vector_elements) {
  530. _mesa_glsl_error(loc, state, "Vector operands to operator %s must "
  531. "have same number of elements",
  532. ast_expression::operator_string(op));
  533. return glsl_type::error_type;
  534. }
  535. /* "In all cases, the resulting type will be the same type as the left
  536. * operand."
  537. */
  538. return type_a;
  539. }
  540. /**
  541. * Validates that a value can be assigned to a location with a specified type
  542. *
  543. * Validates that \c rhs can be assigned to some location. If the types are
  544. * not an exact match but an automatic conversion is possible, \c rhs will be
  545. * converted.
  546. *
  547. * \return
  548. * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
  549. * Otherwise the actual RHS to be assigned will be returned. This may be
  550. * \c rhs, or it may be \c rhs after some type conversion.
  551. *
  552. * \note
  553. * In addition to being used for assignments, this function is used to
  554. * type-check return values.
  555. */
  556. ir_rvalue *
  557. validate_assignment(struct _mesa_glsl_parse_state *state,
  558. const glsl_type *lhs_type, ir_rvalue *rhs,
  559. bool is_initializer)
  560. {
  561. /* If there is already some error in the RHS, just return it. Anything
  562. * else will lead to an avalanche of error message back to the user.
  563. */
  564. if (rhs->type->is_error())
  565. return rhs;
  566. /* If the types are identical, the assignment can trivially proceed.
  567. */
  568. if (rhs->type == lhs_type)
  569. return rhs;
  570. /* If the array element types are the same and the size of the LHS is zero,
  571. * the assignment is okay for initializers embedded in variable
  572. * declarations.
  573. *
  574. * Note: Whole-array assignments are not permitted in GLSL 1.10, but this
  575. * is handled by ir_dereference::is_lvalue.
  576. */
  577. if (is_initializer && lhs_type->is_array() && rhs->type->is_array()
  578. && (lhs_type->element_type() == rhs->type->element_type())
  579. && (lhs_type->array_size() == 0)) {
  580. return rhs;
  581. }
  582. /* Check for implicit conversion in GLSL 1.20 */
  583. if (apply_implicit_conversion(lhs_type, rhs, state)) {
  584. if (rhs->type == lhs_type)
  585. return rhs;
  586. }
  587. return NULL;
  588. }
  589. static void
  590. mark_whole_array_access(ir_rvalue *access)
  591. {
  592. ir_dereference_variable *deref = access->as_dereference_variable();
  593. if (deref && deref->var) {
  594. deref->var->max_array_access = deref->type->length - 1;
  595. }
  596. }
  597. ir_rvalue *
  598. do_assignment(exec_list *instructions, struct _mesa_glsl_parse_state *state,
  599. const char *non_lvalue_description,
  600. ir_rvalue *lhs, ir_rvalue *rhs, bool is_initializer,
  601. YYLTYPE lhs_loc)
  602. {
  603. void *ctx = state;
  604. bool error_emitted = (lhs->type->is_error() || rhs->type->is_error());
  605. ir_variable *lhs_var = lhs->variable_referenced();
  606. if (lhs_var)
  607. lhs_var->assigned = true;
  608. if (!error_emitted) {
  609. if (non_lvalue_description != NULL) {
  610. _mesa_glsl_error(&lhs_loc, state,
  611. "assignment to %s",
  612. non_lvalue_description);
  613. error_emitted = true;
  614. } else if (lhs->variable_referenced() != NULL
  615. && lhs->variable_referenced()->read_only) {
  616. _mesa_glsl_error(&lhs_loc, state,
  617. "assignment to read-only variable '%s'",
  618. lhs->variable_referenced()->name);
  619. error_emitted = true;
  620. } else if (lhs->type->is_array() &&
  621. !state->check_version(120, 300, &lhs_loc,
  622. "whole array assignment forbidden")) {
  623. /* From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
  624. *
  625. * "Other binary or unary expressions, non-dereferenced
  626. * arrays, function names, swizzles with repeated fields,
  627. * and constants cannot be l-values."
  628. *
  629. * The restriction on arrays is lifted in GLSL 1.20 and GLSL ES 3.00.
  630. */
  631. error_emitted = true;
  632. } else if (!lhs->is_lvalue()) {
  633. _mesa_glsl_error(& lhs_loc, state, "non-lvalue in assignment");
  634. error_emitted = true;
  635. }
  636. }
  637. ir_rvalue *new_rhs =
  638. validate_assignment(state, lhs->type, rhs, is_initializer);
  639. if (new_rhs == NULL) {
  640. _mesa_glsl_error(& lhs_loc, state, "type mismatch");
  641. } else {
  642. rhs = new_rhs;
  643. /* If the LHS array was not declared with a size, it takes it size from
  644. * the RHS. If the LHS is an l-value and a whole array, it must be a
  645. * dereference of a variable. Any other case would require that the LHS
  646. * is either not an l-value or not a whole array.
  647. */
  648. if (lhs->type->array_size() == 0) {
  649. ir_dereference *const d = lhs->as_dereference();
  650. assert(d != NULL);
  651. ir_variable *const var = d->variable_referenced();
  652. assert(var != NULL);
  653. if (var->max_array_access >= unsigned(rhs->type->array_size())) {
  654. /* FINISHME: This should actually log the location of the RHS. */
  655. _mesa_glsl_error(& lhs_loc, state, "array size must be > %u due to "
  656. "previous access",
  657. var->max_array_access);
  658. }
  659. var->type = glsl_type::get_array_instance(lhs->type->element_type(),
  660. rhs->type->array_size());
  661. d->type = var->type;
  662. }
  663. mark_whole_array_access(rhs);
  664. mark_whole_array_access(lhs);
  665. }
  666. /* Most callers of do_assignment (assign, add_assign, pre_inc/dec,
  667. * but not post_inc) need the converted assigned value as an rvalue
  668. * to handle things like:
  669. *
  670. * i = j += 1;
  671. *
  672. * So we always just store the computed value being assigned to a
  673. * temporary and return a deref of that temporary. If the rvalue
  674. * ends up not being used, the temp will get copy-propagated out.
  675. */
  676. ir_variable *var = new(ctx) ir_variable(rhs->type, "assignment_tmp",
  677. ir_var_temporary);
  678. ir_dereference_variable *deref_var = new(ctx) ir_dereference_variable(var);
  679. instructions->push_tail(var);
  680. instructions->push_tail(new(ctx) ir_assignment(deref_var, rhs));
  681. deref_var = new(ctx) ir_dereference_variable(var);
  682. if (!error_emitted)
  683. instructions->push_tail(new(ctx) ir_assignment(lhs, deref_var));
  684. return new(ctx) ir_dereference_variable(var);
  685. }
  686. static ir_rvalue *
  687. get_lvalue_copy(exec_list *instructions, ir_rvalue *lvalue)
  688. {
  689. void *ctx = ralloc_parent(lvalue);
  690. ir_variable *var;
  691. var = new(ctx) ir_variable(lvalue->type, "_post_incdec_tmp",
  692. ir_var_temporary);
  693. instructions->push_tail(var);
  694. var->mode = ir_var_auto;
  695. instructions->push_tail(new(ctx) ir_assignment(new(ctx) ir_dereference_variable(var),
  696. lvalue));
  697. return new(ctx) ir_dereference_variable(var);
  698. }
  699. ir_rvalue *
  700. ast_node::hir(exec_list *instructions,
  701. struct _mesa_glsl_parse_state *state)
  702. {
  703. (void) instructions;
  704. (void) state;
  705. return NULL;
  706. }
  707. static ir_rvalue *
  708. do_comparison(void *mem_ctx, int operation, ir_rvalue *op0, ir_rvalue *op1)
  709. {
  710. int join_op;
  711. ir_rvalue *cmp = NULL;
  712. if (operation == ir_binop_all_equal)
  713. join_op = ir_binop_logic_and;
  714. else
  715. join_op = ir_binop_logic_or;
  716. switch (op0->type->base_type) {
  717. case GLSL_TYPE_FLOAT:
  718. case GLSL_TYPE_UINT:
  719. case GLSL_TYPE_INT:
  720. case GLSL_TYPE_BOOL:
  721. return new(mem_ctx) ir_expression(operation, op0, op1);
  722. case GLSL_TYPE_ARRAY: {
  723. for (unsigned int i = 0; i < op0->type->length; i++) {
  724. ir_rvalue *e0, *e1, *result;
  725. e0 = new(mem_ctx) ir_dereference_array(op0->clone(mem_ctx, NULL),
  726. new(mem_ctx) ir_constant(i));
  727. e1 = new(mem_ctx) ir_dereference_array(op1->clone(mem_ctx, NULL),
  728. new(mem_ctx) ir_constant(i));
  729. result = do_comparison(mem_ctx, operation, e0, e1);
  730. if (cmp) {
  731. cmp = new(mem_ctx) ir_expression(join_op, cmp, result);
  732. } else {
  733. cmp = result;
  734. }
  735. }
  736. mark_whole_array_access(op0);
  737. mark_whole_array_access(op1);
  738. break;
  739. }
  740. case GLSL_TYPE_STRUCT: {
  741. for (unsigned int i = 0; i < op0->type->length; i++) {
  742. ir_rvalue *e0, *e1, *result;
  743. const char *field_name = op0->type->fields.structure[i].name;
  744. e0 = new(mem_ctx) ir_dereference_record(op0->clone(mem_ctx, NULL),
  745. field_name);
  746. e1 = new(mem_ctx) ir_dereference_record(op1->clone(mem_ctx, NULL),
  747. field_name);
  748. result = do_comparison(mem_ctx, operation, e0, e1);
  749. if (cmp) {
  750. cmp = new(mem_ctx) ir_expression(join_op, cmp, result);
  751. } else {
  752. cmp = result;
  753. }
  754. }
  755. break;
  756. }
  757. case GLSL_TYPE_ERROR:
  758. case GLSL_TYPE_VOID:
  759. case GLSL_TYPE_SAMPLER:
  760. case GLSL_TYPE_INTERFACE:
  761. /* I assume a comparison of a struct containing a sampler just
  762. * ignores the sampler present in the type.
  763. */
  764. break;
  765. }
  766. if (cmp == NULL)
  767. cmp = new(mem_ctx) ir_constant(true);
  768. return cmp;
  769. }
  770. /* For logical operations, we want to ensure that the operands are
  771. * scalar booleans. If it isn't, emit an error and return a constant
  772. * boolean to avoid triggering cascading error messages.
  773. */
  774. ir_rvalue *
  775. get_scalar_boolean_operand(exec_list *instructions,
  776. struct _mesa_glsl_parse_state *state,
  777. ast_expression *parent_expr,
  778. int operand,
  779. const char *operand_name,
  780. bool *error_emitted)
  781. {
  782. ast_expression *expr = parent_expr->subexpressions[operand];
  783. void *ctx = state;
  784. ir_rvalue *val = expr->hir(instructions, state);
  785. if (val->type->is_boolean() && val->type->is_scalar())
  786. return val;
  787. if (!*error_emitted) {
  788. YYLTYPE loc = expr->get_location();
  789. _mesa_glsl_error(&loc, state, "%s of `%s' must be scalar boolean",
  790. operand_name,
  791. parent_expr->operator_string(parent_expr->oper));
  792. *error_emitted = true;
  793. }
  794. return new(ctx) ir_constant(true);
  795. }
  796. /**
  797. * If name refers to a builtin array whose maximum allowed size is less than
  798. * size, report an error and return true. Otherwise return false.
  799. */
  800. void
  801. check_builtin_array_max_size(const char *name, unsigned size,
  802. YYLTYPE loc, struct _mesa_glsl_parse_state *state)
  803. {
  804. if ((strcmp("gl_TexCoord", name) == 0)
  805. && (size > state->Const.MaxTextureCoords)) {
  806. /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
  807. *
  808. * "The size [of gl_TexCoord] can be at most
  809. * gl_MaxTextureCoords."
  810. */
  811. _mesa_glsl_error(&loc, state, "`gl_TexCoord' array size cannot "
  812. "be larger than gl_MaxTextureCoords (%u)\n",
  813. state->Const.MaxTextureCoords);
  814. } else if (strcmp("gl_ClipDistance", name) == 0
  815. && size > state->Const.MaxClipPlanes) {
  816. /* From section 7.1 (Vertex Shader Special Variables) of the
  817. * GLSL 1.30 spec:
  818. *
  819. * "The gl_ClipDistance array is predeclared as unsized and
  820. * must be sized by the shader either redeclaring it with a
  821. * size or indexing it only with integral constant
  822. * expressions. ... The size can be at most
  823. * gl_MaxClipDistances."
  824. */
  825. _mesa_glsl_error(&loc, state, "`gl_ClipDistance' array size cannot "
  826. "be larger than gl_MaxClipDistances (%u)\n",
  827. state->Const.MaxClipPlanes);
  828. }
  829. }
  830. /**
  831. * Create the constant 1, of a which is appropriate for incrementing and
  832. * decrementing values of the given GLSL type. For example, if type is vec4,
  833. * this creates a constant value of 1.0 having type float.
  834. *
  835. * If the given type is invalid for increment and decrement operators, return
  836. * a floating point 1--the error will be detected later.
  837. */
  838. static ir_rvalue *
  839. constant_one_for_inc_dec(void *ctx, const glsl_type *type)
  840. {
  841. switch (type->base_type) {
  842. case GLSL_TYPE_UINT:
  843. return new(ctx) ir_constant((unsigned) 1);
  844. case GLSL_TYPE_INT:
  845. return new(ctx) ir_constant(1);
  846. default:
  847. case GLSL_TYPE_FLOAT:
  848. return new(ctx) ir_constant(1.0f);
  849. }
  850. }
  851. ir_rvalue *
  852. ast_expression::hir(exec_list *instructions,
  853. struct _mesa_glsl_parse_state *state)
  854. {
  855. void *ctx = state;
  856. static const int operations[AST_NUM_OPERATORS] = {
  857. -1, /* ast_assign doesn't convert to ir_expression. */
  858. -1, /* ast_plus doesn't convert to ir_expression. */
  859. ir_unop_neg,
  860. ir_binop_add,
  861. ir_binop_sub,
  862. ir_binop_mul,
  863. ir_binop_div,
  864. ir_binop_mod,
  865. ir_binop_lshift,
  866. ir_binop_rshift,
  867. ir_binop_less,
  868. ir_binop_greater,
  869. ir_binop_lequal,
  870. ir_binop_gequal,
  871. ir_binop_all_equal,
  872. ir_binop_any_nequal,
  873. ir_binop_bit_and,
  874. ir_binop_bit_xor,
  875. ir_binop_bit_or,
  876. ir_unop_bit_not,
  877. ir_binop_logic_and,
  878. ir_binop_logic_xor,
  879. ir_binop_logic_or,
  880. ir_unop_logic_not,
  881. /* Note: The following block of expression types actually convert
  882. * to multiple IR instructions.
  883. */
  884. ir_binop_mul, /* ast_mul_assign */
  885. ir_binop_div, /* ast_div_assign */
  886. ir_binop_mod, /* ast_mod_assign */
  887. ir_binop_add, /* ast_add_assign */
  888. ir_binop_sub, /* ast_sub_assign */
  889. ir_binop_lshift, /* ast_ls_assign */
  890. ir_binop_rshift, /* ast_rs_assign */
  891. ir_binop_bit_and, /* ast_and_assign */
  892. ir_binop_bit_xor, /* ast_xor_assign */
  893. ir_binop_bit_or, /* ast_or_assign */
  894. -1, /* ast_conditional doesn't convert to ir_expression. */
  895. ir_binop_add, /* ast_pre_inc. */
  896. ir_binop_sub, /* ast_pre_dec. */
  897. ir_binop_add, /* ast_post_inc. */
  898. ir_binop_sub, /* ast_post_dec. */
  899. -1, /* ast_field_selection doesn't conv to ir_expression. */
  900. -1, /* ast_array_index doesn't convert to ir_expression. */
  901. -1, /* ast_function_call doesn't conv to ir_expression. */
  902. -1, /* ast_identifier doesn't convert to ir_expression. */
  903. -1, /* ast_int_constant doesn't convert to ir_expression. */
  904. -1, /* ast_uint_constant doesn't conv to ir_expression. */
  905. -1, /* ast_float_constant doesn't conv to ir_expression. */
  906. -1, /* ast_bool_constant doesn't conv to ir_expression. */
  907. -1, /* ast_sequence doesn't convert to ir_expression. */
  908. };
  909. ir_rvalue *result = NULL;
  910. ir_rvalue *op[3];
  911. const struct glsl_type *type; /* a temporary variable for switch cases */
  912. bool error_emitted = false;
  913. YYLTYPE loc;
  914. loc = this->get_location();
  915. switch (this->oper) {
  916. case ast_assign: {
  917. op[0] = this->subexpressions[0]->hir(instructions, state);
  918. op[1] = this->subexpressions[1]->hir(instructions, state);
  919. result = do_assignment(instructions, state,
  920. this->subexpressions[0]->non_lvalue_description,
  921. op[0], op[1], false,
  922. this->subexpressions[0]->get_location());
  923. error_emitted = result->type->is_error();
  924. break;
  925. }
  926. case ast_plus:
  927. op[0] = this->subexpressions[0]->hir(instructions, state);
  928. type = unary_arithmetic_result_type(op[0]->type, state, & loc);
  929. error_emitted = type->is_error();
  930. result = op[0];
  931. break;
  932. case ast_neg:
  933. op[0] = this->subexpressions[0]->hir(instructions, state);
  934. type = unary_arithmetic_result_type(op[0]->type, state, & loc);
  935. error_emitted = type->is_error();
  936. result = new(ctx) ir_expression(operations[this->oper], type,
  937. op[0], NULL);
  938. break;
  939. case ast_add:
  940. case ast_sub:
  941. case ast_mul:
  942. case ast_div:
  943. op[0] = this->subexpressions[0]->hir(instructions, state);
  944. op[1] = this->subexpressions[1]->hir(instructions, state);
  945. type = arithmetic_result_type(op[0], op[1],
  946. (this->oper == ast_mul),
  947. state, & loc);
  948. error_emitted = type->is_error();
  949. result = new(ctx) ir_expression(operations[this->oper], type,
  950. op[0], op[1]);
  951. break;
  952. case ast_mod:
  953. op[0] = this->subexpressions[0]->hir(instructions, state);
  954. op[1] = this->subexpressions[1]->hir(instructions, state);
  955. type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
  956. assert(operations[this->oper] == ir_binop_mod);
  957. result = new(ctx) ir_expression(operations[this->oper], type,
  958. op[0], op[1]);
  959. error_emitted = type->is_error();
  960. break;
  961. case ast_lshift:
  962. case ast_rshift:
  963. if (!state->check_bitwise_operations_allowed(&loc)) {
  964. error_emitted = true;
  965. }
  966. op[0] = this->subexpressions[0]->hir(instructions, state);
  967. op[1] = this->subexpressions[1]->hir(instructions, state);
  968. type = shift_result_type(op[0]->type, op[1]->type, this->oper, state,
  969. &loc);
  970. result = new(ctx) ir_expression(operations[this->oper], type,
  971. op[0], op[1]);
  972. error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
  973. break;
  974. case ast_less:
  975. case ast_greater:
  976. case ast_lequal:
  977. case ast_gequal:
  978. op[0] = this->subexpressions[0]->hir(instructions, state);
  979. op[1] = this->subexpressions[1]->hir(instructions, state);
  980. type = relational_result_type(op[0], op[1], state, & loc);
  981. /* The relational operators must either generate an error or result
  982. * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
  983. */
  984. assert(type->is_error()
  985. || ((type->base_type == GLSL_TYPE_BOOL)
  986. && type->is_scalar()));
  987. result = new(ctx) ir_expression(operations[this->oper], type,
  988. op[0], op[1]);
  989. error_emitted = type->is_error();
  990. break;
  991. case ast_nequal:
  992. case ast_equal:
  993. op[0] = this->subexpressions[0]->hir(instructions, state);
  994. op[1] = this->subexpressions[1]->hir(instructions, state);
  995. /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
  996. *
  997. * "The equality operators equal (==), and not equal (!=)
  998. * operate on all types. They result in a scalar Boolean. If
  999. * the operand types do not match, then there must be a
  1000. * conversion from Section 4.1.10 "Implicit Conversions"
  1001. * applied to one operand that can make them match, in which
  1002. * case this conversion is done."
  1003. */
  1004. if ((!apply_implicit_conversion(op[0]->type, op[1], state)
  1005. && !apply_implicit_conversion(op[1]->type, op[0], state))
  1006. || (op[0]->type != op[1]->type)) {
  1007. _mesa_glsl_error(& loc, state, "operands of `%s' must have the same "
  1008. "type", (this->oper == ast_equal) ? "==" : "!=");
  1009. error_emitted = true;
  1010. } else if ((op[0]->type->is_array() || op[1]->type->is_array()) &&
  1011. !state->check_version(120, 300, &loc,
  1012. "array comparisons forbidden")) {
  1013. error_emitted = true;
  1014. }
  1015. if (error_emitted) {
  1016. result = new(ctx) ir_constant(false);
  1017. } else {
  1018. result = do_comparison(ctx, operations[this->oper], op[0], op[1]);
  1019. assert(result->type == glsl_type::bool_type);
  1020. }
  1021. break;
  1022. case ast_bit_and:
  1023. case ast_bit_xor:
  1024. case ast_bit_or:
  1025. op[0] = this->subexpressions[0]->hir(instructions, state);
  1026. op[1] = this->subexpressions[1]->hir(instructions, state);
  1027. type = bit_logic_result_type(op[0]->type, op[1]->type, this->oper,
  1028. state, &loc);
  1029. result = new(ctx) ir_expression(operations[this->oper], type,
  1030. op[0], op[1]);
  1031. error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
  1032. break;
  1033. case ast_bit_not:
  1034. op[0] = this->subexpressions[0]->hir(instructions, state);
  1035. if (!state->check_bitwise_operations_allowed(&loc)) {
  1036. error_emitted = true;
  1037. }
  1038. if (!op[0]->type->is_integer()) {
  1039. _mesa_glsl_error(&loc, state, "operand of `~' must be an integer");
  1040. error_emitted = true;
  1041. }
  1042. type = error_emitted ? glsl_type::error_type : op[0]->type;
  1043. result = new(ctx) ir_expression(ir_unop_bit_not, type, op[0], NULL);
  1044. break;
  1045. case ast_logic_and: {
  1046. exec_list rhs_instructions;
  1047. op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
  1048. "LHS", &error_emitted);
  1049. op[1] = get_scalar_boolean_operand(&rhs_instructions, state, this, 1,
  1050. "RHS", &error_emitted);
  1051. if (rhs_instructions.is_empty()) {
  1052. result = new(ctx) ir_expression(ir_binop_logic_and, op[0], op[1]);
  1053. type = result->type;
  1054. } else {
  1055. ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type,
  1056. "and_tmp",
  1057. ir_var_temporary);
  1058. instructions->push_tail(tmp);
  1059. ir_if *const stmt = new(ctx) ir_if(op[0]);
  1060. instructions->push_tail(stmt);
  1061. stmt->then_instructions.append_list(&rhs_instructions);
  1062. ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp);
  1063. ir_assignment *const then_assign =
  1064. new(ctx) ir_assignment(then_deref, op[1]);
  1065. stmt->then_instructions.push_tail(then_assign);
  1066. ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp);
  1067. ir_assignment *const else_assign =
  1068. new(ctx) ir_assignment(else_deref, new(ctx) ir_constant(false));
  1069. stmt->else_instructions.push_tail(else_assign);
  1070. result = new(ctx) ir_dereference_variable(tmp);
  1071. type = tmp->type;
  1072. }
  1073. break;
  1074. }
  1075. case ast_logic_or: {
  1076. exec_list rhs_instructions;
  1077. op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
  1078. "LHS", &error_emitted);
  1079. op[1] = get_scalar_boolean_operand(&rhs_instructions, state, this, 1,
  1080. "RHS", &error_emitted);
  1081. if (rhs_instructions.is_empty()) {
  1082. result = new(ctx) ir_expression(ir_binop_logic_or, op[0], op[1]);
  1083. type = result->type;
  1084. } else {
  1085. ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type,
  1086. "or_tmp",
  1087. ir_var_temporary);
  1088. instructions->push_tail(tmp);
  1089. ir_if *const stmt = new(ctx) ir_if(op[0]);
  1090. instructions->push_tail(stmt);
  1091. ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp);
  1092. ir_assignment *const then_assign =
  1093. new(ctx) ir_assignment(then_deref, new(ctx) ir_constant(true));
  1094. stmt->then_instructions.push_tail(then_assign);
  1095. stmt->else_instructions.append_list(&rhs_instructions);
  1096. ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp);
  1097. ir_assignment *const else_assign =
  1098. new(ctx) ir_assignment(else_deref, op[1]);
  1099. stmt->else_instructions.push_tail(else_assign);
  1100. result = new(ctx) ir_dereference_variable(tmp);
  1101. type = tmp->type;
  1102. }
  1103. break;
  1104. }
  1105. case ast_logic_xor:
  1106. /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
  1107. *
  1108. * "The logical binary operators and (&&), or ( | | ), and
  1109. * exclusive or (^^). They operate only on two Boolean
  1110. * expressions and result in a Boolean expression."
  1111. */
  1112. op[0] = get_scalar_boolean_operand(instructions, state, this, 0, "LHS",
  1113. &error_emitted);
  1114. op[1] = get_scalar_boolean_operand(instructions, state, this, 1, "RHS",
  1115. &error_emitted);
  1116. result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
  1117. op[0], op[1]);
  1118. break;
  1119. case ast_logic_not:
  1120. op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
  1121. "operand", &error_emitted);
  1122. result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
  1123. op[0], NULL);
  1124. break;
  1125. case ast_mul_assign:
  1126. case ast_div_assign:
  1127. case ast_add_assign:
  1128. case ast_sub_assign: {
  1129. op[0] = this->subexpressions[0]->hir(instructions, state);
  1130. op[1] = this->subexpressions[1]->hir(instructions, state);
  1131. type = arithmetic_result_type(op[0], op[1],
  1132. (this->oper == ast_mul_assign),
  1133. state, & loc);
  1134. ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
  1135. op[0], op[1]);
  1136. result = do_assignment(instructions, state,
  1137. this->subexpressions[0]->non_lvalue_description,
  1138. op[0]->clone(ctx, NULL), temp_rhs, false,
  1139. this->subexpressions[0]->get_location());
  1140. error_emitted = (op[0]->type->is_error());
  1141. /* GLSL 1.10 does not allow array assignment. However, we don't have to
  1142. * explicitly test for this because none of the binary expression
  1143. * operators allow array operands either.
  1144. */
  1145. break;
  1146. }
  1147. case ast_mod_assign: {
  1148. op[0] = this->subexpressions[0]->hir(instructions, state);
  1149. op[1] = this->subexpressions[1]->hir(instructions, state);
  1150. type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
  1151. assert(operations[this->oper] == ir_binop_mod);
  1152. ir_rvalue *temp_rhs;
  1153. temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
  1154. op[0], op[1]);
  1155. result = do_assignment(instructions, state,
  1156. this->subexpressions[0]->non_lvalue_description,
  1157. op[0]->clone(ctx, NULL), temp_rhs, false,
  1158. this->subexpressions[0]->get_location());
  1159. error_emitted = type->is_error();
  1160. break;
  1161. }
  1162. case ast_ls_assign:
  1163. case ast_rs_assign: {
  1164. op[0] = this->subexpressions[0]->hir(instructions, state);
  1165. op[1] = this->subexpressions[1]->hir(instructions, state);
  1166. type = shift_result_type(op[0]->type, op[1]->type, this->oper, state,
  1167. &loc);
  1168. ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper],
  1169. type, op[0], op[1]);
  1170. result = do_assignment(instructions, state,
  1171. this->subexpressions[0]->non_lvalue_description,
  1172. op[0]->clone(ctx, NULL), temp_rhs, false,
  1173. this->subexpressions[0]->get_location());
  1174. error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
  1175. break;
  1176. }
  1177. case ast_and_assign:
  1178. case ast_xor_assign:
  1179. case ast_or_assign: {
  1180. op[0] = this->subexpressions[0]->hir(instructions, state);
  1181. op[1] = this->subexpressions[1]->hir(instructions, state);
  1182. type = bit_logic_result_type(op[0]->type, op[1]->type, this->oper,
  1183. state, &loc);
  1184. ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper],
  1185. type, op[0], op[1]);
  1186. result = do_assignment(instructions, state,
  1187. this->subexpressions[0]->non_lvalue_description,
  1188. op[0]->clone(ctx, NULL), temp_rhs, false,
  1189. this->subexpressions[0]->get_location());
  1190. error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
  1191. break;
  1192. }
  1193. case ast_conditional: {
  1194. /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
  1195. *
  1196. * "The ternary selection operator (?:). It operates on three
  1197. * expressions (exp1 ? exp2 : exp3). This operator evaluates the
  1198. * first expression, which must result in a scalar Boolean."
  1199. */
  1200. op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
  1201. "condition", &error_emitted);
  1202. /* The :? operator is implemented by generating an anonymous temporary
  1203. * followed by an if-statement. The last instruction in each branch of
  1204. * the if-statement assigns a value to the anonymous temporary. This
  1205. * temporary is the r-value of the expression.
  1206. */
  1207. exec_list then_instructions;
  1208. exec_list else_instructions;
  1209. op[1] = this->subexpressions[1]->hir(&then_instructions, state);
  1210. op[2] = this->subexpressions[2]->hir(&else_instructions, state);
  1211. /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
  1212. *
  1213. * "The second and third expressions can be any type, as
  1214. * long their types match, or there is a conversion in
  1215. * Section 4.1.10 "Implicit Conversions" that can be applied
  1216. * to one of the expressions to make their types match. This
  1217. * resulting matching type is the type of the entire
  1218. * expression."
  1219. */
  1220. if ((!apply_implicit_conversion(op[1]->type, op[2], state)
  1221. && !apply_implicit_conversion(op[2]->type, op[1], state))
  1222. || (op[1]->type != op[2]->type)) {
  1223. YYLTYPE loc = this->subexpressions[1]->get_location();
  1224. _mesa_glsl_error(& loc, state, "Second and third operands of ?: "
  1225. "operator must have matching types.");
  1226. error_emitted = true;
  1227. type = glsl_type::error_type;
  1228. } else {
  1229. type = op[1]->type;
  1230. }
  1231. /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
  1232. *
  1233. * "The second and third expressions must be the same type, but can
  1234. * be of any type other than an array."
  1235. */
  1236. if (type->is_array() &&
  1237. !state->check_version(120, 300, &loc,
  1238. "Second and third operands of ?: operator "
  1239. "cannot be arrays")) {
  1240. error_emitted = true;
  1241. }
  1242. ir_constant *cond_val = op[0]->constant_expression_value();
  1243. ir_constant *then_val = op[1]->constant_expression_value();
  1244. ir_constant *else_val = op[2]->constant_expression_value();
  1245. if (then_instructions.is_empty()
  1246. && else_instructions.is_empty()
  1247. && (cond_val != NULL) && (then_val != NULL) && (else_val != NULL)) {
  1248. result = (cond_val->value.b[0]) ? then_val : else_val;
  1249. } else {
  1250. ir_variable *const tmp =
  1251. new(ctx) ir_variable(type, "conditional_tmp", ir_var_temporary);
  1252. instructions->push_tail(tmp);
  1253. ir_if *const stmt = new(ctx) ir_if(op[0]);
  1254. instructions->push_tail(stmt);
  1255. then_instructions.move_nodes_to(& stmt->then_instructions);
  1256. ir_dereference *const then_deref =
  1257. new(ctx) ir_dereference_variable(tmp);
  1258. ir_assignment *const then_assign =
  1259. new(ctx) ir_assignment(then_deref, op[1]);
  1260. stmt->then_instructions.push_tail(then_assign);
  1261. else_instructions.move_nodes_to(& stmt->else_instructions);
  1262. ir_dereference *const else_deref =
  1263. new(ctx) ir_dereference_variable(tmp);
  1264. ir_assignment *const else_assign =
  1265. new(ctx) ir_assignment(else_deref, op[2]);
  1266. stmt->else_instructions.push_tail(else_assign);
  1267. result = new(ctx) ir_dereference_variable(tmp);
  1268. }
  1269. break;
  1270. }
  1271. case ast_pre_inc:
  1272. case ast_pre_dec: {
  1273. this->non_lvalue_description = (this->oper == ast_pre_inc)
  1274. ? "pre-increment operation" : "pre-decrement operation";
  1275. op[0] = this->subexpressions[0]->hir(instructions, state);
  1276. op[1] = constant_one_for_inc_dec(ctx, op[0]->type);
  1277. type = arithmetic_result_type(op[0], op[1], false, state, & loc);
  1278. ir_rvalue *temp_rhs;
  1279. temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
  1280. op[0], op[1]);
  1281. result = do_assignment(instructions, state,
  1282. this->subexpressions[0]->non_lvalue_description,
  1283. op[0]->clone(ctx, NULL), temp_rhs, false,
  1284. this->subexpressions[0]->get_location());
  1285. error_emitted = op[0]->type->is_error();
  1286. break;
  1287. }
  1288. case ast_post_inc:
  1289. case ast_post_dec: {
  1290. this->non_lvalue_description = (this->oper == ast_post_inc)
  1291. ? "post-increment operation" : "post-decrement operation";
  1292. op[0] = this->subexpressions[0]->hir(instructions, state);
  1293. op[1] = constant_one_for_inc_dec(ctx, op[0]->type);
  1294. error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
  1295. type = arithmetic_result_type(op[0], op[1], false, state, & loc);
  1296. ir_rvalue *temp_rhs;
  1297. temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
  1298. op[0], op[1]);
  1299. /* Get a temporary of a copy of the lvalue before it's modified.
  1300. * This may get thrown away later.
  1301. */
  1302. result = get_lvalue_copy(instructions, op[0]->clone(ctx, NULL));
  1303. (void)do_assignment(instructions, state,
  1304. this->subexpressions[0]->non_lvalue_description,
  1305. op[0]->clone(ctx, NULL), temp_rhs, false,
  1306. this->subexpressions[0]->get_location());
  1307. error_emitted = op[0]->type->is_error();
  1308. break;
  1309. }
  1310. case ast_field_selection:
  1311. result = _mesa_ast_field_selection_to_hir(this, instructions, state);
  1312. break;
  1313. case ast_array_index: {
  1314. YYLTYPE index_loc = subexpressions[1]->get_location();
  1315. op[0] = subexpressions[0]->hir(instructions, state);
  1316. op[1] = subexpressions[1]->hir(instructions, state);
  1317. result = _mesa_ast_array_index_to_hir(ctx, state, op[0], op[1],
  1318. loc, index_loc);
  1319. if (result->type->is_error())
  1320. error_emitted = true;
  1321. break;
  1322. }
  1323. case ast_function_call:
  1324. /* Should *NEVER* get here. ast_function_call should always be handled
  1325. * by ast_function_expression::hir.
  1326. */
  1327. assert(0);
  1328. break;
  1329. case ast_identifier: {
  1330. /* ast_identifier can appear several places in a full abstract syntax
  1331. * tree. This particular use must be at location specified in the grammar
  1332. * as 'variable_identifier'.
  1333. */
  1334. ir_variable *var =
  1335. state->symbols->get_variable(this->primary_expression.identifier);
  1336. if (var != NULL) {
  1337. var->used = true;
  1338. result = new(ctx) ir_dereference_variable(var);
  1339. } else {
  1340. _mesa_glsl_error(& loc, state, "`%s' undeclared",
  1341. this->primary_expression.identifier);
  1342. result = ir_rvalue::error_value(ctx);
  1343. error_emitted = true;
  1344. }
  1345. break;
  1346. }
  1347. case ast_int_constant:
  1348. result = new(ctx) ir_constant(this->primary_expression.int_constant);
  1349. break;
  1350. case ast_uint_constant:
  1351. result = new(ctx) ir_constant(this->primary_expression.uint_constant);
  1352. break;
  1353. case ast_float_constant:
  1354. result = new(ctx) ir_constant(this->primary_expression.float_constant);
  1355. break;
  1356. case ast_bool_constant:
  1357. result = new(ctx) ir_constant(bool(this->primary_expression.bool_constant));
  1358. break;
  1359. case ast_sequence: {
  1360. /* It should not be possible to generate a sequence in the AST without
  1361. * any expressions in it.
  1362. */
  1363. assert(!this->expressions.is_empty());
  1364. /* The r-value of a sequence is the last expression in the sequence. If
  1365. * the other expressions in the sequence do not have side-effects (and
  1366. * therefore add instructions to the instruction list), they get dropped
  1367. * on the floor.
  1368. */
  1369. exec_node *previous_tail_pred = NULL;
  1370. YYLTYPE previous_operand_loc = loc;
  1371. foreach_list_typed (ast_node, ast, link, &this->expressions) {
  1372. /* If one of the operands of comma operator does not generate any
  1373. * code, we want to emit a warning. At each pass through the loop
  1374. * previous_tail_pred will point to the last instruction in the
  1375. * stream *before* processing the previous operand. Naturally,
  1376. * instructions->tail_pred will point to the last instruction in the
  1377. * stream *after* processing the previous operand. If the two
  1378. * pointers match, then the previous operand had no effect.
  1379. *
  1380. * The warning behavior here differs slightly from GCC. GCC will
  1381. * only emit a warning if none of the left-hand operands have an
  1382. * effect. However, it will emit a warning for each. I believe that
  1383. * there are some cases in C (especially with GCC extensions) where
  1384. * it is useful to have an intermediate step in a sequence have no
  1385. * effect, but I don't think these cases exist in GLSL. Either way,
  1386. * it would be a giant hassle to replicate that behavior.
  1387. */
  1388. if (previous_tail_pred == instructions->tail_pred) {
  1389. _mesa_glsl_warning(&previous_operand_loc, state,
  1390. "left-hand operand of comma expression has "
  1391. "no effect");
  1392. }
  1393. /* tail_pred is directly accessed instead of using the get_tail()
  1394. * method for performance reasons. get_tail() has extra code to
  1395. * return NULL when the list is empty. We don't care about that
  1396. * here, so using tail_pred directly is fine.
  1397. */
  1398. previous_tail_pred = instructions->tail_pred;
  1399. previous_operand_loc = ast->get_location();
  1400. result = ast->hir(instructions, state);
  1401. }
  1402. /* Any errors should have already been emitted in the loop above.
  1403. */
  1404. error_emitted = true;
  1405. break;
  1406. }
  1407. }
  1408. type = NULL; /* use result->type, not type. */
  1409. assert(result != NULL);
  1410. if (result->type->is_error() && !error_emitted)
  1411. _mesa_glsl_error(& loc, state, "type mismatch");
  1412. return result;
  1413. }
  1414. ir_rvalue *
  1415. ast_expression_statement::hir(exec_list *instructions,
  1416. struct _mesa_glsl_parse_state *state)
  1417. {
  1418. /* It is possible to have expression statements that don't have an
  1419. * expression. This is the solitary semicolon:
  1420. *
  1421. * for (i = 0; i < 5; i++)
  1422. * ;
  1423. *
  1424. * In this case the expression will be NULL. Test for NULL and don't do
  1425. * anything in that case.
  1426. */
  1427. if (expression != NULL)
  1428. expression->hir(instructions, state);
  1429. /* Statements do not have r-values.
  1430. */
  1431. return NULL;
  1432. }
  1433. ir_rvalue *
  1434. ast_compound_statement::hir(exec_list *instructions,
  1435. struct _mesa_glsl_parse_state *state)
  1436. {
  1437. if (new_scope)
  1438. state->symbols->push_scope();
  1439. foreach_list_typed (ast_node, ast, link, &this->statements)
  1440. ast->hir(instructions, state);
  1441. if (new_scope)
  1442. state->symbols->pop_scope();
  1443. /* Compound statements do not have r-values.
  1444. */
  1445. return NULL;
  1446. }
  1447. static const glsl_type *
  1448. process_array_type(YYLTYPE *loc, const glsl_type *base, ast_node *array_size,
  1449. struct _mesa_glsl_parse_state *state)
  1450. {
  1451. unsigned length = 0;
  1452. /* From page 19 (page 25) of the GLSL 1.20 spec:
  1453. *
  1454. * "Only one-dimensional arrays may be declared."
  1455. */
  1456. if (base->is_array()) {
  1457. _mesa_glsl_error(loc, state,
  1458. "invalid array of `%s' (only one-dimensional arrays "
  1459. "may be declared)",
  1460. base->name);
  1461. return glsl_type::error_type;
  1462. }
  1463. if (array_size != NULL) {
  1464. exec_list dummy_instructions;
  1465. ir_rvalue *const ir = array_size->hir(& dummy_instructions, state);
  1466. YYLTYPE loc = array_size->get_location();
  1467. if (ir != NULL) {
  1468. if (!ir->type->is_integer()) {
  1469. _mesa_glsl_error(& loc, state, "array size must be integer type");
  1470. } else if (!ir->type->is_scalar()) {
  1471. _mesa_glsl_error(& loc, state, "array size must be scalar type");
  1472. } else {
  1473. ir_constant *const size = ir->constant_expression_value();
  1474. if (size == NULL) {
  1475. _mesa_glsl_error(& loc, state, "array size must be a "
  1476. "constant valued expression");
  1477. } else if (size->value.i[0] <= 0) {
  1478. _mesa_glsl_error(& loc, state, "array size must be > 0");
  1479. } else {
  1480. assert(size->type == ir->type);
  1481. length = size->value.u[0];
  1482. /* If the array size is const (and we've verified that
  1483. * it is) then no instructions should have been emitted
  1484. * when we converted it to HIR. If they were emitted,
  1485. * then either the array size isn't const after all, or
  1486. * we are emitting unnecessary instructions.
  1487. */
  1488. assert(dummy_instructions.is_empty());
  1489. }
  1490. }
  1491. }
  1492. } else if (state->es_shader) {
  1493. /* Section 10.17 of the GLSL ES 1.00 specification states that unsized
  1494. * array declarations have been removed from the language.
  1495. */
  1496. _mesa_glsl_error(loc, state, "unsized array declarations are not "
  1497. "allowed in GLSL ES 1.00.");
  1498. }
  1499. return glsl_type::get_array_instance(base, length);
  1500. }
  1501. const glsl_type *
  1502. ast_type_specifier::glsl_type(const char **name,
  1503. struct _mesa_glsl_parse_state *state) const
  1504. {
  1505. const struct glsl_type *type;
  1506. type = state->symbols->get_type(this->type_name);
  1507. *name = this->type_name;
  1508. if (this->is_array) {
  1509. YYLTYPE loc = this->get_location();
  1510. type = process_array_type(&loc, type, this->array_size, state);
  1511. }
  1512. return type;
  1513. }
  1514. /**
  1515. * Determine whether a toplevel variable declaration declares a varying. This
  1516. * function operates by examining the variable's mode and the shader target,
  1517. * so it correctly identifies linkage variables regardless of whether they are
  1518. * declared using the deprecated "varying" syntax or the new "in/out" syntax.
  1519. *
  1520. * Passing a non-toplevel variable declaration (e.g. a function parameter) to
  1521. * this function will produce undefined results.
  1522. */
  1523. static bool
  1524. is_varying_var(ir_variable *var, _mesa_glsl_parser_targets target)
  1525. {
  1526. switch (target) {
  1527. case vertex_shader:
  1528. return var->mode == ir_var_shader_out;
  1529. case fragment_shader:
  1530. return var->mode == ir_var_shader_in;
  1531. default:
  1532. return var->mode == ir_var_shader_out || var->mode == ir_var_shader_in;
  1533. }
  1534. }
  1535. /**
  1536. * Matrix layout qualifiers are only allowed on certain types
  1537. */
  1538. static void
  1539. validate_matrix_layout_for_type(struct _mesa_glsl_parse_state *state,
  1540. YYLTYPE *loc,
  1541. const glsl_type *type)
  1542. {
  1543. if (!type->is_matrix() && !type->is_record()) {
  1544. _mesa_glsl_error(loc, state,
  1545. "uniform block layout qualifiers row_major and "
  1546. "column_major can only be applied to matrix and "
  1547. "structure types");
  1548. } else if (type->is_record()) {
  1549. /* We allow 'layout(row_major)' on structure types because it's the only
  1550. * way to get row-major layouts on matrices contained in structures.
  1551. */
  1552. _mesa_glsl_warning(loc, state,
  1553. "uniform block layout qualifiers row_major and "
  1554. "column_major applied to structure types is not "
  1555. "strictly conformant and my be rejected by other "
  1556. "compilers");
  1557. }
  1558. }
  1559. static void
  1560. apply_type_qualifier_to_variable(const struct ast_type_qualifier *qual,
  1561. ir_variable *var,
  1562. struct _mesa_glsl_parse_state *state,
  1563. YYLTYPE *loc,
  1564. bool ubo_qualifiers_valid,
  1565. bool is_parameter)
  1566. {
  1567. if (qual->flags.q.invariant) {
  1568. if (var->used) {
  1569. _mesa_glsl_error(loc, state,
  1570. "variable `%s' may not be redeclared "
  1571. "`invariant' after being used",
  1572. var->name);
  1573. } else {
  1574. var->invariant = 1;
  1575. }
  1576. }
  1577. if (qual->flags.q.constant || qual->flags.q.attribute
  1578. || qual->flags.q.uniform
  1579. || (qual->flags.q.varying && (state->target == fragment_shader)))
  1580. var->read_only = 1;
  1581. if (qual->flags.q.centroid)
  1582. var->centroid = 1;
  1583. if (qual->flags.q.attribute && state->target != vertex_shader) {
  1584. var->type = glsl_type::error_type;
  1585. _mesa_glsl_error(loc, state,
  1586. "`attribute' variables may not be declared in the "
  1587. "%s shader",
  1588. _mesa_glsl_shader_target_name(state->target));
  1589. }
  1590. /* If there is no qualifier that changes the mode of the variable, leave
  1591. * the setting alone.
  1592. */
  1593. if (qual->flags.q.in && qual->flags.q.out)
  1594. var->mode = ir_var_function_inout;
  1595. else if (qual->flags.q.in)
  1596. var->mode = is_parameter ? ir_var_function_in : ir_var_shader_in;
  1597. else if (qual->flags.q.attribute
  1598. || (qual->flags.q.varying && (state->target == fragment_shader)))
  1599. var->mode = ir_var_shader_in;
  1600. else if (qual->flags.q.out)
  1601. var->mode = is_parameter ? ir_var_function_out : ir_var_shader_out;
  1602. else if (qual->flags.q.varying && (state->target == vertex_shader))
  1603. var->mode = ir_var_shader_out;
  1604. else if (qual->flags.q.uniform)
  1605. var->mode = ir_var_uniform;
  1606. if (!is_parameter && is_varying_var(var, state->target)) {
  1607. /* This variable is being used to link data between shader stages (in
  1608. * pre-glsl-1.30 parlance, it's a "varying"). Check that it has a type
  1609. * that is allowed for such purposes.
  1610. *
  1611. * From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
  1612. *
  1613. * "The varying qualifier can be used only with the data types
  1614. * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
  1615. * these."
  1616. *
  1617. * This was relaxed in GLSL version 1.30 and GLSL ES version 3.00. From
  1618. * page 31 (page 37 of the PDF) of the GLSL 1.30 spec:
  1619. *
  1620. * "Fragment inputs can only be signed and unsigned integers and
  1621. * integer vectors, float, floating-point vectors, matrices, or
  1622. * arrays of these. Structures cannot be input.
  1623. *
  1624. * Similar text exists in the section on vertex shader outputs.
  1625. *
  1626. * Similar text exists in the GLSL ES 3.00 spec, except that the GLSL ES
  1627. * 3.00 spec allows structs as well. Varying structs are also allowed
  1628. * in GLSL 1.50.
  1629. */
  1630. switch (var->type->get_scalar_type()->base_type) {
  1631. case GLSL_TYPE_FLOAT:
  1632. /* Ok in all GLSL versions */
  1633. break;
  1634. case GLSL_TYPE_UINT:
  1635. case GLSL_TYPE_INT:
  1636. if (state->is_version(130, 300))
  1637. break;
  1638. _mesa_glsl_error(loc, state,
  1639. "varying variables must be of base type float in %s",
  1640. state->get_version_string());
  1641. break;
  1642. case GLSL_TYPE_STRUCT:
  1643. if (state->is_version(150, 300))
  1644. break;
  1645. _mesa_glsl_error(loc, state,
  1646. "varying variables may not be of type struct");
  1647. break;
  1648. default:
  1649. _mesa_glsl_error(loc, state, "illegal type for a varying variable");
  1650. break;
  1651. }
  1652. }
  1653. if (state->all_invariant && (state->current_function == NULL)) {
  1654. switch (state->target) {
  1655. case vertex_shader:
  1656. if (var->mode == ir_var_shader_out)
  1657. var->invariant = true;
  1658. break;
  1659. case geometry_shader:
  1660. if ((var->mode == ir_var_shader_in)
  1661. || (var->mode == ir_var_shader_out))
  1662. var->invariant = true;
  1663. break;
  1664. case fragment_shader:
  1665. if (var->mode == ir_var_shader_in)
  1666. var->invariant = true;
  1667. break;
  1668. }
  1669. }
  1670. if (qual->flags.q.flat)
  1671. var->interpolation = INTERP_QUALIFIER_FLAT;
  1672. else if (qual->flags.q.noperspective)
  1673. var->interpolation = INTERP_QUALIFIER_NOPERSPECTIVE;
  1674. else if (qual->flags.q.smooth)
  1675. var->interpolation = INTERP_QUALIFIER_SMOOTH;
  1676. else
  1677. var->interpolation = INTERP_QUALIFIER_NONE;
  1678. if (var->interpolation != INTERP_QUALIFIER_NONE &&
  1679. !(state->target == vertex_shader && var->mode == ir_var_shader_out) &&
  1680. !(state->target == fragment_shader && var->mode == ir_var_shader_in)) {
  1681. _mesa_glsl_error(loc, state,
  1682. "interpolation qualifier `%s' can only be applied to "
  1683. "vertex shader outputs and fragment shader inputs.",
  1684. var->interpolation_string());
  1685. }
  1686. var->pixel_center_integer = qual->flags.q.pixel_center_integer;
  1687. var->origin_upper_left = qual->flags.q.origin_upper_left;
  1688. if ((qual->flags.q.origin_upper_left || qual->flags.q.pixel_center_integer)
  1689. && (strcmp(var->name, "gl_FragCoord") != 0)) {
  1690. const char *const qual_string = (qual->flags.q.origin_upper_left)
  1691. ? "origin_upper_left" : "pixel_center_integer";
  1692. _mesa_glsl_error(loc, state,
  1693. "layout qualifier `%s' can only be applied to "
  1694. "fragment shader input `gl_FragCoord'",
  1695. qual_string);
  1696. }
  1697. if (qual->flags.q.explicit_location) {
  1698. const bool global_scope = (state->current_function == NULL);
  1699. bool fail = false;
  1700. const char *string = "";
  1701. /* In the vertex shader only shader inputs can be given explicit
  1702. * locations.
  1703. *
  1704. * In the fragment shader only shader outputs can be given explicit
  1705. * locations.
  1706. */
  1707. switch (state->target) {
  1708. case vertex_shader:
  1709. if (!global_scope || (var->mode != ir_var_shader_in)) {
  1710. fail = true;
  1711. string = "input";
  1712. }
  1713. break;
  1714. case geometry_shader:
  1715. _mesa_glsl_error(loc, state,
  1716. "geometry shader variables cannot be given "
  1717. "explicit locations\n");
  1718. break;
  1719. case fragment_shader:
  1720. if (!global_scope || (var->mode != ir_var_shader_out)) {
  1721. fail = true;
  1722. string = "output";
  1723. }
  1724. break;
  1725. };
  1726. if (fail) {
  1727. _mesa_glsl_error(loc, state,
  1728. "only %s shader %s variables can be given an "
  1729. "explicit location\n",
  1730. _mesa_glsl_shader_target_name(state->target),
  1731. string);
  1732. } else {
  1733. var->explicit_location = true;
  1734. /* This bit of silliness is needed because invalid explicit locations
  1735. * are supposed to be flagged during linking. Small negative values
  1736. * biased by VERT_ATTRIB_GENERIC0 or FRAG_RESULT_DATA0 could alias
  1737. * built-in values (e.g., -16+VERT_ATTRIB_GENERIC0 = VERT_ATTRIB_POS).
  1738. * The linker needs to be able to differentiate these cases. This
  1739. * ensures that negative values stay negative.
  1740. */
  1741. if (qual->location >= 0) {
  1742. var->location = (state->target == vertex_shader)
  1743. ? (qual->location + VERT_ATTRIB_GENERIC0)
  1744. : (qual->location + FRAG_RESULT_DATA0);
  1745. } else {
  1746. var->location = qual->location;
  1747. }
  1748. if (qual->flags.q.explicit_index) {
  1749. /* From the GLSL 4.30 specification, section 4.4.2 (Output
  1750. * Layout Qualifiers):
  1751. *
  1752. * "It is also a compile-time error if a fragment shader
  1753. * sets a layout index to less than 0 or greater than 1."
  1754. *
  1755. * Older specifications don't mandate a behavior; we take
  1756. * this as a clarification and always generate the error.
  1757. */
  1758. if (qual->index < 0 || qual->index > 1) {
  1759. _mesa_glsl_error(loc, state,
  1760. "explicit index may only be 0 or 1\n");
  1761. } else {
  1762. var->explicit_index = true;
  1763. var->index = qual->index;
  1764. }
  1765. }
  1766. }
  1767. } else if (qual->flags.q.explicit_index) {
  1768. _mesa_glsl_error(loc, state,
  1769. "explicit index requires explicit location\n");
  1770. }
  1771. /* Does the declaration use the 'layout' keyword?
  1772. */
  1773. const bool uses_layout = qual->flags.q.pixel_center_integer
  1774. || qual->flags.q.origin_upper_left
  1775. || qual->flags.q.explicit_location; /* no need for index since it relies on location */
  1776. /* Does the declaration use the deprecated 'attribute' or 'varying'
  1777. * keywords?
  1778. */
  1779. const bool uses_deprecated_qualifier = qual->flags.q.attribute
  1780. || qual->flags.q.varying;
  1781. /* Is the 'layout' keyword used with parameters that allow relaxed checking.
  1782. * Many implementations of GL_ARB_fragment_coord_conventions_enable and some
  1783. * implementations (only Mesa?) GL_ARB_explicit_attrib_location_enable
  1784. * allowed the layout qualifier to be used with 'varying' and 'attribute'.
  1785. * These extensions and all following extensions that add the 'layout'
  1786. * keyword have been modified to require the use of 'in' or 'out'.
  1787. *
  1788. * The following extension do not allow the deprecated keywords:
  1789. *
  1790. * GL_AMD_conservative_depth
  1791. * GL_ARB_conservative_depth
  1792. * GL_ARB_gpu_shader5
  1793. * GL_ARB_separate_shader_objects
  1794. * GL_ARB_tesselation_shader
  1795. * GL_ARB_transform_feedback3
  1796. * GL_ARB_uniform_buffer_object
  1797. *
  1798. * It is unknown whether GL_EXT_shader_image_load_store or GL_NV_gpu_shader5
  1799. * allow layout with the deprecated keywords.
  1800. */
  1801. const bool relaxed_layout_qualifier_checking =
  1802. state->ARB_fragment_coord_conventions_enable;
  1803. if (uses_layout && uses_deprecated_qualifier) {
  1804. if (relaxed_layout_qualifier_checking) {
  1805. _mesa_glsl_warning(loc, state,
  1806. "`layout' qualifier may not be used with "
  1807. "`attribute' or `varying'");
  1808. } else {
  1809. _mesa_glsl_error(loc, state,
  1810. "`layout' qualifier may not be used with "
  1811. "`attribute' or `varying'");
  1812. }
  1813. }
  1814. /* Layout qualifiers for gl_FragDepth, which are enabled by extension
  1815. * AMD_conservative_depth.
  1816. */
  1817. int depth_layout_count = qual->flags.q.depth_any
  1818. + qual->flags.q.depth_greater
  1819. + qual->flags.q.depth_less
  1820. + qual->flags.q.depth_unchanged;
  1821. if (depth_layout_count > 0
  1822. && !state->AMD_conservative_depth_enable
  1823. && !state->ARB_conservative_depth_enable) {
  1824. _mesa_glsl_error(loc, state,
  1825. "extension GL_AMD_conservative_depth or "
  1826. "GL_ARB_conservative_depth must be enabled "
  1827. "to use depth layout qualifiers");
  1828. } else if (depth_layout_count > 0
  1829. && strcmp(var->name, "gl_FragDepth") != 0) {
  1830. _mesa_glsl_error(loc, state,
  1831. "depth layout qualifiers can be applied only to "
  1832. "gl_FragDepth");
  1833. } else if (depth_layout_count > 1
  1834. && strcmp(var->name, "gl_FragDepth") == 0) {
  1835. _mesa_glsl_error(loc, state,
  1836. "at most one depth layout qualifier can be applied to "
  1837. "gl_FragDepth");
  1838. }
  1839. if (qual->flags.q.depth_any)
  1840. var->depth_layout = ir_depth_layout_any;
  1841. else if (qual->flags.q.depth_greater)
  1842. var->depth_layout = ir_depth_layout_greater;
  1843. else if (qual->flags.q.depth_less)
  1844. var->depth_layout = ir_depth_layout_less;
  1845. else if (qual->flags.q.depth_unchanged)
  1846. var->depth_layout = ir_depth_layout_unchanged;
  1847. else
  1848. var->depth_layout = ir_depth_layout_none;
  1849. if (qual->flags.q.std140 ||
  1850. qual->flags.q.packed ||
  1851. qual->flags.q.shared) {
  1852. _mesa_glsl_error(loc, state,
  1853. "uniform block layout qualifiers std140, packed, and "
  1854. "shared can only be applied to uniform blocks, not "
  1855. "members");
  1856. }
  1857. if (qual->flags.q.row_major || qual->flags.q.column_major) {
  1858. if (!ubo_qualifiers_valid) {
  1859. _mesa_glsl_error(loc, state,
  1860. "uniform block layout qualifiers row_major and "
  1861. "column_major can only be applied to uniform block "
  1862. "members");
  1863. } else
  1864. validate_matrix_layout_for_type(state, loc, var->type);
  1865. }
  1866. }
  1867. /**
  1868. * Get the variable that is being redeclared by this declaration
  1869. *
  1870. * Semantic checks to verify the validity of the redeclaration are also
  1871. * performed. If semantic checks fail, compilation error will be emitted via
  1872. * \c _mesa_glsl_error, but a non-\c NULL pointer will still be returned.
  1873. *
  1874. * \returns
  1875. * A pointer to an existing variable in the current scope if the declaration
  1876. * is a redeclaration, \c NULL otherwise.
  1877. */
  1878. ir_variable *
  1879. get_variable_being_redeclared(ir_variable *var, ast_declaration *decl,
  1880. struct _mesa_glsl_parse_state *state)
  1881. {
  1882. /* Check if this declaration is actually a re-declaration, either to
  1883. * resize an array or add qualifiers to an existing variable.
  1884. *
  1885. * This is allowed for variables in the current scope, or when at
  1886. * global scope (for built-ins in the implicit outer scope).
  1887. */
  1888. ir_variable *earlier = state->symbols->get_variable(decl->identifier);
  1889. if (earlier == NULL ||
  1890. (state->current_function != NULL &&
  1891. !state->symbols->name_declared_this_scope(decl->identifier))) {
  1892. return NULL;
  1893. }
  1894. YYLTYPE loc = decl->get_location();
  1895. /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
  1896. *
  1897. * "It is legal to declare an array without a size and then
  1898. * later re-declare the same name as an array of the same
  1899. * type and specify a size."
  1900. */
  1901. if ((earlier->type->array_size() == 0)
  1902. && var->type->is_array()
  1903. && (var->type->element_type() == earlier->type->element_type())) {
  1904. /* FINISHME: This doesn't match the qualifiers on the two
  1905. * FINISHME: declarations. It's not 100% clear whether this is
  1906. * FINISHME: required or not.
  1907. */
  1908. const unsigned size = unsigned(var->type->array_size());
  1909. check_builtin_array_max_size(var->name, size, loc, state);
  1910. if ((size > 0) && (size <= earlier->max_array_access)) {
  1911. _mesa_glsl_error(& loc, state, "array size must be > %u due to "
  1912. "previous access",
  1913. earlier->max_array_access);
  1914. }
  1915. earlier->type = var->type;
  1916. delete var;
  1917. var = NULL;
  1918. } else if (state->ARB_fragment_coord_conventions_enable
  1919. && strcmp(var->name, "gl_FragCoord") == 0
  1920. && earlier->type == var->type
  1921. && earlier->mode == var->mode) {
  1922. /* Allow redeclaration of gl_FragCoord for ARB_fcc layout
  1923. * qualifiers.
  1924. */
  1925. earlier->origin_upper_left = var->origin_upper_left;
  1926. earlier->pixel_center_integer = var->pixel_center_integer;
  1927. /* According to section 4.3.7 of the GLSL 1.30 spec,
  1928. * the following built-in varaibles can be redeclared with an
  1929. * interpolation qualifier:
  1930. * * gl_FrontColor
  1931. * * gl_BackColor
  1932. * * gl_FrontSecondaryColor
  1933. * * gl_BackSecondaryColor
  1934. * * gl_Color
  1935. * * gl_SecondaryColor
  1936. */
  1937. } else if (state->is_version(130, 0)
  1938. && (strcmp(var->name, "gl_FrontColor") == 0
  1939. || strcmp(var->name, "gl_BackColor") == 0
  1940. || strcmp(var->name, "gl_FrontSecondaryColor") == 0
  1941. || strcmp(var->name, "gl_BackSecondaryColor") == 0
  1942. || strcmp(var->name, "gl_Color") == 0
  1943. || strcmp(var->name, "gl_SecondaryColor") == 0)
  1944. && earlier->type == var->type
  1945. && earlier->mode == var->mode) {
  1946. earlier->interpolation = var->interpolation;
  1947. /* Layout qualifiers for gl_FragDepth. */
  1948. } else if ((state->AMD_conservative_depth_enable ||
  1949. state->ARB_conservative_depth_enable)
  1950. && strcmp(var->name, "gl_FragDepth") == 0
  1951. && earlier->type == var->type
  1952. && earlier->mode == var->mode) {
  1953. /** From the AMD_conservative_depth spec:
  1954. * Within any shader, the first redeclarations of gl_FragDepth
  1955. * must appear before any use of gl_FragDepth.
  1956. */
  1957. if (earlier->used) {
  1958. _mesa_glsl_error(&loc, state,
  1959. "the first redeclaration of gl_FragDepth "
  1960. "must appear before any use of gl_FragDepth");
  1961. }
  1962. /* Prevent inconsistent redeclaration of depth layout qualifier. */
  1963. if (earlier->depth_layout != ir_depth_layout_none
  1964. && earlier->depth_layout != var->depth_layout) {
  1965. _mesa_glsl_error(&loc, state,
  1966. "gl_FragDepth: depth layout is declared here "
  1967. "as '%s, but it was previously declared as "
  1968. "'%s'",
  1969. depth_layout_string(var->depth_layout),
  1970. depth_layout_string(earlier->depth_layout));
  1971. }
  1972. earlier->depth_layout = var->depth_layout;
  1973. } else {
  1974. _mesa_glsl_error(&loc, state, "`%s' redeclared", decl->identifier);
  1975. }
  1976. return earlier;
  1977. }
  1978. /**
  1979. * Generate the IR for an initializer in a variable declaration
  1980. */
  1981. ir_rvalue *
  1982. process_initializer(ir_variable *var, ast_declaration *decl,
  1983. ast_fully_specified_type *type,
  1984. exec_list *initializer_instructions,
  1985. struct _mesa_glsl_parse_state *state)
  1986. {
  1987. ir_rvalue *result = NULL;
  1988. YYLTYPE initializer_loc = decl->initializer->get_location();
  1989. /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
  1990. *
  1991. * "All uniform variables are read-only and are initialized either
  1992. * directly by an application via API commands, or indirectly by
  1993. * OpenGL."
  1994. */
  1995. if (var->mode == ir_var_uniform) {
  1996. state->check_version(120, 0, &initializer_loc,
  1997. "cannot initialize uniforms");
  1998. }
  1999. if (var->type->is_sampler()) {
  2000. _mesa_glsl_error(& initializer_loc, state,
  2001. "cannot initialize samplers");
  2002. }
  2003. if ((var->mode == ir_var_shader_in) && (state->current_function == NULL)) {
  2004. _mesa_glsl_error(& initializer_loc, state,
  2005. "cannot initialize %s shader input / %s",
  2006. _mesa_glsl_shader_target_name(state->target),
  2007. (state->target == vertex_shader)
  2008. ? "attribute" : "varying");
  2009. }
  2010. ir_dereference *const lhs = new(state) ir_dereference_variable(var);
  2011. ir_rvalue *rhs = decl->initializer->hir(initializer_instructions,
  2012. state);
  2013. /* Calculate the constant value if this is a const or uniform
  2014. * declaration.
  2015. */
  2016. if (type->qualifier.flags.q.constant
  2017. || type->qualifier.flags.q.uniform) {
  2018. ir_rvalue *new_rhs = validate_assignment(state, var->type, rhs, true);
  2019. if (new_rhs != NULL) {
  2020. rhs = new_rhs;
  2021. ir_constant *constant_value = rhs->constant_expression_value();
  2022. if (!constant_value) {
  2023. _mesa_glsl_error(& initializer_loc, state,
  2024. "initializer of %s variable `%s' must be a "
  2025. "constant expression",
  2026. (type->qualifier.flags.q.constant)
  2027. ? "const" : "uniform",
  2028. decl->identifier);
  2029. if (var->type->is_numeric()) {
  2030. /* Reduce cascading errors. */
  2031. var->constant_value = ir_constant::zero(state, var->type);
  2032. }
  2033. } else {
  2034. rhs = constant_value;
  2035. var->constant_value = constant_value;
  2036. }
  2037. } else {
  2038. _mesa_glsl_error(&initializer_loc, state,
  2039. "initializer of type %s cannot be assigned to "
  2040. "variable of type %s",
  2041. rhs->type->name, var->type->name);
  2042. if (var->type->is_numeric()) {
  2043. /* Reduce cascading errors. */
  2044. var->constant_value = ir_constant::zero(state, var->type);
  2045. }
  2046. }
  2047. }
  2048. if (rhs && !rhs->type->is_error()) {
  2049. bool temp = var->read_only;
  2050. if (type->qualifier.flags.q.constant)
  2051. var->read_only = false;
  2052. /* Never emit code to initialize a uniform.
  2053. */
  2054. const glsl_type *initializer_type;
  2055. if (!type->qualifier.flags.q.uniform) {
  2056. result = do_assignment(initializer_instructions, state,
  2057. NULL,
  2058. lhs, rhs, true,
  2059. type->get_location());
  2060. initializer_type = result->type;
  2061. } else
  2062. initializer_type = rhs->type;
  2063. var->constant_initializer = rhs->constant_expression_value();
  2064. var->has_initializer = true;
  2065. /* If the declared variable is an unsized array, it must inherrit
  2066. * its full type from the initializer. A declaration such as
  2067. *
  2068. * uniform float a[] = float[](1.0, 2.0, 3.0, 3.0);
  2069. *
  2070. * becomes
  2071. *
  2072. * uniform float a[4] = float[](1.0, 2.0, 3.0, 3.0);
  2073. *
  2074. * The assignment generated in the if-statement (below) will also
  2075. * automatically handle this case for non-uniforms.
  2076. *
  2077. * If the declared variable is not an array, the types must
  2078. * already match exactly. As a result, the type assignment
  2079. * here can be done unconditionally. For non-uniforms the call
  2080. * to do_assignment can change the type of the initializer (via
  2081. * the implicit conversion rules). For uniforms the initializer
  2082. * must be a constant expression, and the type of that expression
  2083. * was validated above.
  2084. */
  2085. var->type = initializer_type;
  2086. var->read_only = temp;
  2087. }
  2088. return result;
  2089. }
  2090. ir_rvalue *
  2091. ast_declarator_list::hir(exec_list *instructions,
  2092. struct _mesa_glsl_parse_state *state)
  2093. {
  2094. void *ctx = state;
  2095. const struct glsl_type *decl_type;
  2096. const char *type_name = NULL;
  2097. ir_rvalue *result = NULL;
  2098. YYLTYPE loc = this->get_location();
  2099. /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec:
  2100. *
  2101. * "To ensure that a particular output variable is invariant, it is
  2102. * necessary to use the invariant qualifier. It can either be used to
  2103. * qualify a previously declared variable as being invariant
  2104. *
  2105. * invariant gl_Position; // make existing gl_Position be invariant"
  2106. *
  2107. * In these cases the parser will set the 'invariant' flag in the declarator
  2108. * list, and the type will be NULL.
  2109. */
  2110. if (this->invariant) {
  2111. assert(this->type == NULL);
  2112. if (state->current_function != NULL) {
  2113. _mesa_glsl_error(& loc, state,
  2114. "All uses of `invariant' keyword must be at global "
  2115. "scope\n");
  2116. }
  2117. foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
  2118. assert(!decl->is_array);
  2119. assert(decl->array_size == NULL);
  2120. assert(decl->initializer == NULL);
  2121. ir_variable *const earlier =
  2122. state->symbols->get_variable(decl->identifier);
  2123. if (earlier == NULL) {
  2124. _mesa_glsl_error(& loc, state,
  2125. "Undeclared variable `%s' cannot be marked "
  2126. "invariant\n", decl->identifier);
  2127. } else if ((state->target == vertex_shader)
  2128. && (earlier->mode != ir_var_shader_out)) {
  2129. _mesa_glsl_error(& loc, state,
  2130. "`%s' cannot be marked invariant, vertex shader "
  2131. "outputs only\n", decl->identifier);
  2132. } else if ((state->target == fragment_shader)
  2133. && (earlier->mode != ir_var_shader_in)) {
  2134. _mesa_glsl_error(& loc, state,
  2135. "`%s' cannot be marked invariant, fragment shader "
  2136. "inputs only\n", decl->identifier);
  2137. } else if (earlier->used) {
  2138. _mesa_glsl_error(& loc, state,
  2139. "variable `%s' may not be redeclared "
  2140. "`invariant' after being used",
  2141. earlier->name);
  2142. } else {
  2143. earlier->invariant = true;
  2144. }
  2145. }
  2146. /* Invariant redeclarations do not have r-values.
  2147. */
  2148. return NULL;
  2149. }
  2150. assert(this->type != NULL);
  2151. assert(!this->invariant);
  2152. /* The type specifier may contain a structure definition. Process that
  2153. * before any of the variable declarations.
  2154. */
  2155. (void) this->type->specifier->hir(instructions, state);
  2156. decl_type = this->type->specifier->glsl_type(& type_name, state);
  2157. if (this->declarations.is_empty()) {
  2158. /* If there is no structure involved in the program text, there are two
  2159. * possible scenarios:
  2160. *
  2161. * - The program text contained something like 'vec4;'. This is an
  2162. * empty declaration. It is valid but weird. Emit a warning.
  2163. *
  2164. * - The program text contained something like 'S;' and 'S' is not the
  2165. * name of a known structure type. This is both invalid and weird.
  2166. * Emit an error.
  2167. *
  2168. * Note that if decl_type is NULL and there is a structure involved,
  2169. * there must have been some sort of error with the structure. In this
  2170. * case we assume that an error was already generated on this line of
  2171. * code for the structure. There is no need to generate an additional,
  2172. * confusing error.
  2173. */
  2174. assert(this->type->specifier->structure == NULL || decl_type != NULL
  2175. || state->error);
  2176. if (this->type->specifier->structure == NULL) {
  2177. if (decl_type != NULL) {
  2178. _mesa_glsl_warning(&loc, state, "empty declaration");
  2179. } else {
  2180. _mesa_glsl_error(&loc, state,
  2181. "invalid type `%s' in empty declaration",
  2182. type_name);
  2183. }
  2184. }
  2185. }
  2186. foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
  2187. const struct glsl_type *var_type;
  2188. ir_variable *var;
  2189. /* FINISHME: Emit a warning if a variable declaration shadows a
  2190. * FINISHME: declaration at a higher scope.
  2191. */
  2192. if ((decl_type == NULL) || decl_type->is_void()) {
  2193. if (type_name != NULL) {
  2194. _mesa_glsl_error(& loc, state,
  2195. "invalid type `%s' in declaration of `%s'",
  2196. type_name, decl->identifier);
  2197. } else {
  2198. _mesa_glsl_error(& loc, state,
  2199. "invalid type in declaration of `%s'",
  2200. decl->identifier);
  2201. }
  2202. continue;
  2203. }
  2204. if (decl->is_array) {
  2205. var_type = process_array_type(&loc, decl_type, decl->array_size,
  2206. state);
  2207. if (var_type->is_error())
  2208. continue;
  2209. } else {
  2210. var_type = decl_type;
  2211. }
  2212. var = new(ctx) ir_variable(var_type, decl->identifier, ir_var_auto);
  2213. /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
  2214. *
  2215. * "Global variables can only use the qualifiers const,
  2216. * attribute, uni form, or varying. Only one may be
  2217. * specified.
  2218. *
  2219. * Local variables can only use the qualifier const."
  2220. *
  2221. * This is relaxed in GLSL 1.30 and GLSL ES 3.00. It is also relaxed by
  2222. * any extension that adds the 'layout' keyword.
  2223. */
  2224. if (!state->is_version(130, 300)
  2225. && !state->ARB_explicit_attrib_location_enable
  2226. && !state->ARB_fragment_coord_conventions_enable) {
  2227. if (this->type->qualifier.flags.q.out) {
  2228. _mesa_glsl_error(& loc, state,
  2229. "`out' qualifier in declaration of `%s' "
  2230. "only valid for function parameters in %s.",
  2231. decl->identifier, state->get_version_string());
  2232. }
  2233. if (this->type->qualifier.flags.q.in) {
  2234. _mesa_glsl_error(& loc, state,
  2235. "`in' qualifier in declaration of `%s' "
  2236. "only valid for function parameters in %s.",
  2237. decl->identifier, state->get_version_string());
  2238. }
  2239. /* FINISHME: Test for other invalid qualifiers. */
  2240. }
  2241. apply_type_qualifier_to_variable(& this->type->qualifier, var, state,
  2242. & loc, this->ubo_qualifiers_valid, false);
  2243. if (this->type->qualifier.flags.q.invariant) {
  2244. if ((state->target == vertex_shader) &&
  2245. var->mode != ir_var_shader_out) {
  2246. _mesa_glsl_error(& loc, state,
  2247. "`%s' cannot be marked invariant, vertex shader "
  2248. "outputs only\n", var->name);
  2249. } else if ((state->target == fragment_shader) &&
  2250. var->mode != ir_var_shader_in) {
  2251. /* FINISHME: Note that this doesn't work for invariant on
  2252. * a function signature inval
  2253. */
  2254. _mesa_glsl_error(& loc, state,
  2255. "`%s' cannot be marked invariant, fragment shader "
  2256. "inputs only\n", var->name);
  2257. }
  2258. }
  2259. if (state->current_function != NULL) {
  2260. const char *mode = NULL;
  2261. const char *extra = "";
  2262. /* There is no need to check for 'inout' here because the parser will
  2263. * only allow that in function parameter lists.
  2264. */
  2265. if (this->type->qualifier.flags.q.attribute) {
  2266. mode = "attribute";
  2267. } else if (this->type->qualifier.flags.q.uniform) {
  2268. mode = "uniform";
  2269. } else if (this->type->qualifier.flags.q.varying) {
  2270. mode = "varying";
  2271. } else if (this->type->qualifier.flags.q.in) {
  2272. mode = "in";
  2273. extra = " or in function parameter list";
  2274. } else if (this->type->qualifier.flags.q.out) {
  2275. mode = "out";
  2276. extra = " or in function parameter list";
  2277. }
  2278. if (mode) {
  2279. _mesa_glsl_error(& loc, state,
  2280. "%s variable `%s' must be declared at "
  2281. "global scope%s",
  2282. mode, var->name, extra);
  2283. }
  2284. } else if (var->mode == ir_var_shader_in) {
  2285. var->read_only = true;
  2286. if (state->target == vertex_shader) {
  2287. bool error_emitted = false;
  2288. /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
  2289. *
  2290. * "Vertex shader inputs can only be float, floating-point
  2291. * vectors, matrices, signed and unsigned integers and integer
  2292. * vectors. Vertex shader inputs can also form arrays of these
  2293. * types, but not structures."
  2294. *
  2295. * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
  2296. *
  2297. * "Vertex shader inputs can only be float, floating-point
  2298. * vectors, matrices, signed and unsigned integers and integer
  2299. * vectors. They cannot be arrays or structures."
  2300. *
  2301. * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
  2302. *
  2303. * "The attribute qualifier can be used only with float,
  2304. * floating-point vectors, and matrices. Attribute variables
  2305. * cannot be declared as arrays or structures."
  2306. *
  2307. * From page 33 (page 39 of the PDF) of the GLSL ES 3.00 spec:
  2308. *
  2309. * "Vertex shader inputs can only be float, floating-point
  2310. * vectors, matrices, signed and unsigned integers and integer
  2311. * vectors. Vertex shader inputs cannot be arrays or
  2312. * structures."
  2313. */
  2314. const glsl_type *check_type = var->type->is_array()
  2315. ? var->type->fields.array : var->type;
  2316. switch (check_type->base_type) {
  2317. case GLSL_TYPE_FLOAT:
  2318. break;
  2319. case GLSL_TYPE_UINT:
  2320. case GLSL_TYPE_INT:
  2321. if (state->is_version(120, 300))
  2322. break;
  2323. /* FALLTHROUGH */
  2324. default:
  2325. _mesa_glsl_error(& loc, state,
  2326. "vertex shader input / attribute cannot have "
  2327. "type %s`%s'",
  2328. var->type->is_array() ? "array of " : "",
  2329. check_type->name);
  2330. error_emitted = true;
  2331. }
  2332. if (!error_emitted && var->type->is_array() &&
  2333. !state->check_version(140, 0, &loc,
  2334. "vertex shader input / attribute "
  2335. "cannot have array type")) {
  2336. error_emitted = true;
  2337. }
  2338. }
  2339. }
  2340. /* Integer fragment inputs must be qualified with 'flat'. In GLSL ES,
  2341. * so must integer vertex outputs.
  2342. *
  2343. * From section 4.3.4 ("Inputs") of the GLSL 1.50 spec:
  2344. * "Fragment shader inputs that are signed or unsigned integers or
  2345. * integer vectors must be qualified with the interpolation qualifier
  2346. * flat."
  2347. *
  2348. * From section 4.3.4 ("Input Variables") of the GLSL 3.00 ES spec:
  2349. * "Fragment shader inputs that are, or contain, signed or unsigned
  2350. * integers or integer vectors must be qualified with the
  2351. * interpolation qualifier flat."
  2352. *
  2353. * From section 4.3.6 ("Output Variables") of the GLSL 3.00 ES spec:
  2354. * "Vertex shader outputs that are, or contain, signed or unsigned
  2355. * integers or integer vectors must be qualified with the
  2356. * interpolation qualifier flat."
  2357. *
  2358. * Note that prior to GLSL 1.50, this requirement applied to vertex
  2359. * outputs rather than fragment inputs. That creates problems in the
  2360. * presence of geometry shaders, so we adopt the GLSL 1.50 rule for all
  2361. * desktop GL shaders. For GLSL ES shaders, we follow the spec and
  2362. * apply the restriction to both vertex outputs and fragment inputs.
  2363. *
  2364. * Note also that the desktop GLSL specs are missing the text "or
  2365. * contain"; this is presumably an oversight, since there is no
  2366. * reasonable way to interpolate a fragment shader input that contains
  2367. * an integer.
  2368. */
  2369. if (state->is_version(130, 300) &&
  2370. var->type->contains_integer() &&
  2371. var->interpolation != INTERP_QUALIFIER_FLAT &&
  2372. ((state->target == fragment_shader && var->mode == ir_var_shader_in)
  2373. || (state->target == vertex_shader && var->mode == ir_var_shader_out
  2374. && state->es_shader))) {
  2375. const char *var_type = (state->target == vertex_shader) ?
  2376. "vertex output" : "fragment input";
  2377. _mesa_glsl_error(&loc, state, "If a %s is (or contains) "
  2378. "an integer, then it must be qualified with 'flat'",
  2379. var_type);
  2380. }
  2381. /* Interpolation qualifiers cannot be applied to 'centroid' and
  2382. * 'centroid varying'.
  2383. *
  2384. * From page 29 (page 35 of the PDF) of the GLSL 1.30 spec:
  2385. * "interpolation qualifiers may only precede the qualifiers in,
  2386. * centroid in, out, or centroid out in a declaration. They do not apply
  2387. * to the deprecated storage qualifiers varying or centroid varying."
  2388. *
  2389. * These deprecated storage qualifiers do not exist in GLSL ES 3.00.
  2390. */
  2391. if (state->is_version(130, 0)
  2392. && this->type->qualifier.has_interpolation()
  2393. && this->type->qualifier.flags.q.varying) {
  2394. const char *i = this->type->qualifier.interpolation_string();
  2395. assert(i != NULL);
  2396. const char *s;
  2397. if (this->type->qualifier.flags.q.centroid)
  2398. s = "centroid varying";
  2399. else
  2400. s = "varying";
  2401. _mesa_glsl_error(&loc, state,
  2402. "qualifier '%s' cannot be applied to the "
  2403. "deprecated storage qualifier '%s'", i, s);
  2404. }
  2405. /* Interpolation qualifiers can only apply to vertex shader outputs and
  2406. * fragment shader inputs.
  2407. *
  2408. * From page 29 (page 35 of the PDF) of the GLSL 1.30 spec:
  2409. * "Outputs from a vertex shader (out) and inputs to a fragment
  2410. * shader (in) can be further qualified with one or more of these
  2411. * interpolation qualifiers"
  2412. *
  2413. * From page 31 (page 37 of the PDF) of the GLSL ES 3.00 spec:
  2414. * "These interpolation qualifiers may only precede the qualifiers
  2415. * in, centroid in, out, or centroid out in a declaration. They do
  2416. * not apply to inputs into a vertex shader or outputs from a
  2417. * fragment shader."
  2418. */
  2419. if (state->is_version(130, 300)
  2420. && this->type->qualifier.has_interpolation()) {
  2421. const char *i = this->type->qualifier.interpolation_string();
  2422. assert(i != NULL);
  2423. switch (state->target) {
  2424. case vertex_shader:
  2425. if (this->type->qualifier.flags.q.in) {
  2426. _mesa_glsl_error(&loc, state,
  2427. "qualifier '%s' cannot be applied to vertex "
  2428. "shader inputs", i);
  2429. }
  2430. break;
  2431. case fragment_shader:
  2432. if (this->type->qualifier.flags.q.out) {
  2433. _mesa_glsl_error(&loc, state,
  2434. "qualifier '%s' cannot be applied to fragment "
  2435. "shader outputs", i);
  2436. }
  2437. break;
  2438. default:
  2439. assert(0);
  2440. }
  2441. }
  2442. /* From section 4.3.4 of the GLSL 1.30 spec:
  2443. * "It is an error to use centroid in in a vertex shader."
  2444. *
  2445. * From section 4.3.4 of the GLSL ES 3.00 spec:
  2446. * "It is an error to use centroid in or interpolation qualifiers in
  2447. * a vertex shader input."
  2448. */
  2449. if (state->is_version(130, 300)
  2450. && this->type->qualifier.flags.q.centroid
  2451. && this->type->qualifier.flags.q.in
  2452. && state->target == vertex_shader) {
  2453. _mesa_glsl_error(&loc, state,
  2454. "'centroid in' cannot be used in a vertex shader");
  2455. }
  2456. /* Precision qualifiers exists only in GLSL versions 1.00 and >= 1.30.
  2457. */
  2458. if (this->type->specifier->precision != ast_precision_none) {
  2459. state->check_precision_qualifiers_allowed(&loc);
  2460. }
  2461. /* Precision qualifiers only apply to floating point and integer types.
  2462. *
  2463. * From section 4.5.2 of the GLSL 1.30 spec:
  2464. * "Any floating point or any integer declaration can have the type
  2465. * preceded by one of these precision qualifiers [...] Literal
  2466. * constants do not have precision qualifiers. Neither do Boolean
  2467. * variables.
  2468. *
  2469. * In GLSL ES, sampler types are also allowed.
  2470. *
  2471. * From page 87 of the GLSL ES spec:
  2472. * "RESOLUTION: Allow sampler types to take a precision qualifier."
  2473. */
  2474. if (this->type->specifier->precision != ast_precision_none
  2475. && !var->type->is_float()
  2476. && !var->type->is_integer()
  2477. && !(var->type->is_sampler() && state->es_shader)
  2478. && !(var->type->is_array()
  2479. && (var->type->fields.array->is_float()
  2480. || var->type->fields.array->is_integer()))) {
  2481. _mesa_glsl_error(&loc, state,
  2482. "precision qualifiers apply only to floating point"
  2483. "%s types", state->es_shader ? ", integer, and sampler"
  2484. : "and integer");
  2485. }
  2486. /* From page 17 (page 23 of the PDF) of the GLSL 1.20 spec:
  2487. *
  2488. * "[Sampler types] can only be declared as function
  2489. * parameters or uniform variables (see Section 4.3.5
  2490. * "Uniform")".
  2491. */
  2492. if (var_type->contains_sampler() &&
  2493. !this->type->qualifier.flags.q.uniform) {
  2494. _mesa_glsl_error(&loc, state, "samplers must be declared uniform");
  2495. }
  2496. /* Process the initializer and add its instructions to a temporary
  2497. * list. This list will be added to the instruction stream (below) after
  2498. * the declaration is added. This is done because in some cases (such as
  2499. * redeclarations) the declaration may not actually be added to the
  2500. * instruction stream.
  2501. */
  2502. exec_list initializer_instructions;
  2503. ir_variable *earlier = get_variable_being_redeclared(var, decl, state);
  2504. if (decl->initializer != NULL) {
  2505. result = process_initializer((earlier == NULL) ? var : earlier,
  2506. decl, this->type,
  2507. &initializer_instructions, state);
  2508. }
  2509. /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
  2510. *
  2511. * "It is an error to write to a const variable outside of
  2512. * its declaration, so they must be initialized when
  2513. * declared."
  2514. */
  2515. if (this->type->qualifier.flags.q.constant && decl->initializer == NULL) {
  2516. _mesa_glsl_error(& loc, state,
  2517. "const declaration of `%s' must be initialized",
  2518. decl->identifier);
  2519. }
  2520. /* If the declaration is not a redeclaration, there are a few additional
  2521. * semantic checks that must be applied. In addition, variable that was
  2522. * created for the declaration should be added to the IR stream.
  2523. */
  2524. if (earlier == NULL) {
  2525. /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
  2526. *
  2527. * "Identifiers starting with "gl_" are reserved for use by
  2528. * OpenGL, and may not be declared in a shader as either a
  2529. * variable or a function."
  2530. */
  2531. if (strncmp(decl->identifier, "gl_", 3) == 0)
  2532. _mesa_glsl_error(& loc, state,
  2533. "identifier `%s' uses reserved `gl_' prefix",
  2534. decl->identifier);
  2535. else if (strstr(decl->identifier, "__")) {
  2536. /* From page 14 (page 20 of the PDF) of the GLSL 1.10
  2537. * spec:
  2538. *
  2539. * "In addition, all identifiers containing two
  2540. * consecutive underscores (__) are reserved as
  2541. * possible future keywords."
  2542. */
  2543. _mesa_glsl_error(& loc, state,
  2544. "identifier `%s' uses reserved `__' string",
  2545. decl->identifier);
  2546. }
  2547. /* Add the variable to the symbol table. Note that the initializer's
  2548. * IR was already processed earlier (though it hasn't been emitted
  2549. * yet), without the variable in scope.
  2550. *
  2551. * This differs from most C-like languages, but it follows the GLSL
  2552. * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50
  2553. * spec:
  2554. *
  2555. * "Within a declaration, the scope of a name starts immediately
  2556. * after the initializer if present or immediately after the name
  2557. * being declared if not."
  2558. */
  2559. if (!state->symbols->add_variable(var)) {
  2560. YYLTYPE loc = this->get_location();
  2561. _mesa_glsl_error(&loc, state, "name `%s' already taken in the "
  2562. "current scope", decl->identifier);
  2563. continue;
  2564. }
  2565. /* Push the variable declaration to the top. It means that all the
  2566. * variable declarations will appear in a funny last-to-first order,
  2567. * but otherwise we run into trouble if a function is prototyped, a
  2568. * global var is decled, then the function is defined with usage of
  2569. * the global var. See glslparsertest's CorrectModule.frag.
  2570. */
  2571. instructions->push_head(var);
  2572. }
  2573. instructions->append_list(&initializer_instructions);
  2574. }
  2575. /* Generally, variable declarations do not have r-values. However,
  2576. * one is used for the declaration in
  2577. *
  2578. * while (bool b = some_condition()) {
  2579. * ...
  2580. * }
  2581. *
  2582. * so we return the rvalue from the last seen declaration here.
  2583. */
  2584. return result;
  2585. }
  2586. ir_rvalue *
  2587. ast_parameter_declarator::hir(exec_list *instructions,
  2588. struct _mesa_glsl_parse_state *state)
  2589. {
  2590. void *ctx = state;
  2591. const struct glsl_type *type;
  2592. const char *name = NULL;
  2593. YYLTYPE loc = this->get_location();
  2594. type = this->type->specifier->glsl_type(& name, state);
  2595. if (type == NULL) {
  2596. if (name != NULL) {
  2597. _mesa_glsl_error(& loc, state,
  2598. "invalid type `%s' in declaration of `%s'",
  2599. name, this->identifier);
  2600. } else {
  2601. _mesa_glsl_error(& loc, state,
  2602. "invalid type in declaration of `%s'",
  2603. this->identifier);
  2604. }
  2605. type = glsl_type::error_type;
  2606. }
  2607. /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
  2608. *
  2609. * "Functions that accept no input arguments need not use void in the
  2610. * argument list because prototypes (or definitions) are required and
  2611. * therefore there is no ambiguity when an empty argument list "( )" is
  2612. * declared. The idiom "(void)" as a parameter list is provided for
  2613. * convenience."
  2614. *
  2615. * Placing this check here prevents a void parameter being set up
  2616. * for a function, which avoids tripping up checks for main taking
  2617. * parameters and lookups of an unnamed symbol.
  2618. */
  2619. if (type->is_void()) {
  2620. if (this->identifier != NULL)
  2621. _mesa_glsl_error(& loc, state,
  2622. "named parameter cannot have type `void'");
  2623. is_void = true;
  2624. return NULL;
  2625. }
  2626. if (formal_parameter && (this->identifier == NULL)) {
  2627. _mesa_glsl_error(& loc, state, "formal parameter lacks a name");
  2628. return NULL;
  2629. }
  2630. /* This only handles "vec4 foo[..]". The earlier specifier->glsl_type(...)
  2631. * call already handled the "vec4[..] foo" case.
  2632. */
  2633. if (this->is_array) {
  2634. type = process_array_type(&loc, type, this->array_size, state);
  2635. }
  2636. if (!type->is_error() && type->array_size() == 0) {
  2637. _mesa_glsl_error(&loc, state, "arrays passed as parameters must have "
  2638. "a declared size.");
  2639. type = glsl_type::error_type;
  2640. }
  2641. is_void = false;
  2642. ir_variable *var = new(ctx)
  2643. ir_variable(type, this->identifier, ir_var_function_in);
  2644. /* Apply any specified qualifiers to the parameter declaration. Note that
  2645. * for function parameters the default mode is 'in'.
  2646. */
  2647. apply_type_qualifier_to_variable(& this->type->qualifier, var, state, & loc,
  2648. false, true);
  2649. /* From page 17 (page 23 of the PDF) of the GLSL 1.20 spec:
  2650. *
  2651. * "Samplers cannot be treated as l-values; hence cannot be used
  2652. * as out or inout function parameters, nor can they be assigned
  2653. * into."
  2654. */
  2655. if ((var->mode == ir_var_function_inout || var->mode == ir_var_function_out)
  2656. && type->contains_sampler()) {
  2657. _mesa_glsl_error(&loc, state, "out and inout parameters cannot contain samplers");
  2658. type = glsl_type::error_type;
  2659. }
  2660. /* From page 39 (page 45 of the PDF) of the GLSL 1.10 spec:
  2661. *
  2662. * "When calling a function, expressions that do not evaluate to
  2663. * l-values cannot be passed to parameters declared as out or inout."
  2664. *
  2665. * From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
  2666. *
  2667. * "Other binary or unary expressions, non-dereferenced arrays,
  2668. * function names, swizzles with repeated fields, and constants
  2669. * cannot be l-values."
  2670. *
  2671. * So for GLSL 1.10, passing an array as an out or inout parameter is not
  2672. * allowed. This restriction is removed in GLSL 1.20, and in GLSL ES.
  2673. */
  2674. if ((var->mode == ir_var_function_inout || var->mode == ir_var_function_out)
  2675. && type->is_array()
  2676. && !state->check_version(120, 100, &loc,
  2677. "Arrays cannot be out or inout parameters")) {
  2678. type = glsl_type::error_type;
  2679. }
  2680. instructions->push_tail(var);
  2681. /* Parameter declarations do not have r-values.
  2682. */
  2683. return NULL;
  2684. }
  2685. void
  2686. ast_parameter_declarator::parameters_to_hir(exec_list *ast_parameters,
  2687. bool formal,
  2688. exec_list *ir_parameters,
  2689. _mesa_glsl_parse_state *state)
  2690. {
  2691. ast_parameter_declarator *void_param = NULL;
  2692. unsigned count = 0;
  2693. foreach_list_typed (ast_parameter_declarator, param, link, ast_parameters) {
  2694. param->formal_parameter = formal;
  2695. param->hir(ir_parameters, state);
  2696. if (param->is_void)
  2697. void_param = param;
  2698. count++;
  2699. }
  2700. if ((void_param != NULL) && (count > 1)) {
  2701. YYLTYPE loc = void_param->get_location();
  2702. _mesa_glsl_error(& loc, state,
  2703. "`void' parameter must be only parameter");
  2704. }
  2705. }
  2706. void
  2707. emit_function(_mesa_glsl_parse_state *state, ir_function *f)
  2708. {
  2709. /* IR invariants disallow function declarations or definitions
  2710. * nested within other function definitions. But there is no
  2711. * requirement about the relative order of function declarations
  2712. * and definitions with respect to one another. So simply insert
  2713. * the new ir_function block at the end of the toplevel instruction
  2714. * list.
  2715. */
  2716. state->toplevel_ir->push_tail(f);
  2717. }
  2718. ir_rvalue *
  2719. ast_function::hir(exec_list *instructions,
  2720. struct _mesa_glsl_parse_state *state)
  2721. {
  2722. void *ctx = state;
  2723. ir_function *f = NULL;
  2724. ir_function_signature *sig = NULL;
  2725. exec_list hir_parameters;
  2726. const char *const name = identifier;
  2727. /* New functions are always added to the top-level IR instruction stream,
  2728. * so this instruction list pointer is ignored. See also emit_function
  2729. * (called below).
  2730. */
  2731. (void) instructions;
  2732. /* From page 21 (page 27 of the PDF) of the GLSL 1.20 spec,
  2733. *
  2734. * "Function declarations (prototypes) cannot occur inside of functions;
  2735. * they must be at global scope, or for the built-in functions, outside
  2736. * the global scope."
  2737. *
  2738. * From page 27 (page 33 of the PDF) of the GLSL ES 1.00.16 spec,
  2739. *
  2740. * "User defined functions may only be defined within the global scope."
  2741. *
  2742. * Note that this language does not appear in GLSL 1.10.
  2743. */
  2744. if ((state->current_function != NULL) &&
  2745. state->is_version(120, 100)) {
  2746. YYLTYPE loc = this->get_location();
  2747. _mesa_glsl_error(&loc, state,
  2748. "declaration of function `%s' not allowed within "
  2749. "function body", name);
  2750. }
  2751. /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
  2752. *
  2753. * "Identifiers starting with "gl_" are reserved for use by
  2754. * OpenGL, and may not be declared in a shader as either a
  2755. * variable or a function."
  2756. */
  2757. if (strncmp(name, "gl_", 3) == 0) {
  2758. YYLTYPE loc = this->get_location();
  2759. _mesa_glsl_error(&loc, state,
  2760. "identifier `%s' uses reserved `gl_' prefix", name);
  2761. }
  2762. /* Convert the list of function parameters to HIR now so that they can be
  2763. * used below to compare this function's signature with previously seen
  2764. * signatures for functions with the same name.
  2765. */
  2766. ast_parameter_declarator::parameters_to_hir(& this->parameters,
  2767. is_definition,
  2768. & hir_parameters, state);
  2769. const char *return_type_name;
  2770. const glsl_type *return_type =
  2771. this->return_type->specifier->glsl_type(& return_type_name, state);
  2772. if (!return_type) {
  2773. YYLTYPE loc = this->get_location();
  2774. _mesa_glsl_error(&loc, state,
  2775. "function `%s' has undeclared return type `%s'",
  2776. name, return_type_name);
  2777. return_type = glsl_type::error_type;
  2778. }
  2779. /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec:
  2780. * "No qualifier is allowed on the return type of a function."
  2781. */
  2782. if (this->return_type->has_qualifiers()) {
  2783. YYLTYPE loc = this->get_location();
  2784. _mesa_glsl_error(& loc, state,
  2785. "function `%s' return type has qualifiers", name);
  2786. }
  2787. /* From page 17 (page 23 of the PDF) of the GLSL 1.20 spec:
  2788. *
  2789. * "[Sampler types] can only be declared as function parameters
  2790. * or uniform variables (see Section 4.3.5 "Uniform")".
  2791. */
  2792. if (return_type->contains_sampler()) {
  2793. YYLTYPE loc = this->get_location();
  2794. _mesa_glsl_error(&loc, state,
  2795. "function `%s' return type can't contain a sampler",
  2796. name);
  2797. }
  2798. /* Verify that this function's signature either doesn't match a previously
  2799. * seen signature for a function with the same name, or, if a match is found,
  2800. * that the previously seen signature does not have an associated definition.
  2801. */
  2802. f = state->symbols->get_function(name);
  2803. if (f != NULL && (state->es_shader || f->has_user_signature())) {
  2804. sig = f->exact_matching_signature(&hir_parameters);
  2805. if (sig != NULL) {
  2806. const char *badvar = sig->qualifiers_match(&hir_parameters);
  2807. if (badvar != NULL) {
  2808. YYLTYPE loc = this->get_location();
  2809. _mesa_glsl_error(&loc, state, "function `%s' parameter `%s' "
  2810. "qualifiers don't match prototype", name, badvar);
  2811. }
  2812. if (sig->return_type != return_type) {
  2813. YYLTYPE loc = this->get_location();
  2814. _mesa_glsl_error(&loc, state, "function `%s' return type doesn't "
  2815. "match prototype", name);
  2816. }
  2817. if (is_definition && sig->is_defined) {
  2818. YYLTYPE loc = this->get_location();
  2819. _mesa_glsl_error(& loc, state, "function `%s' redefined", name);
  2820. }
  2821. }
  2822. } else {
  2823. f = new(ctx) ir_function(name);
  2824. if (!state->symbols->add_function(f)) {
  2825. /* This function name shadows a non-function use of the same name. */
  2826. YYLTYPE loc = this->get_location();
  2827. _mesa_glsl_error(&loc, state, "function name `%s' conflicts with "
  2828. "non-function", name);
  2829. return NULL;
  2830. }
  2831. emit_function(state, f);
  2832. }
  2833. /* Verify the return type of main() */
  2834. if (strcmp(name, "main") == 0) {
  2835. if (! return_type->is_void()) {
  2836. YYLTYPE loc = this->get_location();
  2837. _mesa_glsl_error(& loc, state, "main() must return void");
  2838. }
  2839. if (!hir_parameters.is_empty()) {
  2840. YYLTYPE loc = this->get_location();
  2841. _mesa_glsl_error(& loc, state, "main() must not take any parameters");
  2842. }
  2843. }
  2844. /* Finish storing the information about this new function in its signature.
  2845. */
  2846. if (sig == NULL) {
  2847. sig = new(ctx) ir_function_signature(return_type);
  2848. f->add_signature(sig);
  2849. }
  2850. sig->replace_parameters(&hir_parameters);
  2851. signature = sig;
  2852. /* Function declarations (prototypes) do not have r-values.
  2853. */
  2854. return NULL;
  2855. }
  2856. ir_rvalue *
  2857. ast_function_definition::hir(exec_list *instructions,
  2858. struct _mesa_glsl_parse_state *state)
  2859. {
  2860. prototype->is_definition = true;
  2861. prototype->hir(instructions, state);
  2862. ir_function_signature *signature = prototype->signature;
  2863. if (signature == NULL)
  2864. return NULL;
  2865. assert(state->current_function == NULL);
  2866. state->current_function = signature;
  2867. state->found_return = false;
  2868. /* Duplicate parameters declared in the prototype as concrete variables.
  2869. * Add these to the symbol table.
  2870. */
  2871. state->symbols->push_scope();
  2872. foreach_iter(exec_list_iterator, iter, signature->parameters) {
  2873. ir_variable *const var = ((ir_instruction *) iter.get())->as_variable();
  2874. assert(var != NULL);
  2875. /* The only way a parameter would "exist" is if two parameters have
  2876. * the same name.
  2877. */
  2878. if (state->symbols->name_declared_this_scope(var->name)) {
  2879. YYLTYPE loc = this->get_location();
  2880. _mesa_glsl_error(& loc, state, "parameter `%s' redeclared", var->name);
  2881. } else {
  2882. state->symbols->add_variable(var);
  2883. }
  2884. }
  2885. /* Convert the body of the function to HIR. */
  2886. this->body->hir(&signature->body, state);
  2887. signature->is_defined = true;
  2888. state->symbols->pop_scope();
  2889. assert(state->current_function == signature);
  2890. state->current_function = NULL;
  2891. if (!signature->return_type->is_void() && !state->found_return) {
  2892. YYLTYPE loc = this->get_location();
  2893. _mesa_glsl_error(& loc, state, "function `%s' has non-void return type "
  2894. "%s, but no return statement",
  2895. signature->function_name(),
  2896. signature->return_type->name);
  2897. }
  2898. /* Function definitions do not have r-values.
  2899. */
  2900. return NULL;
  2901. }
  2902. ir_rvalue *
  2903. ast_jump_statement::hir(exec_list *instructions,
  2904. struct _mesa_glsl_parse_state *state)
  2905. {
  2906. void *ctx = state;
  2907. switch (mode) {
  2908. case ast_return: {
  2909. ir_return *inst;
  2910. assert(state->current_function);
  2911. if (opt_return_value) {
  2912. ir_rvalue *const ret = opt_return_value->hir(instructions, state);
  2913. /* The value of the return type can be NULL if the shader says
  2914. * 'return foo();' and foo() is a function that returns void.
  2915. *
  2916. * NOTE: The GLSL spec doesn't say that this is an error. The type
  2917. * of the return value is void. If the return type of the function is
  2918. * also void, then this should compile without error. Seriously.
  2919. */
  2920. const glsl_type *const ret_type =
  2921. (ret == NULL) ? glsl_type::void_type : ret->type;
  2922. /* Implicit conversions are not allowed for return values. */
  2923. if (state->current_function->return_type != ret_type) {
  2924. YYLTYPE loc = this->get_location();
  2925. _mesa_glsl_error(& loc, state,
  2926. "`return' with wrong type %s, in function `%s' "
  2927. "returning %s",
  2928. ret_type->name,
  2929. state->current_function->function_name(),
  2930. state->current_function->return_type->name);
  2931. }
  2932. inst = new(ctx) ir_return(ret);
  2933. } else {
  2934. if (state->current_function->return_type->base_type !=
  2935. GLSL_TYPE_VOID) {
  2936. YYLTYPE loc = this->get_location();
  2937. _mesa_glsl_error(& loc, state,
  2938. "`return' with no value, in function %s returning "
  2939. "non-void",
  2940. state->current_function->function_name());
  2941. }
  2942. inst = new(ctx) ir_return;
  2943. }
  2944. state->found_return = true;
  2945. instructions->push_tail(inst);
  2946. break;
  2947. }
  2948. case ast_discard:
  2949. if (state->target != fragment_shader) {
  2950. YYLTYPE loc = this->get_location();
  2951. _mesa_glsl_error(& loc, state,
  2952. "`discard' may only appear in a fragment shader");
  2953. }
  2954. instructions->push_tail(new(ctx) ir_discard);
  2955. break;
  2956. case ast_break:
  2957. case ast_continue:
  2958. if (mode == ast_continue &&
  2959. state->loop_nesting_ast == NULL) {
  2960. YYLTYPE loc = this->get_location();
  2961. _mesa_glsl_error(& loc, state,
  2962. "continue may only appear in a loop");
  2963. } else if (mode == ast_break &&
  2964. state->loop_nesting_ast == NULL &&
  2965. state->switch_state.switch_nesting_ast == NULL) {
  2966. YYLTYPE loc = this->get_location();
  2967. _mesa_glsl_error(& loc, state,
  2968. "break may only appear in a loop or a switch");
  2969. } else {
  2970. /* For a loop, inline the for loop expression again,
  2971. * since we don't know where near the end of
  2972. * the loop body the normal copy of it
  2973. * is going to be placed.
  2974. */
  2975. if (state->loop_nesting_ast != NULL &&
  2976. mode == ast_continue &&
  2977. state->loop_nesting_ast->rest_expression) {
  2978. state->loop_nesting_ast->rest_expression->hir(instructions,
  2979. state);
  2980. }
  2981. if (state->switch_state.is_switch_innermost &&
  2982. mode == ast_break) {
  2983. /* Force break out of switch by setting is_break switch state.
  2984. */
  2985. ir_variable *const is_break_var = state->switch_state.is_break_var;
  2986. ir_dereference_variable *const deref_is_break_var =
  2987. new(ctx) ir_dereference_variable(is_break_var);
  2988. ir_constant *const true_val = new(ctx) ir_constant(true);
  2989. ir_assignment *const set_break_var =
  2990. new(ctx) ir_assignment(deref_is_break_var, true_val);
  2991. instructions->push_tail(set_break_var);
  2992. }
  2993. else {
  2994. ir_loop_jump *const jump =
  2995. new(ctx) ir_loop_jump((mode == ast_break)
  2996. ? ir_loop_jump::jump_break
  2997. : ir_loop_jump::jump_continue);
  2998. instructions->push_tail(jump);
  2999. }
  3000. }
  3001. break;
  3002. }
  3003. /* Jump instructions do not have r-values.
  3004. */
  3005. return NULL;
  3006. }
  3007. ir_rvalue *
  3008. ast_selection_statement::hir(exec_list *instructions,
  3009. struct _mesa_glsl_parse_state *state)
  3010. {
  3011. void *ctx = state;
  3012. ir_rvalue *const condition = this->condition->hir(instructions, state);
  3013. /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
  3014. *
  3015. * "Any expression whose type evaluates to a Boolean can be used as the
  3016. * conditional expression bool-expression. Vector types are not accepted
  3017. * as the expression to if."
  3018. *
  3019. * The checks are separated so that higher quality diagnostics can be
  3020. * generated for cases where both rules are violated.
  3021. */
  3022. if (!condition->type->is_boolean() || !condition->type->is_scalar()) {
  3023. YYLTYPE loc = this->condition->get_location();
  3024. _mesa_glsl_error(& loc, state, "if-statement condition must be scalar "
  3025. "boolean");
  3026. }
  3027. ir_if *const stmt = new(ctx) ir_if(condition);
  3028. if (then_statement != NULL) {
  3029. state->symbols->push_scope();
  3030. then_statement->hir(& stmt->then_instructions, state);
  3031. state->symbols->pop_scope();
  3032. }
  3033. if (else_statement != NULL) {
  3034. state->symbols->push_scope();
  3035. else_statement->hir(& stmt->else_instructions, state);
  3036. state->symbols->pop_scope();
  3037. }
  3038. instructions->push_tail(stmt);
  3039. /* if-statements do not have r-values.
  3040. */
  3041. return NULL;
  3042. }
  3043. ir_rvalue *
  3044. ast_switch_statement::hir(exec_list *instructions,
  3045. struct _mesa_glsl_parse_state *state)
  3046. {
  3047. void *ctx = state;
  3048. ir_rvalue *const test_expression =
  3049. this->test_expression->hir(instructions, state);
  3050. /* From page 66 (page 55 of the PDF) of the GLSL 1.50 spec:
  3051. *
  3052. * "The type of init-expression in a switch statement must be a
  3053. * scalar integer."
  3054. */
  3055. if (!test_expression->type->is_scalar() ||
  3056. !test_expression->type->is_integer()) {
  3057. YYLTYPE loc = this->test_expression->get_location();
  3058. _mesa_glsl_error(& loc,
  3059. state,
  3060. "switch-statement expression must be scalar "
  3061. "integer");
  3062. }
  3063. /* Track the switch-statement nesting in a stack-like manner.
  3064. */
  3065. struct glsl_switch_state saved = state->switch_state;
  3066. state->switch_state.is_switch_innermost = true;
  3067. state->switch_state.switch_nesting_ast = this;
  3068. state->switch_state.labels_ht = hash_table_ctor(0, hash_table_pointer_hash,
  3069. hash_table_pointer_compare);
  3070. state->switch_state.previous_default = NULL;
  3071. /* Initalize is_fallthru state to false.
  3072. */
  3073. ir_rvalue *const is_fallthru_val = new (ctx) ir_constant(false);
  3074. state->switch_state.is_fallthru_var =
  3075. new(ctx) ir_variable(glsl_type::bool_type,
  3076. "switch_is_fallthru_tmp",
  3077. ir_var_temporary);
  3078. instructions->push_tail(state->switch_state.is_fallthru_var);
  3079. ir_dereference_variable *deref_is_fallthru_var =
  3080. new(ctx) ir_dereference_variable(state->switch_state.is_fallthru_var);
  3081. instructions->push_tail(new(ctx) ir_assignment(deref_is_fallthru_var,
  3082. is_fallthru_val));
  3083. /* Initalize is_break state to false.
  3084. */
  3085. ir_rvalue *const is_break_val = new (ctx) ir_constant(false);
  3086. state->switch_state.is_break_var = new(ctx) ir_variable(glsl_type::bool_type,
  3087. "switch_is_break_tmp",
  3088. ir_var_temporary);
  3089. instructions->push_tail(state->switch_state.is_break_var);
  3090. ir_dereference_variable *deref_is_break_var =
  3091. new(ctx) ir_dereference_variable(state->switch_state.is_break_var);
  3092. instructions->push_tail(new(ctx) ir_assignment(deref_is_break_var,
  3093. is_break_val));
  3094. /* Cache test expression.
  3095. */
  3096. test_to_hir(instructions, state);
  3097. /* Emit code for body of switch stmt.
  3098. */
  3099. body->hir(instructions, state);
  3100. hash_table_dtor(state->switch_state.labels_ht);
  3101. state->switch_state = saved;
  3102. /* Switch statements do not have r-values. */
  3103. return NULL;
  3104. }
  3105. void
  3106. ast_switch_statement::test_to_hir(exec_list *instructions,
  3107. struct _mesa_glsl_parse_state *state)
  3108. {
  3109. void *ctx = state;
  3110. /* Cache value of test expression. */
  3111. ir_rvalue *const test_val =
  3112. test_expression->hir(instructions,
  3113. state);
  3114. state->switch_state.test_var = new(ctx) ir_variable(test_val->type,
  3115. "switch_test_tmp",
  3116. ir_var_temporary);
  3117. ir_dereference_variable *deref_test_var =
  3118. new(ctx) ir_dereference_variable(state->switch_state.test_var);
  3119. instructions->push_tail(state->switch_state.test_var);
  3120. instructions->push_tail(new(ctx) ir_assignment(deref_test_var, test_val));
  3121. }
  3122. ir_rvalue *
  3123. ast_switch_body::hir(exec_list *instructions,
  3124. struct _mesa_glsl_parse_state *state)
  3125. {
  3126. if (stmts != NULL)
  3127. stmts->hir(instructions, state);
  3128. /* Switch bodies do not have r-values. */
  3129. return NULL;
  3130. }
  3131. ir_rvalue *
  3132. ast_case_statement_list::hir(exec_list *instructions,
  3133. struct _mesa_glsl_parse_state *state)
  3134. {
  3135. foreach_list_typed (ast_case_statement, case_stmt, link, & this->cases)
  3136. case_stmt->hir(instructions, state);
  3137. /* Case statements do not have r-values. */
  3138. return NULL;
  3139. }
  3140. ir_rvalue *
  3141. ast_case_statement::hir(exec_list *instructions,
  3142. struct _mesa_glsl_parse_state *state)
  3143. {
  3144. labels->hir(instructions, state);
  3145. /* Conditionally set fallthru state based on break state. */
  3146. ir_constant *const false_val = new(state) ir_constant(false);
  3147. ir_dereference_variable *const deref_is_fallthru_var =
  3148. new(state) ir_dereference_variable(state->switch_state.is_fallthru_var);
  3149. ir_dereference_variable *const deref_is_break_var =
  3150. new(state) ir_dereference_variable(state->switch_state.is_break_var);
  3151. ir_assignment *const reset_fallthru_on_break =
  3152. new(state) ir_assignment(deref_is_fallthru_var,
  3153. false_val,
  3154. deref_is_break_var);
  3155. instructions->push_tail(reset_fallthru_on_break);
  3156. /* Guard case statements depending on fallthru state. */
  3157. ir_dereference_variable *const deref_fallthru_guard =
  3158. new(state) ir_dereference_variable(state->switch_state.is_fallthru_var);
  3159. ir_if *const test_fallthru = new(state) ir_if(deref_fallthru_guard);
  3160. foreach_list_typed (ast_node, stmt, link, & this->stmts)
  3161. stmt->hir(& test_fallthru->then_instructions, state);
  3162. instructions->push_tail(test_fallthru);
  3163. /* Case statements do not have r-values. */
  3164. return NULL;
  3165. }
  3166. ir_rvalue *
  3167. ast_case_label_list::hir(exec_list *instructions,
  3168. struct _mesa_glsl_parse_state *state)
  3169. {
  3170. foreach_list_typed (ast_case_label, label, link, & this->labels)
  3171. label->hir(instructions, state);
  3172. /* Case labels do not have r-values. */
  3173. return NULL;
  3174. }
  3175. ir_rvalue *
  3176. ast_case_label::hir(exec_list *instructions,
  3177. struct _mesa_glsl_parse_state *state)
  3178. {
  3179. void *ctx = state;
  3180. ir_dereference_variable *deref_fallthru_var =
  3181. new(ctx) ir_dereference_variable(state->switch_state.is_fallthru_var);
  3182. ir_rvalue *const true_val = new(ctx) ir_constant(true);
  3183. /* If not default case, ... */
  3184. if (this->test_value != NULL) {
  3185. /* Conditionally set fallthru state based on
  3186. * comparison of cached test expression value to case label.
  3187. */
  3188. ir_rvalue *const label_rval = this->test_value->hir(instructions, state);
  3189. ir_constant *label_const = label_rval->constant_expression_value();
  3190. if (!label_const) {
  3191. YYLTYPE loc = this->test_value->get_location();
  3192. _mesa_glsl_error(& loc, state,
  3193. "switch statement case label must be a "
  3194. "constant expression");
  3195. /* Stuff a dummy value in to allow processing to continue. */
  3196. label_const = new(ctx) ir_constant(0);
  3197. } else {
  3198. ast_expression *previous_label = (ast_expression *)
  3199. hash_table_find(state->switch_state.labels_ht,
  3200. (void *)(uintptr_t)label_const->value.u[0]);
  3201. if (previous_label) {
  3202. YYLTYPE loc = this->test_value->get_location();
  3203. _mesa_glsl_error(& loc, state,
  3204. "duplicate case value");
  3205. loc = previous_label->get_location();
  3206. _mesa_glsl_error(& loc, state,
  3207. "this is the previous case label");
  3208. } else {
  3209. hash_table_insert(state->switch_state.labels_ht,
  3210. this->test_value,
  3211. (void *)(uintptr_t)label_const->value.u[0]);
  3212. }
  3213. }
  3214. ir_dereference_variable *deref_test_var =
  3215. new(ctx) ir_dereference_variable(state->switch_state.test_var);
  3216. ir_rvalue *const test_cond = new(ctx) ir_expression(ir_binop_all_equal,
  3217. label_const,
  3218. deref_test_var);
  3219. ir_assignment *set_fallthru_on_test =
  3220. new(ctx) ir_assignment(deref_fallthru_var,
  3221. true_val,
  3222. test_cond);
  3223. instructions->push_tail(set_fallthru_on_test);
  3224. } else { /* default case */
  3225. if (state->switch_state.previous_default) {
  3226. YYLTYPE loc = this->get_location();
  3227. _mesa_glsl_error(& loc, state,
  3228. "multiple default labels in one switch");
  3229. loc = state->switch_state.previous_default->get_location();
  3230. _mesa_glsl_error(& loc, state,
  3231. "this is the first default label");
  3232. }
  3233. state->switch_state.previous_default = this;
  3234. /* Set falltrhu state. */
  3235. ir_assignment *set_fallthru =
  3236. new(ctx) ir_assignment(deref_fallthru_var, true_val);
  3237. instructions->push_tail(set_fallthru);
  3238. }
  3239. /* Case statements do not have r-values. */
  3240. return NULL;
  3241. }
  3242. void
  3243. ast_iteration_statement::condition_to_hir(ir_loop *stmt,
  3244. struct _mesa_glsl_parse_state *state)
  3245. {
  3246. void *ctx = state;
  3247. if (condition != NULL) {
  3248. ir_rvalue *const cond =
  3249. condition->hir(& stmt->body_instructions, state);
  3250. if ((cond == NULL)
  3251. || !cond->type->is_boolean() || !cond->type->is_scalar()) {
  3252. YYLTYPE loc = condition->get_location();
  3253. _mesa_glsl_error(& loc, state,
  3254. "loop condition must be scalar boolean");
  3255. } else {
  3256. /* As the first code in the loop body, generate a block that looks
  3257. * like 'if (!condition) break;' as the loop termination condition.
  3258. */
  3259. ir_rvalue *const not_cond =
  3260. new(ctx) ir_expression(ir_unop_logic_not, cond);
  3261. ir_if *const if_stmt = new(ctx) ir_if(not_cond);
  3262. ir_jump *const break_stmt =
  3263. new(ctx) ir_loop_jump(ir_loop_jump::jump_break);
  3264. if_stmt->then_instructions.push_tail(break_stmt);
  3265. stmt->body_instructions.push_tail(if_stmt);
  3266. }
  3267. }
  3268. }
  3269. ir_rvalue *
  3270. ast_iteration_statement::hir(exec_list *instructions,
  3271. struct _mesa_glsl_parse_state *state)
  3272. {
  3273. void *ctx = state;
  3274. /* For-loops and while-loops start a new scope, but do-while loops do not.
  3275. */
  3276. if (mode != ast_do_while)
  3277. state->symbols->push_scope();
  3278. if (init_statement != NULL)
  3279. init_statement->hir(instructions, state);
  3280. ir_loop *const stmt = new(ctx) ir_loop();
  3281. instructions->push_tail(stmt);
  3282. /* Track the current loop nesting. */
  3283. ast_iteration_statement *nesting_ast = state->loop_nesting_ast;
  3284. state->loop_nesting_ast = this;
  3285. /* Likewise, indicate that following code is closest to a loop,
  3286. * NOT closest to a switch.
  3287. */
  3288. bool saved_is_switch_innermost = state->switch_state.is_switch_innermost;
  3289. state->switch_state.is_switch_innermost = false;
  3290. if (mode != ast_do_while)
  3291. condition_to_hir(stmt, state);
  3292. if (body != NULL)
  3293. body->hir(& stmt->body_instructions, state);
  3294. if (rest_expression != NULL)
  3295. rest_expression->hir(& stmt->body_instructions, state);
  3296. if (mode == ast_do_while)
  3297. condition_to_hir(stmt, state);
  3298. if (mode != ast_do_while)
  3299. state->symbols->pop_scope();
  3300. /* Restore previous nesting before returning. */
  3301. state->loop_nesting_ast = nesting_ast;
  3302. state->switch_state.is_switch_innermost = saved_is_switch_innermost;
  3303. /* Loops do not have r-values.
  3304. */
  3305. return NULL;
  3306. }
  3307. /**
  3308. * Determine if the given type is valid for establishing a default precision
  3309. * qualifier.
  3310. *
  3311. * From GLSL ES 3.00 section 4.5.4 ("Default Precision Qualifiers"):
  3312. *
  3313. * "The precision statement
  3314. *
  3315. * precision precision-qualifier type;
  3316. *
  3317. * can be used to establish a default precision qualifier. The type field
  3318. * can be either int or float or any of the sampler types, and the
  3319. * precision-qualifier can be lowp, mediump, or highp."
  3320. *
  3321. * GLSL ES 1.00 has similar language. GLSL 1.30 doesn't allow precision
  3322. * qualifiers on sampler types, but this seems like an oversight (since the
  3323. * intention of including these in GLSL 1.30 is to allow compatibility with ES
  3324. * shaders). So we allow int, float, and all sampler types regardless of GLSL
  3325. * version.
  3326. */
  3327. static bool
  3328. is_valid_default_precision_type(const struct _mesa_glsl_parse_state *state,
  3329. const char *type_name)
  3330. {
  3331. const struct glsl_type *type = state->symbols->get_type(type_name);
  3332. if (type == NULL)
  3333. return false;
  3334. switch (type->base_type) {
  3335. case GLSL_TYPE_INT:
  3336. case GLSL_TYPE_FLOAT:
  3337. /* "int" and "float" are valid, but vectors and matrices are not. */
  3338. return type->vector_elements == 1 && type->matrix_columns == 1;
  3339. case GLSL_TYPE_SAMPLER:
  3340. return true;
  3341. default:
  3342. return false;
  3343. }
  3344. }
  3345. ir_rvalue *
  3346. ast_type_specifier::hir(exec_list *instructions,
  3347. struct _mesa_glsl_parse_state *state)
  3348. {
  3349. if (!this->is_precision_statement && this->structure == NULL)
  3350. return NULL;
  3351. YYLTYPE loc = this->get_location();
  3352. if (this->precision != ast_precision_none
  3353. && !state->check_precision_qualifiers_allowed(&loc)) {
  3354. return NULL;
  3355. }
  3356. if (this->precision != ast_precision_none
  3357. && this->structure != NULL) {
  3358. _mesa_glsl_error(&loc, state,
  3359. "precision qualifiers do not apply to structures");
  3360. return NULL;
  3361. }
  3362. /* If this is a precision statement, check that the type to which it is
  3363. * applied is either float or int.
  3364. *
  3365. * From section 4.5.3 of the GLSL 1.30 spec:
  3366. * "The precision statement
  3367. * precision precision-qualifier type;
  3368. * can be used to establish a default precision qualifier. The type
  3369. * field can be either int or float [...]. Any other types or
  3370. * qualifiers will result in an error.
  3371. */
  3372. if (this->is_precision_statement) {
  3373. assert(this->precision != ast_precision_none);
  3374. assert(this->structure == NULL); /* The check for structures was
  3375. * performed above. */
  3376. if (this->is_array) {
  3377. _mesa_glsl_error(&loc, state,
  3378. "default precision statements do not apply to "
  3379. "arrays");
  3380. return NULL;
  3381. }
  3382. if (!is_valid_default_precision_type(state, this->type_name)) {
  3383. _mesa_glsl_error(&loc, state,
  3384. "default precision statements apply only to types "
  3385. "float, int, and sampler types");
  3386. return NULL;
  3387. }
  3388. /* FINISHME: Translate precision statements into IR. */
  3389. return NULL;
  3390. }
  3391. if (this->structure != NULL)
  3392. return this->structure->hir(instructions, state);
  3393. return NULL;
  3394. }
  3395. /**
  3396. * Process a structure or interface block tree into an array of structure fields
  3397. *
  3398. * After parsing, where there are some syntax differnces, structures and
  3399. * interface blocks are almost identical. They are similar enough that the
  3400. * AST for each can be processed the same way into a set of
  3401. * \c glsl_struct_field to describe the members.
  3402. *
  3403. * \return
  3404. * The number of fields processed. A pointer to the array structure fields is
  3405. * stored in \c *fields_ret.
  3406. */
  3407. unsigned
  3408. ast_process_structure_or_interface_block(exec_list *instructions,
  3409. struct _mesa_glsl_parse_state *state,
  3410. exec_list *declarations,
  3411. YYLTYPE &loc,
  3412. glsl_struct_field **fields_ret,
  3413. bool is_interface,
  3414. bool block_row_major)
  3415. {
  3416. unsigned decl_count = 0;
  3417. /* Make an initial pass over the list of fields to determine how
  3418. * many there are. Each element in this list is an ast_declarator_list.
  3419. * This means that we actually need to count the number of elements in the
  3420. * 'declarations' list in each of the elements.
  3421. */
  3422. foreach_list_typed (ast_declarator_list, decl_list, link, declarations) {
  3423. foreach_list_const (decl_ptr, & decl_list->declarations) {
  3424. decl_count++;
  3425. }
  3426. }
  3427. /* Allocate storage for the fields and process the field
  3428. * declarations. As the declarations are processed, try to also convert
  3429. * the types to HIR. This ensures that structure definitions embedded in
  3430. * other structure definitions or in interface blocks are processed.
  3431. */
  3432. glsl_struct_field *const fields = ralloc_array(state, glsl_struct_field,
  3433. decl_count);
  3434. unsigned i = 0;
  3435. foreach_list_typed (ast_declarator_list, decl_list, link, declarations) {
  3436. const char *type_name;
  3437. decl_list->type->specifier->hir(instructions, state);
  3438. /* Section 10.9 of the GLSL ES 1.00 specification states that
  3439. * embedded structure definitions have been removed from the language.
  3440. */
  3441. if (state->es_shader && decl_list->type->specifier->structure != NULL) {
  3442. _mesa_glsl_error(&loc, state, "Embedded structure definitions are "
  3443. "not allowed in GLSL ES 1.00.");
  3444. }
  3445. const glsl_type *decl_type =
  3446. decl_list->type->specifier->glsl_type(& type_name, state);
  3447. foreach_list_typed (ast_declaration, decl, link,
  3448. &decl_list->declarations) {
  3449. /* From the GL_ARB_uniform_buffer_object spec:
  3450. *
  3451. * "Sampler types are not allowed inside of uniform
  3452. * blocks. All other types, arrays, and structures
  3453. * allowed for uniforms are allowed within a uniform
  3454. * block."
  3455. *
  3456. * It should be impossible for decl_type to be NULL here. Cases that
  3457. * might naturally lead to decl_type being NULL, especially for the
  3458. * is_interface case, will have resulted in compilation having
  3459. * already halted due to a syntax error.
  3460. */
  3461. const struct glsl_type *field_type =
  3462. decl_type != NULL ? decl_type : glsl_type::error_type;
  3463. if (is_interface && field_type->contains_sampler()) {
  3464. YYLTYPE loc = decl_list->get_location();
  3465. _mesa_glsl_error(&loc, state,
  3466. "Uniform in non-default uniform block contains sampler\n");
  3467. }
  3468. const struct ast_type_qualifier *const qual =
  3469. & decl_list->type->qualifier;
  3470. if (qual->flags.q.std140 ||
  3471. qual->flags.q.packed ||
  3472. qual->flags.q.shared) {
  3473. _mesa_glsl_error(&loc, state,
  3474. "uniform block layout qualifiers std140, packed, and "
  3475. "shared can only be applied to uniform blocks, not "
  3476. "members");
  3477. }
  3478. if (decl->is_array) {
  3479. field_type = process_array_type(&loc, decl_type, decl->array_size,
  3480. state);
  3481. }
  3482. fields[i].type = field_type;
  3483. fields[i].name = decl->identifier;
  3484. if (qual->flags.q.row_major || qual->flags.q.column_major) {
  3485. if (!field_type->is_matrix() && !field_type->is_record()) {
  3486. _mesa_glsl_error(&loc, state,
  3487. "uniform block layout qualifiers row_major and "
  3488. "column_major can only be applied to matrix and "
  3489. "structure types");
  3490. } else
  3491. validate_matrix_layout_for_type(state, &loc, field_type);
  3492. }
  3493. if (field_type->is_matrix() ||
  3494. (field_type->is_array() && field_type->fields.array->is_matrix())) {
  3495. fields[i].row_major = block_row_major;
  3496. if (qual->flags.q.row_major)
  3497. fields[i].row_major = true;
  3498. else if (qual->flags.q.column_major)
  3499. fields[i].row_major = false;
  3500. }
  3501. i++;
  3502. }
  3503. }
  3504. assert(i == decl_count);
  3505. *fields_ret = fields;
  3506. return decl_count;
  3507. }
  3508. ir_rvalue *
  3509. ast_struct_specifier::hir(exec_list *instructions,
  3510. struct _mesa_glsl_parse_state *state)
  3511. {
  3512. YYLTYPE loc = this->get_location();
  3513. glsl_struct_field *fields;
  3514. unsigned decl_count =
  3515. ast_process_structure_or_interface_block(instructions,
  3516. state,
  3517. &this->declarations,
  3518. loc,
  3519. &fields,
  3520. false,
  3521. false);
  3522. const glsl_type *t =
  3523. glsl_type::get_record_instance(fields, decl_count, this->name);
  3524. if (!state->symbols->add_type(name, t)) {
  3525. _mesa_glsl_error(& loc, state, "struct `%s' previously defined", name);
  3526. } else {
  3527. const glsl_type **s = reralloc(state, state->user_structures,
  3528. const glsl_type *,
  3529. state->num_user_structures + 1);
  3530. if (s != NULL) {
  3531. s[state->num_user_structures] = t;
  3532. state->user_structures = s;
  3533. state->num_user_structures++;
  3534. }
  3535. }
  3536. /* Structure type definitions do not have r-values.
  3537. */
  3538. return NULL;
  3539. }
  3540. ir_rvalue *
  3541. ast_uniform_block::hir(exec_list *instructions,
  3542. struct _mesa_glsl_parse_state *state)
  3543. {
  3544. YYLTYPE loc = this->get_location();
  3545. /* The ast_uniform_block has a list of ast_declarator_lists. We
  3546. * need to turn those into ir_variables with an association
  3547. * with this uniform block.
  3548. */
  3549. enum glsl_interface_packing packing;
  3550. if (this->layout.flags.q.shared) {
  3551. packing = GLSL_INTERFACE_PACKING_SHARED;
  3552. } else if (this->layout.flags.q.packed) {
  3553. packing = GLSL_INTERFACE_PACKING_PACKED;
  3554. } else {
  3555. /* The default layout is std140.
  3556. */
  3557. packing = GLSL_INTERFACE_PACKING_STD140;
  3558. }
  3559. bool block_row_major = this->layout.flags.q.row_major;
  3560. exec_list declared_variables;
  3561. glsl_struct_field *fields;
  3562. unsigned int num_variables =
  3563. ast_process_structure_or_interface_block(&declared_variables,
  3564. state,
  3565. &this->declarations,
  3566. loc,
  3567. &fields,
  3568. true,
  3569. block_row_major);
  3570. const glsl_type *block_type =
  3571. glsl_type::get_interface_instance(fields,
  3572. num_variables,
  3573. packing,
  3574. this->block_name);
  3575. if (!state->symbols->add_type(block_type->name, block_type)) {
  3576. YYLTYPE loc = this->get_location();
  3577. _mesa_glsl_error(&loc, state, "Uniform block name `%s' already taken in "
  3578. "the current scope.\n", this->block_name);
  3579. }
  3580. /* Since interface blocks cannot contain statements, it should be
  3581. * impossible for the block to generate any instructions.
  3582. */
  3583. assert(declared_variables.is_empty());
  3584. /* Page 39 (page 45 of the PDF) of section 4.3.7 in the GLSL ES 3.00 spec
  3585. * says:
  3586. *
  3587. * "If an instance name (instance-name) is used, then it puts all the
  3588. * members inside a scope within its own name space, accessed with the
  3589. * field selector ( . ) operator (analogously to structures)."
  3590. */
  3591. if (this->instance_name) {
  3592. ir_variable *var;
  3593. if (this->array_size != NULL) {
  3594. const glsl_type *block_array_type =
  3595. process_array_type(&loc, block_type, this->array_size, state);
  3596. var = new(state) ir_variable(block_array_type,
  3597. this->instance_name,
  3598. ir_var_uniform);
  3599. } else {
  3600. var = new(state) ir_variable(block_type,
  3601. this->instance_name,
  3602. ir_var_uniform);
  3603. }
  3604. var->interface_type = block_type;
  3605. state->symbols->add_variable(var);
  3606. instructions->push_tail(var);
  3607. } else {
  3608. /* In order to have an array size, the block must also be declared with
  3609. * an instane name.
  3610. */
  3611. assert(this->array_size == NULL);
  3612. for (unsigned i = 0; i < num_variables; i++) {
  3613. ir_variable *var =
  3614. new(state) ir_variable(fields[i].type,
  3615. ralloc_strdup(state, fields[i].name),
  3616. ir_var_uniform);
  3617. var->interface_type = block_type;
  3618. state->symbols->add_variable(var);
  3619. instructions->push_tail(var);
  3620. }
  3621. }
  3622. return NULL;
  3623. }
  3624. static void
  3625. detect_conflicting_assignments(struct _mesa_glsl_parse_state *state,
  3626. exec_list *instructions)
  3627. {
  3628. bool gl_FragColor_assigned = false;
  3629. bool gl_FragData_assigned = false;
  3630. bool user_defined_fs_output_assigned = false;
  3631. ir_variable *user_defined_fs_output = NULL;
  3632. /* It would be nice to have proper location information. */
  3633. YYLTYPE loc;
  3634. memset(&loc, 0, sizeof(loc));
  3635. foreach_list(node, instructions) {
  3636. ir_variable *var = ((ir_instruction *)node)->as_variable();
  3637. if (!var || !var->assigned)
  3638. continue;
  3639. if (strcmp(var->name, "gl_FragColor") == 0)
  3640. gl_FragColor_assigned = true;
  3641. else if (strcmp(var->name, "gl_FragData") == 0)
  3642. gl_FragData_assigned = true;
  3643. else if (strncmp(var->name, "gl_", 3) != 0) {
  3644. if (state->target == fragment_shader &&
  3645. var->mode == ir_var_shader_out) {
  3646. user_defined_fs_output_assigned = true;
  3647. user_defined_fs_output = var;
  3648. }
  3649. }
  3650. }
  3651. /* From the GLSL 1.30 spec:
  3652. *
  3653. * "If a shader statically assigns a value to gl_FragColor, it
  3654. * may not assign a value to any element of gl_FragData. If a
  3655. * shader statically writes a value to any element of
  3656. * gl_FragData, it may not assign a value to
  3657. * gl_FragColor. That is, a shader may assign values to either
  3658. * gl_FragColor or gl_FragData, but not both. Multiple shaders
  3659. * linked together must also consistently write just one of
  3660. * these variables. Similarly, if user declared output
  3661. * variables are in use (statically assigned to), then the
  3662. * built-in variables gl_FragColor and gl_FragData may not be
  3663. * assigned to. These incorrect usages all generate compile
  3664. * time errors."
  3665. */
  3666. if (gl_FragColor_assigned && gl_FragData_assigned) {
  3667. _mesa_glsl_error(&loc, state, "fragment shader writes to both "
  3668. "`gl_FragColor' and `gl_FragData'\n");
  3669. } else if (gl_FragColor_assigned && user_defined_fs_output_assigned) {
  3670. _mesa_glsl_error(&loc, state, "fragment shader writes to both "
  3671. "`gl_FragColor' and `%s'\n",
  3672. user_defined_fs_output->name);
  3673. } else if (gl_FragData_assigned && user_defined_fs_output_assigned) {
  3674. _mesa_glsl_error(&loc, state, "fragment shader writes to both "
  3675. "`gl_FragData' and `%s'\n",
  3676. user_defined_fs_output->name);
  3677. }
  3678. }