ir_reader.cpp 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  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 "ir_reader.h"
  24. #include "glsl_parser_extras.h"
  25. #include "glsl_types.h"
  26. #include "s_expression.h"
  27. const static bool debug = false;
  28. class ir_reader {
  29. public:
  30. ir_reader(_mesa_glsl_parse_state *);
  31. void read(exec_list *instructions, const char *src, bool scan_for_protos);
  32. private:
  33. void *mem_ctx;
  34. _mesa_glsl_parse_state *state;
  35. void ir_read_error(s_expression *, const char *fmt, ...);
  36. const glsl_type *read_type(s_expression *);
  37. void scan_for_prototypes(exec_list *, s_expression *);
  38. ir_function *read_function(s_expression *, bool skip_body);
  39. void read_function_sig(ir_function *, s_expression *, bool skip_body);
  40. void read_instructions(exec_list *, s_expression *, ir_loop *);
  41. ir_instruction *read_instruction(s_expression *, ir_loop *);
  42. ir_variable *read_declaration(s_expression *);
  43. ir_if *read_if(s_expression *, ir_loop *);
  44. ir_loop *read_loop(s_expression *);
  45. ir_call *read_call(s_expression *);
  46. ir_return *read_return(s_expression *);
  47. ir_rvalue *read_rvalue(s_expression *);
  48. ir_assignment *read_assignment(s_expression *);
  49. ir_expression *read_expression(s_expression *);
  50. ir_swizzle *read_swizzle(s_expression *);
  51. ir_constant *read_constant(s_expression *);
  52. ir_texture *read_texture(s_expression *);
  53. ir_dereference *read_dereference(s_expression *);
  54. ir_dereference_variable *read_var_ref(s_expression *);
  55. };
  56. ir_reader::ir_reader(_mesa_glsl_parse_state *state) : state(state)
  57. {
  58. this->mem_ctx = state;
  59. }
  60. void
  61. _mesa_glsl_read_ir(_mesa_glsl_parse_state *state, exec_list *instructions,
  62. const char *src, bool scan_for_protos)
  63. {
  64. ir_reader r(state);
  65. r.read(instructions, src, scan_for_protos);
  66. }
  67. void
  68. ir_reader::read(exec_list *instructions, const char *src, bool scan_for_protos)
  69. {
  70. void *sx_mem_ctx = ralloc_context(NULL);
  71. s_expression *expr = s_expression::read_expression(sx_mem_ctx, src);
  72. if (expr == NULL) {
  73. ir_read_error(NULL, "couldn't parse S-Expression.");
  74. return;
  75. }
  76. if (scan_for_protos) {
  77. scan_for_prototypes(instructions, expr);
  78. if (state->error)
  79. return;
  80. }
  81. read_instructions(instructions, expr, NULL);
  82. ralloc_free(sx_mem_ctx);
  83. if (debug)
  84. validate_ir_tree(instructions);
  85. }
  86. void
  87. ir_reader::ir_read_error(s_expression *expr, const char *fmt, ...)
  88. {
  89. va_list ap;
  90. state->error = true;
  91. if (state->current_function != NULL)
  92. ralloc_asprintf_append(&state->info_log, "In function %s:\n",
  93. state->current_function->function_name());
  94. ralloc_strcat(&state->info_log, "error: ");
  95. va_start(ap, fmt);
  96. ralloc_vasprintf_append(&state->info_log, fmt, ap);
  97. va_end(ap);
  98. ralloc_strcat(&state->info_log, "\n");
  99. if (expr != NULL) {
  100. ralloc_strcat(&state->info_log, "...in this context:\n ");
  101. expr->print();
  102. ralloc_strcat(&state->info_log, "\n\n");
  103. }
  104. }
  105. const glsl_type *
  106. ir_reader::read_type(s_expression *expr)
  107. {
  108. s_expression *s_base_type;
  109. s_int *s_size;
  110. s_pattern pat[] = { "array", s_base_type, s_size };
  111. if (MATCH(expr, pat)) {
  112. const glsl_type *base_type = read_type(s_base_type);
  113. if (base_type == NULL) {
  114. ir_read_error(NULL, "when reading base type of array type");
  115. return NULL;
  116. }
  117. return glsl_type::get_array_instance(base_type, s_size->value());
  118. }
  119. s_symbol *type_sym = SX_AS_SYMBOL(expr);
  120. if (type_sym == NULL) {
  121. ir_read_error(expr, "expected <type>");
  122. return NULL;
  123. }
  124. const glsl_type *type = state->symbols->get_type(type_sym->value());
  125. if (type == NULL)
  126. ir_read_error(expr, "invalid type: %s", type_sym->value());
  127. return type;
  128. }
  129. void
  130. ir_reader::scan_for_prototypes(exec_list *instructions, s_expression *expr)
  131. {
  132. s_list *list = SX_AS_LIST(expr);
  133. if (list == NULL) {
  134. ir_read_error(expr, "Expected (<instruction> ...); found an atom.");
  135. return;
  136. }
  137. foreach_iter(exec_list_iterator, it, list->subexpressions) {
  138. s_list *sub = SX_AS_LIST(it.get());
  139. if (sub == NULL)
  140. continue; // not a (function ...); ignore it.
  141. s_symbol *tag = SX_AS_SYMBOL(sub->subexpressions.get_head());
  142. if (tag == NULL || strcmp(tag->value(), "function") != 0)
  143. continue; // not a (function ...); ignore it.
  144. ir_function *f = read_function(sub, true);
  145. if (f == NULL)
  146. return;
  147. instructions->push_tail(f);
  148. }
  149. }
  150. ir_function *
  151. ir_reader::read_function(s_expression *expr, bool skip_body)
  152. {
  153. bool added = false;
  154. s_symbol *name;
  155. s_pattern pat[] = { "function", name };
  156. if (!PARTIAL_MATCH(expr, pat)) {
  157. ir_read_error(expr, "Expected (function <name> (signature ...) ...)");
  158. return NULL;
  159. }
  160. ir_function *f = state->symbols->get_function(name->value());
  161. if (f == NULL) {
  162. f = new(mem_ctx) ir_function(name->value());
  163. added = state->symbols->add_function(f);
  164. assert(added);
  165. }
  166. exec_list_iterator it = ((s_list *) expr)->subexpressions.iterator();
  167. it.next(); // skip "function" tag
  168. it.next(); // skip function name
  169. for (/* nothing */; it.has_next(); it.next()) {
  170. s_expression *s_sig = (s_expression *) it.get();
  171. read_function_sig(f, s_sig, skip_body);
  172. }
  173. return added ? f : NULL;
  174. }
  175. void
  176. ir_reader::read_function_sig(ir_function *f, s_expression *expr, bool skip_body)
  177. {
  178. s_expression *type_expr;
  179. s_list *paramlist;
  180. s_list *body_list;
  181. s_pattern pat[] = { "signature", type_expr, paramlist, body_list };
  182. if (!MATCH(expr, pat)) {
  183. ir_read_error(expr, "Expected (signature <type> (parameters ...) "
  184. "(<instruction> ...))");
  185. return;
  186. }
  187. const glsl_type *return_type = read_type(type_expr);
  188. if (return_type == NULL)
  189. return;
  190. s_symbol *paramtag = SX_AS_SYMBOL(paramlist->subexpressions.get_head());
  191. if (paramtag == NULL || strcmp(paramtag->value(), "parameters") != 0) {
  192. ir_read_error(paramlist, "Expected (parameters ...)");
  193. return;
  194. }
  195. // Read the parameters list into a temporary place.
  196. exec_list hir_parameters;
  197. state->symbols->push_scope();
  198. exec_list_iterator it = paramlist->subexpressions.iterator();
  199. for (it.next() /* skip "parameters" */; it.has_next(); it.next()) {
  200. ir_variable *var = read_declaration((s_expression *) it.get());
  201. if (var == NULL)
  202. return;
  203. hir_parameters.push_tail(var);
  204. }
  205. ir_function_signature *sig = f->exact_matching_signature(&hir_parameters);
  206. if (sig == NULL && skip_body) {
  207. /* If scanning for prototypes, generate a new signature. */
  208. sig = new(mem_ctx) ir_function_signature(return_type, glsl_precision_undefined);
  209. sig->is_builtin = true;
  210. f->add_signature(sig);
  211. } else if (sig != NULL) {
  212. const char *badvar = sig->qualifiers_match(&hir_parameters);
  213. if (badvar != NULL) {
  214. ir_read_error(expr, "function `%s' parameter `%s' qualifiers "
  215. "don't match prototype", f->name, badvar);
  216. return;
  217. }
  218. if (sig->return_type != return_type) {
  219. ir_read_error(expr, "function `%s' return type doesn't "
  220. "match prototype", f->name);
  221. return;
  222. }
  223. } else {
  224. /* No prototype for this body exists - skip it. */
  225. state->symbols->pop_scope();
  226. return;
  227. }
  228. assert(sig != NULL);
  229. sig->replace_parameters(&hir_parameters);
  230. if (!skip_body && !body_list->subexpressions.is_empty()) {
  231. if (sig->is_defined) {
  232. ir_read_error(expr, "function %s redefined", f->name);
  233. return;
  234. }
  235. state->current_function = sig;
  236. read_instructions(&sig->body, body_list, NULL);
  237. state->current_function = NULL;
  238. sig->is_defined = true;
  239. }
  240. state->symbols->pop_scope();
  241. }
  242. void
  243. ir_reader::read_instructions(exec_list *instructions, s_expression *expr,
  244. ir_loop *loop_ctx)
  245. {
  246. // Read in a list of instructions
  247. s_list *list = SX_AS_LIST(expr);
  248. if (list == NULL) {
  249. ir_read_error(expr, "Expected (<instruction> ...); found an atom.");
  250. return;
  251. }
  252. foreach_iter(exec_list_iterator, it, list->subexpressions) {
  253. s_expression *sub = (s_expression*) it.get();
  254. ir_instruction *ir = read_instruction(sub, loop_ctx);
  255. if (ir != NULL) {
  256. /* Global variable declarations should be moved to the top, before
  257. * any functions that might use them. Functions are added to the
  258. * instruction stream when scanning for prototypes, so without this
  259. * hack, they always appear before variable declarations.
  260. */
  261. if (state->current_function == NULL && ir->as_variable() != NULL)
  262. instructions->push_head(ir);
  263. else
  264. instructions->push_tail(ir);
  265. }
  266. }
  267. }
  268. ir_instruction *
  269. ir_reader::read_instruction(s_expression *expr, ir_loop *loop_ctx)
  270. {
  271. s_symbol *symbol = SX_AS_SYMBOL(expr);
  272. if (symbol != NULL) {
  273. if (strcmp(symbol->value(), "break") == 0 && loop_ctx != NULL)
  274. return new(mem_ctx) ir_loop_jump(ir_loop_jump::jump_break);
  275. if (strcmp(symbol->value(), "continue") == 0 && loop_ctx != NULL)
  276. return new(mem_ctx) ir_loop_jump(ir_loop_jump::jump_continue);
  277. }
  278. s_list *list = SX_AS_LIST(expr);
  279. if (list == NULL || list->subexpressions.is_empty()) {
  280. ir_read_error(expr, "Invalid instruction.\n");
  281. return NULL;
  282. }
  283. s_symbol *tag = SX_AS_SYMBOL(list->subexpressions.get_head());
  284. if (tag == NULL) {
  285. ir_read_error(expr, "expected instruction tag");
  286. return NULL;
  287. }
  288. ir_instruction *inst = NULL;
  289. if (strcmp(tag->value(), "declare") == 0) {
  290. inst = read_declaration(list);
  291. } else if (strcmp(tag->value(), "assign") == 0) {
  292. inst = read_assignment(list);
  293. } else if (strcmp(tag->value(), "if") == 0) {
  294. inst = read_if(list, loop_ctx);
  295. } else if (strcmp(tag->value(), "loop") == 0) {
  296. inst = read_loop(list);
  297. } else if (strcmp(tag->value(), "call") == 0) {
  298. inst = read_call(list);
  299. } else if (strcmp(tag->value(), "return") == 0) {
  300. inst = read_return(list);
  301. } else if (strcmp(tag->value(), "function") == 0) {
  302. inst = read_function(list, false);
  303. } else {
  304. inst = read_rvalue(list);
  305. if (inst == NULL)
  306. ir_read_error(NULL, "when reading instruction");
  307. }
  308. return inst;
  309. }
  310. ir_variable *
  311. ir_reader::read_declaration(s_expression *expr)
  312. {
  313. s_list *s_quals;
  314. s_expression *s_type;
  315. s_symbol *s_name;
  316. s_pattern pat[] = { "declare", s_quals, s_type, s_name };
  317. if (!MATCH(expr, pat)) {
  318. ir_read_error(expr, "expected (declare (<qualifiers>) <type> <name>)");
  319. return NULL;
  320. }
  321. const glsl_type *type = read_type(s_type);
  322. if (type == NULL)
  323. return NULL;
  324. ir_variable *var = new(mem_ctx) ir_variable(type, s_name->value(),
  325. ir_var_auto, glsl_precision_undefined);
  326. foreach_iter(exec_list_iterator, it, s_quals->subexpressions) {
  327. s_symbol *qualifier = SX_AS_SYMBOL(it.get());
  328. if (qualifier == NULL) {
  329. ir_read_error(expr, "qualifier list must contain only symbols");
  330. return NULL;
  331. }
  332. // FINISHME: Check for duplicate/conflicting qualifiers.
  333. if (strcmp(qualifier->value(), "centroid") == 0) {
  334. var->centroid = 1;
  335. } else if (strcmp(qualifier->value(), "invariant") == 0) {
  336. var->invariant = 1;
  337. } else if (strcmp(qualifier->value(), "uniform") == 0) {
  338. var->mode = ir_var_uniform;
  339. } else if (strcmp(qualifier->value(), "auto") == 0) {
  340. var->mode = ir_var_auto;
  341. } else if (strcmp(qualifier->value(), "in") == 0) {
  342. var->mode = ir_var_in;
  343. } else if (strcmp(qualifier->value(), "const_in") == 0) {
  344. var->mode = ir_var_const_in;
  345. } else if (strcmp(qualifier->value(), "out") == 0) {
  346. var->mode = ir_var_out;
  347. } else if (strcmp(qualifier->value(), "inout") == 0) {
  348. var->mode = ir_var_inout;
  349. } else if (strcmp(qualifier->value(), "temporary") == 0) {
  350. var->mode = ir_var_temporary;
  351. } else if (strcmp(qualifier->value(), "smooth") == 0) {
  352. var->interpolation = INTERP_QUALIFIER_SMOOTH;
  353. } else if (strcmp(qualifier->value(), "flat") == 0) {
  354. var->interpolation = INTERP_QUALIFIER_FLAT;
  355. } else if (strcmp(qualifier->value(), "noperspective") == 0) {
  356. var->interpolation = INTERP_QUALIFIER_NOPERSPECTIVE;
  357. } else {
  358. ir_read_error(expr, "unknown qualifier: %s", qualifier->value());
  359. return NULL;
  360. }
  361. }
  362. // Add the variable to the symbol table
  363. state->symbols->add_variable(var);
  364. return var;
  365. }
  366. ir_if *
  367. ir_reader::read_if(s_expression *expr, ir_loop *loop_ctx)
  368. {
  369. s_expression *s_cond;
  370. s_expression *s_then;
  371. s_expression *s_else;
  372. s_pattern pat[] = { "if", s_cond, s_then, s_else };
  373. if (!MATCH(expr, pat)) {
  374. ir_read_error(expr, "expected (if <condition> (<then>...) (<else>...))");
  375. return NULL;
  376. }
  377. ir_rvalue *condition = read_rvalue(s_cond);
  378. if (condition == NULL) {
  379. ir_read_error(NULL, "when reading condition of (if ...)");
  380. return NULL;
  381. }
  382. ir_if *iff = new(mem_ctx) ir_if(condition);
  383. read_instructions(&iff->then_instructions, s_then, loop_ctx);
  384. read_instructions(&iff->else_instructions, s_else, loop_ctx);
  385. if (state->error) {
  386. delete iff;
  387. iff = NULL;
  388. }
  389. return iff;
  390. }
  391. ir_loop *
  392. ir_reader::read_loop(s_expression *expr)
  393. {
  394. s_expression *s_counter, *s_from, *s_to, *s_inc, *s_body;
  395. s_pattern pat[] = { "loop", s_counter, s_from, s_to, s_inc, s_body };
  396. if (!MATCH(expr, pat)) {
  397. ir_read_error(expr, "expected (loop <counter> <from> <to> "
  398. "<increment> <body>)");
  399. return NULL;
  400. }
  401. // FINISHME: actually read the count/from/to fields.
  402. ir_loop *loop = new(mem_ctx) ir_loop;
  403. read_instructions(&loop->body_instructions, s_body, loop);
  404. if (state->error) {
  405. delete loop;
  406. loop = NULL;
  407. }
  408. return loop;
  409. }
  410. ir_return *
  411. ir_reader::read_return(s_expression *expr)
  412. {
  413. s_expression *s_retval;
  414. s_pattern return_value_pat[] = { "return", s_retval};
  415. s_pattern return_void_pat[] = { "return" };
  416. if (MATCH(expr, return_value_pat)) {
  417. ir_rvalue *retval = read_rvalue(s_retval);
  418. if (retval == NULL) {
  419. ir_read_error(NULL, "when reading return value");
  420. return NULL;
  421. }
  422. return new(mem_ctx) ir_return(retval);
  423. } else if (MATCH(expr, return_void_pat)) {
  424. return new(mem_ctx) ir_return;
  425. } else {
  426. ir_read_error(expr, "expected (return <rvalue>) or (return)");
  427. return NULL;
  428. }
  429. }
  430. ir_rvalue *
  431. ir_reader::read_rvalue(s_expression *expr)
  432. {
  433. s_list *list = SX_AS_LIST(expr);
  434. if (list == NULL || list->subexpressions.is_empty())
  435. return NULL;
  436. s_symbol *tag = SX_AS_SYMBOL(list->subexpressions.get_head());
  437. if (tag == NULL) {
  438. ir_read_error(expr, "expected rvalue tag");
  439. return NULL;
  440. }
  441. ir_rvalue *rvalue = read_dereference(list);
  442. if (rvalue != NULL || state->error)
  443. return rvalue;
  444. else if (strcmp(tag->value(), "swiz") == 0) {
  445. rvalue = read_swizzle(list);
  446. } else if (strcmp(tag->value(), "expression") == 0) {
  447. rvalue = read_expression(list);
  448. } else if (strcmp(tag->value(), "constant") == 0) {
  449. rvalue = read_constant(list);
  450. } else {
  451. rvalue = read_texture(list);
  452. if (rvalue == NULL && !state->error)
  453. ir_read_error(expr, "unrecognized rvalue tag: %s", tag->value());
  454. }
  455. return rvalue;
  456. }
  457. ir_assignment *
  458. ir_reader::read_assignment(s_expression *expr)
  459. {
  460. s_expression *cond_expr = NULL;
  461. s_expression *lhs_expr, *rhs_expr;
  462. s_list *mask_list;
  463. s_pattern pat4[] = { "assign", mask_list, lhs_expr, rhs_expr };
  464. s_pattern pat5[] = { "assign", cond_expr, mask_list, lhs_expr, rhs_expr };
  465. if (!MATCH(expr, pat4) && !MATCH(expr, pat5)) {
  466. ir_read_error(expr, "expected (assign [<condition>] (<write mask>) "
  467. "<lhs> <rhs>)");
  468. return NULL;
  469. }
  470. ir_rvalue *condition = NULL;
  471. if (cond_expr != NULL) {
  472. condition = read_rvalue(cond_expr);
  473. if (condition == NULL) {
  474. ir_read_error(NULL, "when reading condition of assignment");
  475. return NULL;
  476. }
  477. }
  478. unsigned mask = 0;
  479. s_symbol *mask_symbol;
  480. s_pattern mask_pat[] = { mask_symbol };
  481. if (MATCH(mask_list, mask_pat)) {
  482. const char *mask_str = mask_symbol->value();
  483. unsigned mask_length = strlen(mask_str);
  484. if (mask_length > 4) {
  485. ir_read_error(expr, "invalid write mask: %s", mask_str);
  486. return NULL;
  487. }
  488. const unsigned idx_map[] = { 3, 0, 1, 2 }; /* w=bit 3, x=0, y=1, z=2 */
  489. for (unsigned i = 0; i < mask_length; i++) {
  490. if (mask_str[i] < 'w' || mask_str[i] > 'z') {
  491. ir_read_error(expr, "write mask contains invalid character: %c",
  492. mask_str[i]);
  493. return NULL;
  494. }
  495. mask |= 1 << idx_map[mask_str[i] - 'w'];
  496. }
  497. } else if (!mask_list->subexpressions.is_empty()) {
  498. ir_read_error(mask_list, "expected () or (<write mask>)");
  499. return NULL;
  500. }
  501. ir_dereference *lhs = read_dereference(lhs_expr);
  502. if (lhs == NULL) {
  503. ir_read_error(NULL, "when reading left-hand side of assignment");
  504. return NULL;
  505. }
  506. ir_rvalue *rhs = read_rvalue(rhs_expr);
  507. if (rhs == NULL) {
  508. ir_read_error(NULL, "when reading right-hand side of assignment");
  509. return NULL;
  510. }
  511. if (mask == 0 && (lhs->type->is_vector() || lhs->type->is_scalar())) {
  512. ir_read_error(expr, "non-zero write mask required.");
  513. return NULL;
  514. }
  515. return new(mem_ctx) ir_assignment(lhs, rhs, condition, mask);
  516. }
  517. ir_call *
  518. ir_reader::read_call(s_expression *expr)
  519. {
  520. s_symbol *name;
  521. s_list *params;
  522. s_list *s_return = NULL;
  523. ir_dereference_variable *return_deref = NULL;
  524. s_pattern void_pat[] = { "call", name, params };
  525. s_pattern non_void_pat[] = { "call", name, s_return, params };
  526. if (MATCH(expr, non_void_pat)) {
  527. return_deref = read_var_ref(s_return);
  528. if (return_deref == NULL) {
  529. ir_read_error(s_return, "when reading a call's return storage");
  530. return NULL;
  531. }
  532. } else if (!MATCH(expr, void_pat)) {
  533. ir_read_error(expr, "expected (call <name> [<deref>] (<param> ...))");
  534. return NULL;
  535. }
  536. exec_list parameters;
  537. foreach_iter(exec_list_iterator, it, params->subexpressions) {
  538. s_expression *expr = (s_expression*) it.get();
  539. ir_rvalue *param = read_rvalue(expr);
  540. if (param == NULL) {
  541. ir_read_error(expr, "when reading parameter to function call");
  542. return NULL;
  543. }
  544. parameters.push_tail(param);
  545. }
  546. ir_function *f = state->symbols->get_function(name->value());
  547. if (f == NULL) {
  548. ir_read_error(expr, "found call to undefined function %s",
  549. name->value());
  550. return NULL;
  551. }
  552. ir_function_signature *callee = f->matching_signature(&parameters);
  553. if (callee == NULL) {
  554. ir_read_error(expr, "couldn't find matching signature for function "
  555. "%s", name->value());
  556. return NULL;
  557. }
  558. if (callee->return_type == glsl_type::void_type && return_deref) {
  559. ir_read_error(expr, "call has return value storage but void type");
  560. return NULL;
  561. } else if (callee->return_type != glsl_type::void_type && !return_deref) {
  562. ir_read_error(expr, "call has non-void type but no return value storage");
  563. return NULL;
  564. }
  565. return new(mem_ctx) ir_call(callee, return_deref, &parameters);
  566. }
  567. ir_expression *
  568. ir_reader::read_expression(s_expression *expr)
  569. {
  570. s_expression *s_type;
  571. s_symbol *s_op;
  572. s_expression *s_arg1;
  573. s_pattern pat[] = { "expression", s_type, s_op, s_arg1 };
  574. if (!PARTIAL_MATCH(expr, pat)) {
  575. ir_read_error(expr, "expected (expression <type> <operator> "
  576. "<operand> [<operand>])");
  577. return NULL;
  578. }
  579. s_expression *s_arg2 = (s_expression *) s_arg1->next; // may be tail sentinel
  580. s_expression *s_arg3 = s_arg2->is_tail_sentinel() ? s_arg2 : (s_expression*)s_arg2->next; // may be tail sentinel
  581. const glsl_type *type = read_type(s_type);
  582. if (type == NULL)
  583. return NULL;
  584. /* Read the operator */
  585. ir_expression_operation op = ir_expression::get_operator(s_op->value());
  586. if (op == (ir_expression_operation) -1) {
  587. ir_read_error(expr, "invalid operator: %s", s_op->value());
  588. return NULL;
  589. }
  590. unsigned num_operands = ir_expression::get_num_operands(op);
  591. if (num_operands == 1 && !s_arg1->next->is_tail_sentinel()) {
  592. ir_read_error(expr, "expected (expression <type> %s <operand>)",
  593. s_op->value());
  594. return NULL;
  595. }
  596. ir_rvalue *arg1 = read_rvalue(s_arg1);
  597. ir_rvalue *arg2 = NULL;
  598. ir_rvalue *arg3 = NULL;
  599. if (arg1 == NULL) {
  600. ir_read_error(NULL, "when reading first operand of %s", s_op->value());
  601. return NULL;
  602. }
  603. if (num_operands == 2) {
  604. if (s_arg2->is_tail_sentinel() || !s_arg2->next->is_tail_sentinel()) {
  605. ir_read_error(expr, "expected (expression <type> %s <operand> "
  606. "<operand>)", s_op->value());
  607. return NULL;
  608. }
  609. arg2 = read_rvalue(s_arg2);
  610. if (arg2 == NULL) {
  611. ir_read_error(NULL, "when reading second operand of %s",
  612. s_op->value());
  613. return NULL;
  614. }
  615. }
  616. if (num_operands == 3) {
  617. if (s_arg2->is_tail_sentinel() || s_arg3->is_tail_sentinel() || !s_arg3->next->is_tail_sentinel()) {
  618. ir_read_error(expr, "expected (expression <type> %s <operand> "
  619. "<operand> <operand>)", s_op->value());
  620. return NULL;
  621. }
  622. arg2 = read_rvalue(s_arg2);
  623. if (arg2 == NULL) {
  624. ir_read_error(NULL, "when reading second operand of %s",
  625. s_op->value());
  626. return NULL;
  627. }
  628. arg3 = read_rvalue(s_arg3);
  629. if (arg3 == NULL) {
  630. ir_read_error(NULL, "when reading third operand of %s",
  631. s_op->value());
  632. return NULL;
  633. }
  634. }
  635. return new(mem_ctx) ir_expression(op, type, arg1, arg2, arg3);
  636. }
  637. ir_swizzle *
  638. ir_reader::read_swizzle(s_expression *expr)
  639. {
  640. s_symbol *swiz;
  641. s_expression *sub;
  642. s_pattern pat[] = { "swiz", swiz, sub };
  643. if (!MATCH(expr, pat)) {
  644. ir_read_error(expr, "expected (swiz <swizzle> <rvalue>)");
  645. return NULL;
  646. }
  647. if (strlen(swiz->value()) > 4) {
  648. ir_read_error(expr, "expected a valid swizzle; found %s", swiz->value());
  649. return NULL;
  650. }
  651. ir_rvalue *rvalue = read_rvalue(sub);
  652. if (rvalue == NULL)
  653. return NULL;
  654. ir_swizzle *ir = ir_swizzle::create(rvalue, swiz->value(),
  655. rvalue->type->vector_elements);
  656. if (ir == NULL)
  657. ir_read_error(expr, "invalid swizzle");
  658. return ir;
  659. }
  660. ir_constant *
  661. ir_reader::read_constant(s_expression *expr)
  662. {
  663. s_expression *type_expr;
  664. s_list *values;
  665. s_pattern pat[] = { "constant", type_expr, values };
  666. if (!MATCH(expr, pat)) {
  667. ir_read_error(expr, "expected (constant <type> (...))");
  668. return NULL;
  669. }
  670. const glsl_type *type = read_type(type_expr);
  671. if (type == NULL)
  672. return NULL;
  673. if (values == NULL) {
  674. ir_read_error(expr, "expected (constant <type> (...))");
  675. return NULL;
  676. }
  677. if (type->is_array()) {
  678. unsigned elements_supplied = 0;
  679. exec_list elements;
  680. foreach_iter(exec_list_iterator, it, values->subexpressions) {
  681. s_expression *elt = (s_expression *) it.get();
  682. ir_constant *ir_elt = read_constant(elt);
  683. if (ir_elt == NULL)
  684. return NULL;
  685. elements.push_tail(ir_elt);
  686. elements_supplied++;
  687. }
  688. if (elements_supplied != type->length) {
  689. ir_read_error(values, "expected exactly %u array elements, "
  690. "given %u", type->length, elements_supplied);
  691. return NULL;
  692. }
  693. return new(mem_ctx) ir_constant(type, &elements);
  694. }
  695. ir_constant_data data = { { 0 } };
  696. // Read in list of values (at most 16).
  697. unsigned k = 0;
  698. foreach_iter(exec_list_iterator, it, values->subexpressions) {
  699. if (k >= 16) {
  700. ir_read_error(values, "expected at most 16 numbers");
  701. return NULL;
  702. }
  703. s_expression *expr = (s_expression*) it.get();
  704. if (type->base_type == GLSL_TYPE_FLOAT) {
  705. s_number *value = SX_AS_NUMBER(expr);
  706. if (value == NULL) {
  707. ir_read_error(values, "expected numbers");
  708. return NULL;
  709. }
  710. data.f[k] = value->fvalue();
  711. } else {
  712. s_int *value = SX_AS_INT(expr);
  713. if (value == NULL) {
  714. ir_read_error(values, "expected integers");
  715. return NULL;
  716. }
  717. switch (type->base_type) {
  718. case GLSL_TYPE_UINT: {
  719. data.u[k] = value->value();
  720. break;
  721. }
  722. case GLSL_TYPE_INT: {
  723. data.i[k] = value->value();
  724. break;
  725. }
  726. case GLSL_TYPE_BOOL: {
  727. data.b[k] = !!value->value();
  728. break;
  729. }
  730. default:
  731. ir_read_error(values, "unsupported constant type");
  732. return NULL;
  733. }
  734. }
  735. ++k;
  736. }
  737. if (k != type->components()) {
  738. ir_read_error(values, "expected %u constant values, found %u",
  739. type->components(), k);
  740. return NULL;
  741. }
  742. return new(mem_ctx) ir_constant(type, &data);
  743. }
  744. ir_dereference_variable *
  745. ir_reader::read_var_ref(s_expression *expr)
  746. {
  747. s_symbol *s_var;
  748. s_pattern var_pat[] = { "var_ref", s_var };
  749. if (MATCH(expr, var_pat)) {
  750. ir_variable *var = state->symbols->get_variable(s_var->value());
  751. if (var == NULL) {
  752. ir_read_error(expr, "undeclared variable: %s", s_var->value());
  753. return NULL;
  754. }
  755. return new(mem_ctx) ir_dereference_variable(var);
  756. }
  757. return NULL;
  758. }
  759. ir_dereference *
  760. ir_reader::read_dereference(s_expression *expr)
  761. {
  762. s_expression *s_subject;
  763. s_expression *s_index;
  764. s_symbol *s_field;
  765. s_pattern array_pat[] = { "array_ref", s_subject, s_index };
  766. s_pattern record_pat[] = { "record_ref", s_subject, s_field };
  767. ir_dereference_variable *var_ref = read_var_ref(expr);
  768. if (var_ref != NULL) {
  769. return var_ref;
  770. } else if (MATCH(expr, array_pat)) {
  771. ir_rvalue *subject = read_rvalue(s_subject);
  772. if (subject == NULL) {
  773. ir_read_error(NULL, "when reading the subject of an array_ref");
  774. return NULL;
  775. }
  776. ir_rvalue *idx = read_rvalue(s_index);
  777. if (subject == NULL) {
  778. ir_read_error(NULL, "when reading the index of an array_ref");
  779. return NULL;
  780. }
  781. return new(mem_ctx) ir_dereference_array(subject, idx);
  782. } else if (MATCH(expr, record_pat)) {
  783. ir_rvalue *subject = read_rvalue(s_subject);
  784. if (subject == NULL) {
  785. ir_read_error(NULL, "when reading the subject of a record_ref");
  786. return NULL;
  787. }
  788. return new(mem_ctx) ir_dereference_record(subject, s_field->value());
  789. }
  790. return NULL;
  791. }
  792. ir_texture *
  793. ir_reader::read_texture(s_expression *expr)
  794. {
  795. s_symbol *tag = NULL;
  796. s_expression *s_type = NULL;
  797. s_expression *s_sampler = NULL;
  798. s_expression *s_coord = NULL;
  799. s_expression *s_offset = NULL;
  800. s_expression *s_lod = NULL;
  801. ir_texture_opcode op = ir_tex; /* silence warning */
  802. s_pattern tex_pattern[] =
  803. { "tex", s_type, s_sampler, s_coord, s_offset };
  804. s_pattern txf_pattern[] =
  805. { "txf", s_type, s_sampler, s_coord, s_offset, s_lod };
  806. s_pattern txs_pattern[] =
  807. { "txs", s_type, s_sampler, s_lod };
  808. s_pattern other_pattern[] =
  809. { tag, s_type, s_sampler, s_coord, s_offset, s_lod };
  810. if (MATCH(expr, tex_pattern)) {
  811. op = ir_tex;
  812. } else if (MATCH(expr, txf_pattern)) {
  813. op = ir_txf;
  814. } else if (MATCH(expr, txs_pattern)) {
  815. op = ir_txs;
  816. } else if (MATCH(expr, other_pattern)) {
  817. op = ir_texture::get_opcode(tag->value());
  818. if (op == -1)
  819. return NULL;
  820. } else {
  821. ir_read_error(NULL, "unexpected texture pattern");
  822. return NULL;
  823. }
  824. ir_texture *tex = new(mem_ctx) ir_texture(op);
  825. // Read return type
  826. const glsl_type *type = read_type(s_type);
  827. if (type == NULL) {
  828. ir_read_error(NULL, "when reading type in (%s ...)",
  829. tex->opcode_string());
  830. return NULL;
  831. }
  832. // Read sampler (must be a deref)
  833. ir_dereference *sampler = read_dereference(s_sampler);
  834. if (sampler == NULL) {
  835. ir_read_error(NULL, "when reading sampler in (%s ...)",
  836. tex->opcode_string());
  837. return NULL;
  838. }
  839. tex->set_sampler(sampler, type);
  840. if (op != ir_txs) {
  841. // Read coordinate (any rvalue)
  842. tex->coordinate = read_rvalue(s_coord);
  843. if (tex->coordinate == NULL) {
  844. ir_read_error(NULL, "when reading coordinate in (%s ...)",
  845. tex->opcode_string());
  846. return NULL;
  847. }
  848. // Read texel offset - either 0 or an rvalue.
  849. s_int *si_offset = SX_AS_INT(s_offset);
  850. if (si_offset == NULL || si_offset->value() != 0) {
  851. tex->offset = read_rvalue(s_offset);
  852. if (tex->offset == NULL) {
  853. ir_read_error(s_offset, "expected 0 or an expression");
  854. return NULL;
  855. }
  856. }
  857. }
  858. switch (op) {
  859. case ir_txb:
  860. tex->lod_info.bias = read_rvalue(s_lod);
  861. if (tex->lod_info.bias == NULL) {
  862. ir_read_error(NULL, "when reading LOD bias in (txb ...)");
  863. return NULL;
  864. }
  865. break;
  866. case ir_txl:
  867. case ir_txf:
  868. case ir_txs:
  869. tex->lod_info.lod = read_rvalue(s_lod);
  870. if (tex->lod_info.lod == NULL) {
  871. ir_read_error(NULL, "when reading LOD in (%s ...)",
  872. tex->opcode_string());
  873. return NULL;
  874. }
  875. break;
  876. case ir_txd: {
  877. s_expression *s_dx, *s_dy;
  878. s_pattern dxdy_pat[] = { s_dx, s_dy };
  879. if (!MATCH(s_lod, dxdy_pat)) {
  880. ir_read_error(s_lod, "expected (dPdx dPdy) in (txd ...)");
  881. return NULL;
  882. }
  883. tex->lod_info.grad.dPdx = read_rvalue(s_dx);
  884. if (tex->lod_info.grad.dPdx == NULL) {
  885. ir_read_error(NULL, "when reading dPdx in (txd ...)");
  886. return NULL;
  887. }
  888. tex->lod_info.grad.dPdy = read_rvalue(s_dy);
  889. if (tex->lod_info.grad.dPdy == NULL) {
  890. ir_read_error(NULL, "when reading dPdy in (txd ...)");
  891. return NULL;
  892. }
  893. break;
  894. }
  895. default:
  896. // tex doesn't have any extra parameters.
  897. break;
  898. };
  899. return tex;
  900. }