void
_mesa_glsl_read_ir(_mesa_glsl_parse_state *state, exec_list *instructions,
- const char *src)
+ const char *src, bool scan_for_protos)
{
s_expression *expr = s_expression::read_expression(state, src);
if (expr == NULL) {
return;
}
- scan_for_prototypes(state, instructions, expr);
- if (state->error)
- return;
+ if (scan_for_protos) {
+ scan_for_prototypes(state, instructions, expr);
+ if (state->error)
+ return;
+ }
read_instructions(state, instructions, expr, NULL);
talloc_free(expr);
return NULL;
}
- return glsl_type::get_array_instance(st, base_type, size->value());
+ return glsl_type::get_array_instance(base_type, size->value());
} else if (strcmp(type_sym->value(), "struct") == 0) {
assert(false); // FINISHME
} else {
static ir_function *
read_function(_mesa_glsl_parse_state *st, s_list *list, bool skip_body)
{
- void *ctx = talloc_parent(st);
+ void *ctx = st;
+ bool added = false;
if (list->length() < 3) {
ir_read_error(st, list, "Expected (function <name> (signature ...) ...)");
return NULL;
ir_function *f = st->symbols->get_function(name->value());
if (f == NULL) {
f = new(ctx) ir_function(name->value());
- bool added = st->symbols->add_function(name->value(), f);
+ f->is_builtin = true;
+ added = st->symbols->add_function(f->name, f);
assert(added);
}
read_function_sig(st, f, siglist, skip_body);
}
- return f;
+ return added ? f : NULL;
}
static void
read_function_sig(_mesa_glsl_parse_state *st, ir_function *f, s_list *list,
bool skip_body)
{
- void *ctx = talloc_parent(st);
+ void *ctx = st;
if (list->length() != 4) {
ir_read_error(st, list, "Expected (signature <type> (parameters ...) "
"(<instruction> ...))");
}
ir_function_signature *sig = f->exact_matching_signature(&hir_parameters);
- if (sig != NULL) {
+ if (sig == NULL && skip_body) {
+ /* If scanning for prototypes, generate a new signature. */
+ sig = new(ctx) ir_function_signature(return_type);
+ f->add_signature(sig);
+ } else if (sig != NULL) {
const char *badvar = sig->qualifiers_match(&hir_parameters);
if (badvar != NULL) {
ir_read_error(st, list, "function `%s' parameter `%s' qualifiers "
return;
}
} else {
- sig = new(ctx) ir_function_signature(return_type);
- f->add_signature(sig);
+ /* No prototype for this body exists - skip it. */
+ st->symbols->pop_scope();
+ return;
}
+ assert(sig != NULL);
sig->replace_parameters(&hir_parameters);
- if (!skip_body) {
+ if (!skip_body && !body_list->subexpressions.is_empty()) {
if (sig->is_defined) {
ir_read_error(st, list, "function %s redefined", f->name);
return;
foreach_iter(exec_list_iterator, it, list->subexpressions) {
s_expression *sub = (s_expression*) it.get();
ir_instruction *ir = read_instruction(st, sub, loop_ctx);
- if (ir == NULL) {
- ir_read_error(st, sub, "Invalid instruction.\n");
- return;
- }
- instructions->push_tail(ir);
+ if (ir != NULL)
+ instructions->push_tail(ir);
}
}
read_instruction(_mesa_glsl_parse_state *st, s_expression *expr,
ir_loop *loop_ctx)
{
- void *ctx = talloc_parent(st);
+ void *ctx = st;
s_symbol *symbol = SX_AS_SYMBOL(expr);
if (symbol != NULL) {
if (strcmp(symbol->value(), "break") == 0 && loop_ctx != NULL)
}
s_list *list = SX_AS_LIST(expr);
- if (list == NULL || list->subexpressions.is_empty())
+ if (list == NULL || list->subexpressions.is_empty()) {
+ ir_read_error(st, expr, "Invalid instruction.\n");
return NULL;
+ }
s_symbol *tag = SX_AS_SYMBOL(list->subexpressions.get_head());
if (tag == NULL) {
ir_instruction *inst = NULL;
if (strcmp(tag->value(), "declare") == 0) {
inst = read_declaration(st, list);
+ } else if (strcmp(tag->value(), "assign") == 0) {
+ inst = read_assignment(st, list);
} else if (strcmp(tag->value(), "if") == 0) {
inst = read_if(st, list, loop_ctx);
} else if (strcmp(tag->value(), "loop") == 0) {
static ir_variable *
read_declaration(_mesa_glsl_parse_state *st, s_list *list)
{
- void *ctx = talloc_parent(st);
+ void *ctx = st;
if (list->length() != 4) {
ir_read_error(st, list, "expected (declare (<qualifiers>) <type> "
"<name>)");
return NULL;
}
- ir_variable *var = new(ctx) ir_variable(type, var_name->value());
+ ir_variable *var = new(ctx) ir_variable(type, var_name->value(),
+ ir_var_auto);
foreach_iter(exec_list_iterator, it, quals->subexpressions) {
s_symbol *qualifier = SX_AS_SYMBOL(it.get());
}
// Add the variable to the symbol table
- st->symbols->add_variable(var_name->value(), var);
+ st->symbols->add_variable(var->name, var);
return var;
}
static ir_if *
read_if(_mesa_glsl_parse_state *st, s_list *list, ir_loop *loop_ctx)
{
- void *ctx = talloc_parent(st);
+ void *ctx = st;
if (list->length() != 4) {
ir_read_error(st, list, "expected (if <condition> (<then> ...) "
"(<else> ...))");
static ir_loop *
read_loop(_mesa_glsl_parse_state *st, s_list *list)
{
- void *ctx = talloc_parent(st);
+ void *ctx = st;
if (list->length() != 6) {
ir_read_error(st, list, "expected (loop <counter> <from> <to> "
"<increment> <body>)");
static ir_return *
read_return(_mesa_glsl_parse_state *st, s_list *list)
{
- void *ctx = talloc_parent(st);
+ void *ctx = st;
if (list->length() != 2) {
ir_read_error(st, list, "expected (return <rvalue>)");
return NULL;
return rvalue;
else if (strcmp(tag->value(), "swiz") == 0) {
rvalue = read_swizzle(st, list);
- } else if (strcmp(tag->value(), "assign") == 0) {
- rvalue = read_assignment(st, list);
} else if (strcmp(tag->value(), "expression") == 0) {
rvalue = read_expression(st, list);
} else if (strcmp(tag->value(), "call") == 0) {
static ir_assignment *
read_assignment(_mesa_glsl_parse_state *st, s_list *list)
{
- void *ctx = talloc_parent(st);
+ void *ctx = st;
if (list->length() != 4) {
ir_read_error(st, list, "expected (assign <condition> <lhs> <rhs>)");
return NULL;
static ir_call *
read_call(_mesa_glsl_parse_state *st, s_list *list)
{
- void *ctx = talloc_parent(st);
+ void *ctx = st;
if (list->length() != 3) {
ir_read_error(st, list, "expected (call <name> (<param> ...))");
return NULL;
return NULL;
}
- const ir_function_signature *callee = f->matching_signature(¶meters);
+ ir_function_signature *callee = f->matching_signature(¶meters);
if (callee == NULL) {
ir_read_error(st, list, "couldn't find matching signature for function "
"%s", name->value());
static ir_expression *
read_expression(_mesa_glsl_parse_state *st, s_list *list)
{
- void *ctx = talloc_parent(st);
+ void *ctx = st;
const unsigned list_length = list->length();
if (list_length < 4) {
ir_read_error(st, list, "expected (expression <type> <operator> "
static ir_constant *
read_constant(_mesa_glsl_parse_state *st, s_list *list)
{
- void *ctx = talloc_parent(st);
+ void *ctx = st;
if (list->length() != 3) {
- ir_read_error(st, list, "expected (constant <type> (<num> ... <num>))");
+ ir_read_error(st, list, "expected (constant <type> (...))");
return NULL;
}
s_list *values = SX_AS_LIST(type_expr->next);
if (values == NULL) {
- ir_read_error(st, list, "expected (constant <type> (<num> ... <num>))");
+ ir_read_error(st, list, "expected (constant <type> (...))");
return NULL;
}
+ if (type->is_array()) {
+ const unsigned elements_supplied = values->length();
+ if (elements_supplied != type->length) {
+ ir_read_error(st, values, "expected exactly %u array elements, "
+ "given %u", type->length, elements_supplied);
+ return NULL;
+ }
+
+ exec_list elements;
+ foreach_iter(exec_list_iterator, it, values->subexpressions) {
+ s_expression *expr = (s_expression *) it.get();
+ s_list *elt = SX_AS_LIST(expr);
+ if (elt == NULL) {
+ ir_read_error(st, expr, "expected (constant ...) array element");
+ return NULL;
+ }
+
+ ir_constant *ir_elt = read_constant(st, elt);
+ if (ir_elt == NULL)
+ return NULL;
+ elements.push_tail(ir_elt);
+ }
+ return new(ctx) ir_constant(type, &elements);
+ }
+
const glsl_type *const base_type = type->get_base_type();
- ir_constant_data data;
+ ir_constant_data data = { { 0 } };
// Read in list of values (at most 16).
int k = 0;
static ir_dereference *
read_var_ref(_mesa_glsl_parse_state *st, s_list *list)
{
- void *ctx = talloc_parent(st);
+ void *ctx = st;
if (list->length() != 2) {
ir_read_error(st, list, "expected (var_ref <variable name>)");
return NULL;
static ir_dereference *
read_array_ref(_mesa_glsl_parse_state *st, s_list *list)
{
- void *ctx = talloc_parent(st);
+ void *ctx = st;
if (list->length() != 3) {
ir_read_error(st, list, "expected (array_ref <rvalue> <index>)");
return NULL;
static ir_dereference *
read_record_ref(_mesa_glsl_parse_state *st, s_list *list)
{
- void *ctx = talloc_parent(st);
+ void *ctx = st;
if (list->length() != 3) {
ir_read_error(st, list, "expected (record_ref <rvalue> <field>)");
return NULL;
static ir_texture *
read_texture(_mesa_glsl_parse_state *st, s_list *list)
{
- void *ctx = talloc_parent(st);
+ void *ctx = st;
s_symbol *tag = SX_AS_SYMBOL(list->subexpressions.head);
assert(tag != NULL);