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