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.

ir_reader.cpp 31KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  1. /*
  2. * Copyright © 2010 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <cstdarg>
  24. extern "C" {
  25. #include <talloc.h>
  26. }
  27. #include "ir_reader.h"
  28. #include "glsl_parser_extras.h"
  29. #include "glsl_types.h"
  30. #include "s_expression.h"
  31. static void ir_read_error(_mesa_glsl_parse_state *, s_expression *,
  32. const char *fmt, ...);
  33. static const glsl_type *read_type(_mesa_glsl_parse_state *, s_expression *);
  34. static void scan_for_prototypes(_mesa_glsl_parse_state *, exec_list *,
  35. s_expression *);
  36. static ir_function *read_function(_mesa_glsl_parse_state *, s_list *,
  37. bool skip_body);
  38. static void read_function_sig(_mesa_glsl_parse_state *, ir_function *,
  39. s_list *, bool skip_body);
  40. static void read_instructions(_mesa_glsl_parse_state *, exec_list *,
  41. s_expression *, ir_loop *);
  42. static ir_instruction *read_instruction(_mesa_glsl_parse_state *,
  43. s_expression *, ir_loop *);
  44. static ir_variable *read_declaration(_mesa_glsl_parse_state *, s_list *);
  45. static ir_if *read_if(_mesa_glsl_parse_state *, s_list *, ir_loop *);
  46. static ir_loop *read_loop(_mesa_glsl_parse_state *st, s_list *list);
  47. static ir_return *read_return(_mesa_glsl_parse_state *, s_list *);
  48. static ir_rvalue *read_rvalue(_mesa_glsl_parse_state *, s_expression *);
  49. static ir_assignment *read_assignment(_mesa_glsl_parse_state *, s_list *);
  50. static ir_expression *read_expression(_mesa_glsl_parse_state *, s_list *);
  51. static ir_call *read_call(_mesa_glsl_parse_state *, s_list *);
  52. static ir_swizzle *read_swizzle(_mesa_glsl_parse_state *, s_list *);
  53. static ir_constant *read_constant(_mesa_glsl_parse_state *, s_list *);
  54. static ir_texture *read_texture(_mesa_glsl_parse_state *, s_list *);
  55. static ir_dereference *read_dereference(_mesa_glsl_parse_state *,
  56. s_expression *);
  57. static ir_dereference *read_var_ref(_mesa_glsl_parse_state *, s_list *);
  58. static ir_dereference *read_array_ref(_mesa_glsl_parse_state *, s_list *);
  59. static ir_dereference *read_record_ref(_mesa_glsl_parse_state *, s_list *);
  60. void
  61. _mesa_glsl_read_ir(_mesa_glsl_parse_state *state, exec_list *instructions,
  62. const char *src)
  63. {
  64. s_expression *expr = s_expression::read_expression(src);
  65. if (expr == NULL) {
  66. ir_read_error(state, NULL, "couldn't parse S-Expression.");
  67. return;
  68. }
  69. scan_for_prototypes(state, instructions, expr);
  70. if (state->error)
  71. return;
  72. read_instructions(state, instructions, expr, NULL);
  73. }
  74. static void
  75. ir_read_error(_mesa_glsl_parse_state *state, s_expression *expr,
  76. const char *fmt, ...)
  77. {
  78. va_list ap;
  79. state->error = true;
  80. state->info_log = talloc_strdup_append(state->info_log, "error: ");
  81. va_start(ap, fmt);
  82. state->info_log = talloc_vasprintf_append(state->info_log, fmt, ap);
  83. va_end(ap);
  84. state->info_log = talloc_strdup_append(state->info_log, "\n");
  85. if (expr != NULL) {
  86. state->info_log = talloc_strdup_append(state->info_log,
  87. "...in this context:\n ");
  88. expr->print();
  89. state->info_log = talloc_strdup_append(state->info_log, "\n\n");
  90. }
  91. }
  92. static const glsl_type *
  93. read_type(_mesa_glsl_parse_state *st, s_expression *expr)
  94. {
  95. s_list *list = SX_AS_LIST(expr);
  96. if (list != NULL) {
  97. s_symbol *type_sym = SX_AS_SYMBOL(list->subexpressions.get_head());
  98. if (type_sym == NULL) {
  99. ir_read_error(st, expr, "expected type (array ...) or (struct ...)");
  100. return NULL;
  101. }
  102. if (strcmp(type_sym->value(), "array") == 0) {
  103. if (list->length() != 3) {
  104. ir_read_error(st, expr, "expected type (array <type> <int>)");
  105. return NULL;
  106. }
  107. // Read base type
  108. s_expression *base_expr = (s_expression*) type_sym->next;
  109. const glsl_type *base_type = read_type(st, base_expr);
  110. if (base_type == NULL) {
  111. ir_read_error(st, NULL, "when reading base type of array");
  112. return NULL;
  113. }
  114. // Read array size
  115. s_int *size = SX_AS_INT(base_expr->next);
  116. if (size == NULL) {
  117. ir_read_error(st, expr, "found non-integer array size");
  118. return NULL;
  119. }
  120. return glsl_type::get_array_instance(base_type, size->value());
  121. } else if (strcmp(type_sym->value(), "struct") == 0) {
  122. assert(false); // FINISHME
  123. } else {
  124. ir_read_error(st, expr, "expected (array ...) or (struct ...); "
  125. "found (%s ...)", type_sym->value());
  126. return NULL;
  127. }
  128. }
  129. s_symbol *type_sym = SX_AS_SYMBOL(expr);
  130. if (type_sym == NULL) {
  131. ir_read_error(st, expr, "expected <type> (symbol or list)");
  132. return NULL;
  133. }
  134. const glsl_type *type = st->symbols->get_type(type_sym->value());
  135. if (type == NULL)
  136. ir_read_error(st, expr, "invalid type: %s", type_sym->value());
  137. return type;
  138. }
  139. static void
  140. scan_for_prototypes(_mesa_glsl_parse_state *st, exec_list *instructions,
  141. s_expression *expr)
  142. {
  143. s_list *list = SX_AS_LIST(expr);
  144. if (list == NULL) {
  145. ir_read_error(st, expr, "Expected (<instruction> ...); found an atom.");
  146. return;
  147. }
  148. foreach_iter(exec_list_iterator, it, list->subexpressions) {
  149. s_list *sub = SX_AS_LIST(it.get());
  150. if (sub == NULL)
  151. continue; // not a (function ...); ignore it.
  152. s_symbol *tag = SX_AS_SYMBOL(sub->subexpressions.get_head());
  153. if (tag == NULL || strcmp(tag->value(), "function") != 0)
  154. continue; // not a (function ...); ignore it.
  155. ir_function *f = read_function(st, sub, true);
  156. if (f == NULL)
  157. return;
  158. instructions->push_tail(f);
  159. }
  160. }
  161. static ir_function *
  162. read_function(_mesa_glsl_parse_state *st, s_list *list, bool skip_body)
  163. {
  164. if (list->length() < 3) {
  165. ir_read_error(st, list, "Expected (function <name> (signature ...) ...)");
  166. return NULL;
  167. }
  168. s_symbol *name = SX_AS_SYMBOL(list->subexpressions.head->next);
  169. if (name == NULL) {
  170. ir_read_error(st, list, "Expected (function <name> ...)");
  171. return NULL;
  172. }
  173. ir_function *f = st->symbols->get_function(name->value());
  174. if (f == NULL) {
  175. f = new ir_function(name->value());
  176. bool added = st->symbols->add_function(name->value(), f);
  177. assert(added);
  178. }
  179. exec_list_iterator it = list->subexpressions.iterator();
  180. it.next(); // skip "function" tag
  181. it.next(); // skip function name
  182. for (/* nothing */; it.has_next(); it.next()) {
  183. s_list *siglist = SX_AS_LIST(it.get());
  184. if (siglist == NULL) {
  185. ir_read_error(st, list, "Expected (function (signature ...) ...)");
  186. return NULL;
  187. }
  188. s_symbol *tag = SX_AS_SYMBOL(siglist->subexpressions.get_head());
  189. if (tag == NULL || strcmp(tag->value(), "signature") != 0) {
  190. ir_read_error(st, siglist, "Expected (signature ...)");
  191. return NULL;
  192. }
  193. read_function_sig(st, f, siglist, skip_body);
  194. }
  195. return f;
  196. }
  197. static void
  198. read_function_sig(_mesa_glsl_parse_state *st, ir_function *f, s_list *list,
  199. bool skip_body)
  200. {
  201. if (list->length() != 4) {
  202. ir_read_error(st, list, "Expected (signature <type> (parameters ...) "
  203. "(<instruction> ...))");
  204. return;
  205. }
  206. s_expression *type_expr = (s_expression*) list->subexpressions.head->next;
  207. const glsl_type *return_type = read_type(st, type_expr);
  208. if (return_type == NULL)
  209. return;
  210. s_list *paramlist = SX_AS_LIST(type_expr->next);
  211. s_list *body_list = SX_AS_LIST(paramlist->next);
  212. if (paramlist == NULL || body_list == NULL) {
  213. ir_read_error(st, list, "Expected (signature <type> (parameters ...) "
  214. "(<instruction> ...))");
  215. return;
  216. }
  217. s_symbol *paramtag = SX_AS_SYMBOL(paramlist->subexpressions.get_head());
  218. if (paramtag == NULL || strcmp(paramtag->value(), "parameters") != 0) {
  219. ir_read_error(st, paramlist, "Expected (parameters ...)");
  220. return;
  221. }
  222. // Read the parameters list into a temporary place.
  223. exec_list hir_parameters;
  224. st->symbols->push_scope();
  225. exec_list_iterator it = paramlist->subexpressions.iterator();
  226. for (it.next() /* skip "parameters" */; it.has_next(); it.next()) {
  227. s_list *decl = SX_AS_LIST(it.get());
  228. ir_variable *var = read_declaration(st, decl);
  229. if (var == NULL)
  230. return;
  231. hir_parameters.push_tail(var);
  232. }
  233. ir_function_signature *sig = f->exact_matching_signature(&hir_parameters);
  234. if (sig != NULL) {
  235. const char *badvar = sig->qualifiers_match(&hir_parameters);
  236. if (badvar != NULL) {
  237. ir_read_error(st, list, "function `%s' parameter `%s' qualifiers "
  238. "don't match prototype", f->name, badvar);
  239. return;
  240. }
  241. if (sig->return_type != return_type) {
  242. ir_read_error(st, list, "function `%s' return type doesn't "
  243. "match prototype", f->name);
  244. return;
  245. }
  246. } else {
  247. sig = new ir_function_signature(return_type);
  248. f->add_signature(sig);
  249. }
  250. sig->replace_parameters(&hir_parameters);
  251. if (!skip_body) {
  252. if (sig->is_defined) {
  253. ir_read_error(st, list, "function %s redefined", f->name);
  254. return;
  255. }
  256. read_instructions(st, &sig->body, body_list, NULL);
  257. sig->is_defined = true;
  258. }
  259. st->symbols->pop_scope();
  260. }
  261. static void
  262. read_instructions(_mesa_glsl_parse_state *st, exec_list *instructions,
  263. s_expression *expr, ir_loop *loop_ctx)
  264. {
  265. // Read in a list of instructions
  266. s_list *list = SX_AS_LIST(expr);
  267. if (list == NULL) {
  268. ir_read_error(st, expr, "Expected (<instruction> ...); found an atom.");
  269. return;
  270. }
  271. foreach_iter(exec_list_iterator, it, list->subexpressions) {
  272. s_expression *sub = (s_expression*) it.get();
  273. ir_instruction *ir = read_instruction(st, sub, loop_ctx);
  274. if (ir == NULL) {
  275. ir_read_error(st, sub, "Invalid instruction.\n");
  276. return;
  277. }
  278. instructions->push_tail(ir);
  279. }
  280. }
  281. static ir_instruction *
  282. read_instruction(_mesa_glsl_parse_state *st, s_expression *expr,
  283. ir_loop *loop_ctx)
  284. {
  285. s_symbol *symbol = SX_AS_SYMBOL(expr);
  286. if (symbol != NULL) {
  287. if (strcmp(symbol->value(), "break") == 0 && loop_ctx != NULL)
  288. return new ir_loop_jump(loop_ctx, ir_loop_jump::jump_break);
  289. if (strcmp(symbol->value(), "continue") == 0 && loop_ctx != NULL)
  290. return new ir_loop_jump(loop_ctx, ir_loop_jump::jump_continue);
  291. }
  292. s_list *list = SX_AS_LIST(expr);
  293. if (list == NULL || list->subexpressions.is_empty())
  294. return NULL;
  295. s_symbol *tag = SX_AS_SYMBOL(list->subexpressions.get_head());
  296. if (tag == NULL) {
  297. ir_read_error(st, expr, "expected instruction tag");
  298. return NULL;
  299. }
  300. ir_instruction *inst = NULL;
  301. if (strcmp(tag->value(), "declare") == 0) {
  302. inst = read_declaration(st, list);
  303. } else if (strcmp(tag->value(), "if") == 0) {
  304. inst = read_if(st, list, loop_ctx);
  305. } else if (strcmp(tag->value(), "loop") == 0) {
  306. inst = read_loop(st, list);
  307. } else if (strcmp(tag->value(), "return") == 0) {
  308. inst = read_return(st, list);
  309. } else if (strcmp(tag->value(), "function") == 0) {
  310. inst = read_function(st, list, false);
  311. } else {
  312. inst = read_rvalue(st, list);
  313. if (inst == NULL)
  314. ir_read_error(st, NULL, "when reading instruction");
  315. }
  316. return inst;
  317. }
  318. static ir_variable *
  319. read_declaration(_mesa_glsl_parse_state *st, s_list *list)
  320. {
  321. if (list->length() != 4) {
  322. ir_read_error(st, list, "expected (declare (<qualifiers>) <type> "
  323. "<name>)");
  324. return NULL;
  325. }
  326. s_list *quals = SX_AS_LIST(list->subexpressions.head->next);
  327. if (quals == NULL) {
  328. ir_read_error(st, list, "expected a list of variable qualifiers");
  329. return NULL;
  330. }
  331. s_expression *type_expr = (s_expression*) quals->next;
  332. const glsl_type *type = read_type(st, type_expr);
  333. if (type == NULL)
  334. return NULL;
  335. s_symbol *var_name = SX_AS_SYMBOL(type_expr->next);
  336. if (var_name == NULL) {
  337. ir_read_error(st, list, "expected variable name, found non-symbol");
  338. return NULL;
  339. }
  340. ir_variable *var = new ir_variable(type, var_name->value());
  341. foreach_iter(exec_list_iterator, it, quals->subexpressions) {
  342. s_symbol *qualifier = SX_AS_SYMBOL(it.get());
  343. if (qualifier == NULL) {
  344. ir_read_error(st, list, "qualifier list must contain only symbols");
  345. delete var;
  346. return NULL;
  347. }
  348. // FINISHME: Check for duplicate/conflicting qualifiers.
  349. if (strcmp(qualifier->value(), "centroid") == 0) {
  350. var->centroid = 1;
  351. } else if (strcmp(qualifier->value(), "invariant") == 0) {
  352. var->invariant = 1;
  353. } else if (strcmp(qualifier->value(), "uniform") == 0) {
  354. var->mode = ir_var_uniform;
  355. } else if (strcmp(qualifier->value(), "auto") == 0) {
  356. var->mode = ir_var_auto;
  357. } else if (strcmp(qualifier->value(), "in") == 0) {
  358. var->mode = ir_var_in;
  359. } else if (strcmp(qualifier->value(), "out") == 0) {
  360. var->mode = ir_var_out;
  361. } else if (strcmp(qualifier->value(), "inout") == 0) {
  362. var->mode = ir_var_inout;
  363. } else if (strcmp(qualifier->value(), "smooth") == 0) {
  364. var->interpolation = ir_var_smooth;
  365. } else if (strcmp(qualifier->value(), "flat") == 0) {
  366. var->interpolation = ir_var_flat;
  367. } else if (strcmp(qualifier->value(), "noperspective") == 0) {
  368. var->interpolation = ir_var_noperspective;
  369. } else {
  370. ir_read_error(st, list, "unknown qualifier: %s", qualifier->value());
  371. delete var;
  372. return NULL;
  373. }
  374. }
  375. // Add the variable to the symbol table
  376. st->symbols->add_variable(var_name->value(), var);
  377. return var;
  378. }
  379. static ir_if *
  380. read_if(_mesa_glsl_parse_state *st, s_list *list, ir_loop *loop_ctx)
  381. {
  382. if (list->length() != 4) {
  383. ir_read_error(st, list, "expected (if <condition> (<then> ...) "
  384. "(<else> ...))");
  385. return NULL;
  386. }
  387. s_expression *cond_expr = (s_expression*) list->subexpressions.head->next;
  388. ir_rvalue *condition = read_rvalue(st, cond_expr);
  389. if (condition == NULL) {
  390. ir_read_error(st, NULL, "when reading condition of (if ...)");
  391. return NULL;
  392. }
  393. s_expression *then_expr = (s_expression*) cond_expr->next;
  394. s_expression *else_expr = (s_expression*) then_expr->next;
  395. ir_if *iff = new ir_if(condition);
  396. read_instructions(st, &iff->then_instructions, then_expr, loop_ctx);
  397. read_instructions(st, &iff->else_instructions, else_expr, loop_ctx);
  398. if (st->error) {
  399. delete iff;
  400. iff = NULL;
  401. }
  402. return iff;
  403. }
  404. static ir_loop *
  405. read_loop(_mesa_glsl_parse_state *st, s_list *list)
  406. {
  407. if (list->length() != 6) {
  408. ir_read_error(st, list, "expected (loop <counter> <from> <to> "
  409. "<increment> <body>)");
  410. return NULL;
  411. }
  412. s_expression *count_expr = (s_expression*) list->subexpressions.head->next;
  413. s_expression *from_expr = (s_expression*) count_expr->next;
  414. s_expression *to_expr = (s_expression*) from_expr->next;
  415. s_expression *inc_expr = (s_expression*) to_expr->next;
  416. s_expression *body_expr = (s_expression*) inc_expr->next;
  417. // FINISHME: actually read the count/from/to fields.
  418. ir_loop *loop = new ir_loop;
  419. read_instructions(st, &loop->body_instructions, body_expr, loop);
  420. if (st->error) {
  421. delete loop;
  422. loop = NULL;
  423. }
  424. return loop;
  425. }
  426. static ir_return *
  427. read_return(_mesa_glsl_parse_state *st, s_list *list)
  428. {
  429. if (list->length() != 2) {
  430. ir_read_error(st, list, "expected (return <rvalue>)");
  431. return NULL;
  432. }
  433. s_expression *expr = (s_expression*) list->subexpressions.head->next;
  434. ir_rvalue *retval = read_rvalue(st, expr);
  435. if (retval == NULL) {
  436. ir_read_error(st, NULL, "when reading return value");
  437. return NULL;
  438. }
  439. return new ir_return(retval);
  440. }
  441. static ir_rvalue *
  442. read_rvalue(_mesa_glsl_parse_state *st, s_expression *expr)
  443. {
  444. s_list *list = SX_AS_LIST(expr);
  445. if (list == NULL || list->subexpressions.is_empty())
  446. return NULL;
  447. s_symbol *tag = SX_AS_SYMBOL(list->subexpressions.get_head());
  448. if (tag == NULL) {
  449. ir_read_error(st, expr, "expected rvalue tag");
  450. return NULL;
  451. }
  452. ir_rvalue *rvalue = read_dereference(st, list);
  453. if (rvalue != NULL || st->error)
  454. return rvalue;
  455. else if (strcmp(tag->value(), "swiz") == 0) {
  456. rvalue = read_swizzle(st, list);
  457. } else if (strcmp(tag->value(), "assign") == 0) {
  458. rvalue = read_assignment(st, list);
  459. } else if (strcmp(tag->value(), "expression") == 0) {
  460. rvalue = read_expression(st, list);
  461. } else if (strcmp(tag->value(), "call") == 0) {
  462. rvalue = read_call(st, list);
  463. } else if (strcmp(tag->value(), "constant") == 0) {
  464. rvalue = read_constant(st, list);
  465. } else {
  466. rvalue = read_texture(st, list);
  467. if (rvalue == NULL && !st->error)
  468. ir_read_error(st, expr, "unrecognized rvalue tag: %s", tag->value());
  469. }
  470. return rvalue;
  471. }
  472. static ir_assignment *
  473. read_assignment(_mesa_glsl_parse_state *st, s_list *list)
  474. {
  475. if (list->length() != 4) {
  476. ir_read_error(st, list, "expected (assign <condition> <lhs> <rhs>)");
  477. return NULL;
  478. }
  479. s_expression *cond_expr = (s_expression*) list->subexpressions.head->next;
  480. s_expression *lhs_expr = (s_expression*) cond_expr->next;
  481. s_expression *rhs_expr = (s_expression*) lhs_expr->next;
  482. // FINISHME: Deal with "true" condition
  483. ir_rvalue *condition = read_rvalue(st, cond_expr);
  484. if (condition == NULL) {
  485. ir_read_error(st, NULL, "when reading condition of assignment");
  486. return NULL;
  487. }
  488. ir_rvalue *lhs = read_rvalue(st, lhs_expr);
  489. if (lhs == NULL) {
  490. ir_read_error(st, NULL, "when reading left-hand side of assignment");
  491. return NULL;
  492. }
  493. ir_rvalue *rhs = read_rvalue(st, rhs_expr);
  494. if (rhs == NULL) {
  495. ir_read_error(st, NULL, "when reading right-hand side of assignment");
  496. return NULL;
  497. }
  498. return new ir_assignment(lhs, rhs, condition);
  499. }
  500. static ir_call *
  501. read_call(_mesa_glsl_parse_state *st, s_list *list)
  502. {
  503. if (list->length() != 3) {
  504. ir_read_error(st, list, "expected (call <name> (<param> ...))");
  505. return NULL;
  506. }
  507. s_symbol *name = SX_AS_SYMBOL(list->subexpressions.head->next);
  508. s_list *params = SX_AS_LIST(name->next);
  509. if (name == NULL || params == NULL) {
  510. ir_read_error(st, list, "expected (call <name> (<param> ...))");
  511. return NULL;
  512. }
  513. exec_list parameters;
  514. foreach_iter(exec_list_iterator, it, params->subexpressions) {
  515. s_expression *expr = (s_expression*) it.get();
  516. ir_rvalue *param = read_rvalue(st, expr);
  517. if (param == NULL) {
  518. ir_read_error(st, list, "when reading parameter to function call");
  519. return NULL;
  520. }
  521. parameters.push_tail(param);
  522. }
  523. ir_function *f = st->symbols->get_function(name->value());
  524. if (f == NULL) {
  525. ir_read_error(st, list, "found call to undefined function %s",
  526. name->value());
  527. return NULL;
  528. }
  529. const ir_function_signature *callee = f->matching_signature(&parameters);
  530. if (callee == NULL) {
  531. ir_read_error(st, list, "couldn't find matching signature for function "
  532. "%s", name->value());
  533. return NULL;
  534. }
  535. return new ir_call(callee, &parameters);
  536. }
  537. static ir_expression *
  538. read_expression(_mesa_glsl_parse_state *st, s_list *list)
  539. {
  540. const unsigned list_length = list->length();
  541. if (list_length < 4) {
  542. ir_read_error(st, list, "expected (expression <type> <operator> "
  543. "<operand> [<operand>])");
  544. return NULL;
  545. }
  546. s_expression *type_expr = (s_expression*) list->subexpressions.head->next;
  547. const glsl_type *type = read_type(st, type_expr);
  548. if (type == NULL)
  549. return NULL;
  550. /* Read the operator */
  551. s_symbol *op_sym = SX_AS_SYMBOL(type_expr->next);
  552. if (op_sym == NULL) {
  553. ir_read_error(st, list, "expected operator, found non-symbol");
  554. return NULL;
  555. }
  556. ir_expression_operation op = ir_expression::get_operator(op_sym->value());
  557. if (op == (ir_expression_operation) -1) {
  558. ir_read_error(st, list, "invalid operator: %s", op_sym->value());
  559. return NULL;
  560. }
  561. /* Now that we know the operator, check for the right number of operands */
  562. if (ir_expression::get_num_operands(op) == 2) {
  563. if (list_length != 5) {
  564. ir_read_error(st, list, "expected (expression <type> %s <operand> "
  565. " <operand>)", op_sym->value());
  566. return NULL;
  567. }
  568. } else {
  569. if (list_length != 4) {
  570. ir_read_error(st, list, "expected (expression <type> %s <operand>)",
  571. op_sym->value());
  572. return NULL;
  573. }
  574. }
  575. s_expression *exp1 = (s_expression*) (op_sym->next);
  576. ir_rvalue *arg1 = read_rvalue(st, exp1);
  577. if (arg1 == NULL) {
  578. ir_read_error(st, NULL, "when reading first operand of %s",
  579. op_sym->value());
  580. return NULL;
  581. }
  582. ir_rvalue *arg2 = NULL;
  583. if (ir_expression::get_num_operands(op) == 2) {
  584. s_expression *exp2 = (s_expression*) (exp1->next);
  585. arg2 = read_rvalue(st, exp2);
  586. if (arg2 == NULL) {
  587. ir_read_error(st, NULL, "when reading second operand of %s",
  588. op_sym->value());
  589. return NULL;
  590. }
  591. }
  592. return new ir_expression(op, type, arg1, arg2);
  593. }
  594. static ir_swizzle *
  595. read_swizzle(_mesa_glsl_parse_state *st, s_list *list)
  596. {
  597. if (list->length() != 3) {
  598. ir_read_error(st, list, "expected (swiz <swizzle> <rvalue>)");
  599. return NULL;
  600. }
  601. s_symbol *swiz = SX_AS_SYMBOL(list->subexpressions.head->next);
  602. if (swiz == NULL) {
  603. ir_read_error(st, list, "expected a valid swizzle; found non-symbol");
  604. return NULL;
  605. }
  606. if (strlen(swiz->value()) > 4) {
  607. ir_read_error(st, list, "expected a valid swizzle; found %s",
  608. swiz->value());
  609. return NULL;
  610. }
  611. s_expression *sub = (s_expression*) swiz->next;
  612. if (sub == NULL) {
  613. ir_read_error(st, list, "expected rvalue: (swizzle %s <rvalue>)",
  614. swiz->value());
  615. return NULL;
  616. }
  617. ir_rvalue *rvalue = read_rvalue(st, sub);
  618. if (rvalue == NULL)
  619. return NULL;
  620. ir_swizzle *ir = ir_swizzle::create(rvalue, swiz->value(),
  621. rvalue->type->vector_elements);
  622. if (ir == NULL)
  623. ir_read_error(st, list, "invalid swizzle");
  624. return ir;
  625. }
  626. static ir_constant *
  627. read_constant(_mesa_glsl_parse_state *st, s_list *list)
  628. {
  629. if (list->length() != 3) {
  630. ir_read_error(st, list, "expected (constant <type> (<num> ... <num>))");
  631. return NULL;
  632. }
  633. s_expression *type_expr = (s_expression*) list->subexpressions.head->next;
  634. const glsl_type *type = read_type(st, type_expr);
  635. if (type == NULL)
  636. return NULL;
  637. s_list *values = SX_AS_LIST(type_expr->next);
  638. if (values == NULL) {
  639. ir_read_error(st, list, "expected (constant <type> (<num> ... <num>))");
  640. return NULL;
  641. }
  642. const glsl_type *const base_type = type->get_base_type();
  643. ir_constant_data data;
  644. // Read in list of values (at most 16).
  645. int k = 0;
  646. foreach_iter(exec_list_iterator, it, values->subexpressions) {
  647. if (k >= 16) {
  648. ir_read_error(st, values, "expected at most 16 numbers");
  649. return NULL;
  650. }
  651. s_expression *expr = (s_expression*) it.get();
  652. if (base_type->base_type == GLSL_TYPE_FLOAT) {
  653. s_number *value = SX_AS_NUMBER(expr);
  654. if (value == NULL) {
  655. ir_read_error(st, values, "expected numbers");
  656. return NULL;
  657. }
  658. data.f[k] = value->fvalue();
  659. } else {
  660. s_int *value = SX_AS_INT(expr);
  661. if (value == NULL) {
  662. ir_read_error(st, values, "expected integers");
  663. return NULL;
  664. }
  665. switch (base_type->base_type) {
  666. case GLSL_TYPE_UINT: {
  667. data.u[k] = value->value();
  668. break;
  669. }
  670. case GLSL_TYPE_INT: {
  671. data.i[k] = value->value();
  672. break;
  673. }
  674. case GLSL_TYPE_BOOL: {
  675. data.b[k] = value->value();
  676. break;
  677. }
  678. default:
  679. ir_read_error(st, values, "unsupported constant type");
  680. return NULL;
  681. }
  682. }
  683. ++k;
  684. }
  685. return new ir_constant(type, &data);
  686. }
  687. static ir_dereference *
  688. read_dereference(_mesa_glsl_parse_state *st, s_expression *expr)
  689. {
  690. s_list *list = SX_AS_LIST(expr);
  691. if (list == NULL || list->subexpressions.is_empty())
  692. return NULL;
  693. s_symbol *tag = SX_AS_SYMBOL(list->subexpressions.head);
  694. assert(tag != NULL);
  695. if (strcmp(tag->value(), "var_ref") == 0)
  696. return read_var_ref(st, list);
  697. if (strcmp(tag->value(), "array_ref") == 0)
  698. return read_array_ref(st, list);
  699. if (strcmp(tag->value(), "record_ref") == 0)
  700. return read_record_ref(st, list);
  701. return NULL;
  702. }
  703. static ir_dereference *
  704. read_var_ref(_mesa_glsl_parse_state *st, s_list *list)
  705. {
  706. if (list->length() != 2) {
  707. ir_read_error(st, list, "expected (var_ref <variable name>)");
  708. return NULL;
  709. }
  710. s_symbol *var_name = SX_AS_SYMBOL(list->subexpressions.head->next);
  711. if (var_name == NULL) {
  712. ir_read_error(st, list, "expected (var_ref <variable name>)");
  713. return NULL;
  714. }
  715. ir_variable *var = st->symbols->get_variable(var_name->value());
  716. if (var == NULL) {
  717. ir_read_error(st, list, "undeclared variable: %s", var_name->value());
  718. return NULL;
  719. }
  720. return new ir_dereference_variable(var);
  721. }
  722. static ir_dereference *
  723. read_array_ref(_mesa_glsl_parse_state *st, s_list *list)
  724. {
  725. if (list->length() != 3) {
  726. ir_read_error(st, list, "expected (array_ref <rvalue> <index>)");
  727. return NULL;
  728. }
  729. s_expression *subj_expr = (s_expression*) list->subexpressions.head->next;
  730. ir_rvalue *subject = read_rvalue(st, subj_expr);
  731. if (subject == NULL) {
  732. ir_read_error(st, NULL, "when reading the subject of an array_ref");
  733. return NULL;
  734. }
  735. s_expression *idx_expr = (s_expression*) subj_expr->next;
  736. ir_rvalue *idx = read_rvalue(st, idx_expr);
  737. return new ir_dereference_array(subject, idx);
  738. }
  739. static ir_dereference *
  740. read_record_ref(_mesa_glsl_parse_state *st, s_list *list)
  741. {
  742. if (list->length() != 3) {
  743. ir_read_error(st, list, "expected (record_ref <rvalue> <field>)");
  744. return NULL;
  745. }
  746. s_expression *subj_expr = (s_expression*) list->subexpressions.head->next;
  747. ir_rvalue *subject = read_rvalue(st, subj_expr);
  748. if (subject == NULL) {
  749. ir_read_error(st, NULL, "when reading the subject of a record_ref");
  750. return NULL;
  751. }
  752. s_symbol *field = SX_AS_SYMBOL(subj_expr->next);
  753. if (field == NULL) {
  754. ir_read_error(st, list, "expected (record_ref ... <field name>)");
  755. return NULL;
  756. }
  757. return new ir_dereference_record(subject, field->value());
  758. }
  759. static bool
  760. valid_texture_list_length(ir_texture_opcode op, s_list *list)
  761. {
  762. unsigned required_length = 7;
  763. if (op == ir_txf)
  764. required_length = 5;
  765. else if (op == ir_tex)
  766. required_length = 6;
  767. return list->length() == required_length;
  768. }
  769. static ir_texture *
  770. read_texture(_mesa_glsl_parse_state *st, s_list *list)
  771. {
  772. s_symbol *tag = SX_AS_SYMBOL(list->subexpressions.head);
  773. assert(tag != NULL);
  774. ir_texture_opcode op = ir_texture::get_opcode(tag->value());
  775. if (op == (ir_texture_opcode) -1)
  776. return NULL;
  777. if (!valid_texture_list_length(op, list)) {
  778. ir_read_error(st, NULL, "invalid list size in (%s ...)", tag->value());
  779. return NULL;
  780. }
  781. ir_texture *tex = new ir_texture(op);
  782. // Read sampler (must be a deref)
  783. s_expression *sampler_expr = (s_expression *) tag->next;
  784. ir_dereference *sampler = read_dereference(st, sampler_expr);
  785. if (sampler == NULL) {
  786. ir_read_error(st, NULL, "when reading sampler in (%s ...)", tag->value());
  787. return NULL;
  788. }
  789. tex->set_sampler(sampler);
  790. // Read coordinate (any rvalue)
  791. s_expression *coordinate_expr = (s_expression *) sampler_expr->next;
  792. tex->coordinate = read_rvalue(st, coordinate_expr);
  793. if (tex->coordinate == NULL) {
  794. ir_read_error(st, NULL, "when reading coordinate in (%s ...)",
  795. tag->value());
  796. return NULL;
  797. }
  798. // Read texel offset, i.e. (0 0 0)
  799. s_list *offset_list = SX_AS_LIST(coordinate_expr->next);
  800. if (offset_list == NULL || offset_list->length() != 3) {
  801. ir_read_error(st, offset_list, "expected (<int> <int> <int>)");
  802. return NULL;
  803. }
  804. s_int *offset_x = SX_AS_INT(offset_list->subexpressions.head);
  805. s_int *offset_y = SX_AS_INT(offset_x->next);
  806. s_int *offset_z = SX_AS_INT(offset_y->next);
  807. if (offset_x == NULL || offset_y == NULL || offset_z == NULL) {
  808. ir_read_error(st, offset_list, "expected (<int> <int> <int>)");
  809. return NULL;
  810. }
  811. tex->offsets[0] = offset_x->value();
  812. tex->offsets[1] = offset_y->value();
  813. tex->offsets[2] = offset_z->value();
  814. if (op == ir_txf) {
  815. s_expression *lod_expr = (s_expression *) offset_list->next;
  816. tex->lod_info.lod = read_rvalue(st, lod_expr);
  817. if (tex->lod_info.lod == NULL) {
  818. ir_read_error(st, NULL, "when reading LOD in (txf ...)");
  819. return NULL;
  820. }
  821. } else {
  822. s_expression *proj_expr = (s_expression *) offset_list->next;
  823. s_int *proj_as_int = SX_AS_INT(proj_expr);
  824. if (proj_as_int && proj_as_int->value() == 1) {
  825. tex->projector = NULL;
  826. } else {
  827. tex->projector = read_rvalue(st, proj_expr);
  828. if (tex->projector == NULL) {
  829. ir_read_error(st, NULL, "when reading projective divide in (%s ..)",
  830. tag->value());
  831. return NULL;
  832. }
  833. }
  834. s_list *shadow_list = SX_AS_LIST(proj_expr->next);
  835. if (shadow_list == NULL) {
  836. ir_read_error(st, NULL, "shadow comparitor must be a list");
  837. return NULL;
  838. }
  839. if (shadow_list->subexpressions.is_empty()) {
  840. tex->shadow_comparitor= NULL;
  841. } else {
  842. tex->shadow_comparitor = read_rvalue(st, shadow_list);
  843. if (tex->shadow_comparitor == NULL) {
  844. ir_read_error(st, NULL, "when reading shadow comparitor in (%s ..)",
  845. tag->value());
  846. return NULL;
  847. }
  848. }
  849. s_expression *lod_expr = (s_expression *) shadow_list->next;
  850. switch (op) {
  851. case ir_txb:
  852. tex->lod_info.bias = read_rvalue(st, lod_expr);
  853. if (tex->lod_info.bias == NULL) {
  854. ir_read_error(st, NULL, "when reading LOD bias in (txb ...)");
  855. return NULL;
  856. }
  857. break;
  858. case ir_txl:
  859. tex->lod_info.lod = read_rvalue(st, lod_expr);
  860. if (tex->lod_info.lod == NULL) {
  861. ir_read_error(st, NULL, "when reading LOD in (txl ...)");
  862. return NULL;
  863. }
  864. break;
  865. case ir_txd: {
  866. s_list *lod_list = SX_AS_LIST(lod_expr);
  867. if (lod_list->length() != 2) {
  868. ir_read_error(st, lod_expr, "expected (dPdx dPdy) in (txd ...)");
  869. return NULL;
  870. }
  871. s_expression *dx_expr = (s_expression *) lod_list->subexpressions.head;
  872. s_expression *dy_expr = (s_expression *) dx_expr->next;
  873. tex->lod_info.grad.dPdx = read_rvalue(st, dx_expr);
  874. if (tex->lod_info.grad.dPdx == NULL) {
  875. ir_read_error(st, NULL, "when reading dPdx in (txd ...)");
  876. return NULL;
  877. }
  878. tex->lod_info.grad.dPdy = read_rvalue(st, dy_expr);
  879. if (tex->lod_info.grad.dPdy == NULL) {
  880. ir_read_error(st, NULL, "when reading dPdy in (txd ...)");
  881. return NULL;
  882. }
  883. break;
  884. }
  885. default:
  886. // tex doesn't have any extra parameters and txf was handled earlier.
  887. break;
  888. };
  889. }
  890. return tex;
  891. }