-/*\r
- * Mesa 3-D graphics library\r
- * Version: 6.3\r
- *\r
- * Copyright (C) 2005 Brian Paul All Rights Reserved.\r
- *\r
- * Permission is hereby granted, free of charge, to any person obtaining a\r
- * copy of this software and associated documentation files (the "Software"),\r
- * to deal in the Software without restriction, including without limitation\r
- * the rights to use, copy, modify, merge, publish, distribute, sublicense,\r
- * and/or sell copies of the Software, and to permit persons to whom the\r
- * Software is furnished to do so, subject to the following conditions:\r
- *\r
- * The above copyright notice and this permission notice shall be included\r
- * in all copies or substantial portions of the Software.\r
- *\r
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS\r
- * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL\r
- * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN\r
- * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
- */\r
-\r
-/**\r
- * \file slang_compile.c\r
- * slang front-end compiler\r
- * \author Michal Krol\r
- */\r
-\r
-#include "imports.h" \r
-#include "slang_compile.h"\r
-#include "grammar_mesa.h"\r
-\r
-/*\r
- This is a straightforward implementation of the slang front-end compiler.\r
- Lots of error-checking functionality is missing but every well-formed shader source should\r
- compile successfully and execute as expected. However, some semantically ill-formed shaders\r
- may be accepted resulting in undefined behaviour.\r
-*/\r
-\r
-void slang_alloc_free (void *ptr)\r
-{\r
- _mesa_free (ptr);\r
-}\r
-\r
-void *slang_alloc_malloc (unsigned int size)\r
-{\r
- return _mesa_malloc (size);\r
-}\r
-\r
-void *slang_alloc_realloc (void *ptr, unsigned int old_size, unsigned int size)\r
-{\r
- return _mesa_realloc (ptr, old_size, size);\r
-}\r
-\r
-int slang_string_compare (const char *str1, const char *str2)\r
-{\r
- return _mesa_strcmp (str1, str2);\r
-}\r
-\r
-char *slang_string_copy (char *dst, const char *src)\r
-{\r
- return _mesa_strcpy (dst, src);\r
-}\r
-\r
-char *slang_string_concat (char *dst, const char *src)\r
-{\r
- return _mesa_strcpy (dst + _mesa_strlen (dst), src);\r
-}\r
-\r
-char *slang_string_duplicate (const char *src)\r
-{\r
- return _mesa_strdup (src);\r
-}\r
-\r
-static void slang_variable_construct (slang_variable *);\r
-static int slang_variable_copy (slang_variable *, const slang_variable *);\r
-static void slang_struct_construct (slang_struct *);\r
-static void slang_struct_destruct (slang_struct *);\r
-static int slang_struct_copy (slang_struct *, const slang_struct *);\r
-static int slang_struct_equal (const slang_struct *, const slang_struct *);\r
-static void slang_variable_destruct (slang_variable *);\r
-\r
-/* slang_type_specifier */\r
-\r
-static void slang_type_specifier_construct (slang_type_specifier *spec)\r
-{\r
- spec->type = slang_spec_void;\r
- spec->_struct = NULL;\r
- spec->_array = NULL;\r
-}\r
-\r
-static void slang_type_specifier_destruct (slang_type_specifier *spec)\r
-{\r
- if (spec->_struct != NULL)\r
- {\r
- slang_struct_destruct (spec->_struct);\r
- slang_alloc_free (spec->_struct);\r
- }\r
- if (spec->_array != NULL)\r
- {\r
- slang_type_specifier_destruct (spec->_array);\r
- slang_alloc_free (spec->_array);\r
- }\r
-}\r
-\r
-static int slang_type_specifier_copy (slang_type_specifier *x, const slang_type_specifier *y)\r
-{\r
- slang_type_specifier_destruct (x);\r
- slang_type_specifier_construct (x);\r
- x->type = y->type;\r
- if (x->type == slang_spec_struct)\r
- {\r
- x->_struct = (slang_struct *) slang_alloc_malloc (sizeof (slang_struct));\r
- slang_struct_construct (x->_struct);\r
- return slang_struct_copy (x->_struct, y->_struct);\r
- }\r
- if (x->type == slang_spec_array)\r
- {\r
- x->_array = (slang_type_specifier *) slang_alloc_malloc (sizeof (slang_type_specifier));\r
- slang_type_specifier_construct (x->_array);\r
- return slang_type_specifier_copy (x->_array, y->_array);\r
- }\r
- return 1;\r
-}\r
-\r
-static int slang_type_specifier_equal (const slang_type_specifier *x, const slang_type_specifier *y)\r
-{\r
- if (x->type != y->type)\r
- return 0;\r
- if (x->type == slang_spec_struct)\r
- return slang_struct_equal (x->_struct, y->_struct);\r
- if (x->type == slang_spec_array)\r
- return slang_type_specifier_equal (x->_array, y->_array);\r
- return 1;\r
-}\r
-\r
-/* slang_fully_specified_type */\r
-\r
-static void slang_fully_specified_type_construct (slang_fully_specified_type *type)\r
-{\r
- type->qualifier = slang_qual_none;\r
- slang_type_specifier_construct (&type->specifier);\r
-}\r
-\r
-static void slang_fully_specified_type_destruct (slang_fully_specified_type *type)\r
-{\r
- slang_type_specifier_destruct (&type->specifier);\r
-}\r
-\r
-static int slang_fully_specified_type_copy (slang_fully_specified_type *x,\r
- const slang_fully_specified_type *y)\r
-{\r
- slang_fully_specified_type_construct (x);\r
- slang_fully_specified_type_destruct (x);\r
- x->qualifier = y->qualifier;\r
- return slang_type_specifier_copy (&x->specifier, &y->specifier);\r
-}\r
-\r
-/* slang_variable_scope */\r
-\r
-static void slang_variable_scope_construct (slang_variable_scope *scope)\r
-{\r
- scope->variables = NULL;\r
- scope->num_variables = 0;\r
- scope->outer_scope = NULL;\r
-}\r
-\r
-static void slang_variable_scope_destruct (slang_variable_scope *scope)\r
-{\r
- unsigned int i;\r
- for (i = 0; i < scope->num_variables; i++)\r
- slang_variable_destruct (scope->variables + i);\r
- slang_alloc_free (scope->variables);\r
-}\r
-\r
-static int slang_variable_scope_copy (slang_variable_scope *x, const slang_variable_scope *y)\r
-{\r
- unsigned int i;\r
- slang_variable_scope_destruct (x);\r
- slang_variable_scope_construct (x);\r
- x->variables = (slang_variable *) slang_alloc_malloc (y->num_variables * sizeof (\r
- slang_variable));\r
- if (x->variables == NULL)\r
- return 0;\r
- x->num_variables = y->num_variables;\r
- for (i = 0; i < x->num_variables; i++)\r
- slang_variable_construct (x->variables + i);\r
- for (i = 0; i < x->num_variables; i++)\r
- if (!slang_variable_copy (x->variables + i, y->variables + i))\r
- return 0;\r
- x->outer_scope = y->outer_scope;\r
- return 1;\r
-}\r
-\r
-/* slang_operation */\r
-/* XXX mem! */\r
-static void slang_operation_construct (slang_operation *oper)\r
-{\r
- oper->type = slang_oper_none;\r
- oper->children = NULL;\r
- oper->num_children = 0;\r
- oper->literal = (float) 0;\r
- oper->identifier = NULL;\r
- oper->locals = (slang_variable_scope *) slang_alloc_malloc (sizeof (slang_variable_scope));\r
- slang_variable_scope_construct (oper->locals);\r
-}\r
-\r
-static void slang_operation_destruct (slang_operation *oper)\r
-{\r
- unsigned int i;\r
- for (i = 0; i < oper->num_children; i++)\r
- slang_operation_destruct (oper->children + i);\r
- slang_alloc_free (oper->children);\r
- slang_alloc_free (oper->identifier);\r
- slang_variable_scope_destruct (oper->locals);\r
- slang_alloc_free (oper->locals);\r
-}\r
-\r
-static int slang_operation_copy (slang_operation *x, const slang_operation *y)\r
-{\r
- unsigned int i;\r
- slang_operation_destruct (x);\r
- slang_operation_construct (x);\r
- x->type = y->type;\r
- x->children = (slang_operation *) slang_alloc_malloc (y->num_children * sizeof (\r
- slang_operation));\r
- if (x->children == NULL)\r
- return 0;\r
- x->num_children = y->num_children;\r
- for (i = 0; i < x->num_children; i++)\r
- slang_operation_construct (x->children + i);\r
- for (i = 0; i < x->num_children; i++)\r
- if (!slang_operation_copy (x->children + i, y->children + i))\r
- return 0;\r
- x->literal = y->literal;\r
- if (y->identifier != NULL)\r
- {\r
- x->identifier = slang_string_duplicate (y->identifier);\r
- if (x->identifier == NULL)\r
- return 0;\r
- }\r
- if (!slang_variable_scope_copy (x->locals, y->locals))\r
- return 0;\r
- return 1;\r
-}\r
-\r
-/* slang_variable */\r
-\r
-static void slang_variable_construct (slang_variable *var)\r
-{\r
- slang_fully_specified_type_construct (&var->type);\r
- var->name = NULL;\r
- var->array_size = NULL;\r
- var->initializer = NULL;\r
-}\r
-\r
-static void slang_variable_destruct (slang_variable *var)\r
-{\r
- slang_fully_specified_type_destruct (&var->type);\r
- slang_alloc_free (var->name);\r
- if (var->array_size != NULL)\r
- {\r
- slang_operation_destruct (var->array_size);\r
- slang_alloc_free (var->array_size);\r
- }\r
- if (var->initializer != NULL)\r
- {\r
- slang_operation_destruct (var->initializer);\r
- slang_alloc_free (var->initializer);\r
- }\r
-}\r
-\r
-static int slang_variable_copy (slang_variable *x, const slang_variable *y)\r
-{\r
- slang_variable_destruct (x);\r
- slang_variable_construct (x);\r
- if (!slang_fully_specified_type_copy (&x->type, &y->type))\r
- return 0;\r
- if (y->name != NULL)\r
- {\r
- x->name = slang_string_duplicate (y->name);\r
- if (x->name == NULL)\r
- return 0;\r
- }\r
- if (y->array_size != NULL)\r
- if (!slang_operation_copy (x->array_size, y->array_size))\r
- return 0;\r
- if (y->initializer != NULL)\r
- if (!slang_operation_copy (x->initializer, y->initializer))\r
- return 0;\r
- return 1;\r
-}\r
-\r
-/* slang_struct_scope */\r
-\r
-static void slang_struct_scope_construct (slang_struct_scope *scope)\r
-{\r
- scope->structs = NULL;\r
- scope->num_structs = 0;\r
- scope->outer_scope = NULL;\r
-}\r
-\r
-static void slang_struct_scope_destruct (slang_struct_scope *scope)\r
-{\r
- unsigned int i;\r
- for (i = 0; i < scope->num_structs; i++)\r
- slang_struct_destruct (scope->structs + i);\r
- slang_alloc_free (scope->structs);\r
-}\r
-\r
-static int slang_struct_scope_copy (slang_struct_scope *x, const slang_struct_scope *y)\r
-{\r
- unsigned int i;\r
- slang_struct_scope_destruct (x);\r
- slang_struct_scope_construct (x);\r
- x->structs = (slang_struct *) slang_alloc_malloc (y->num_structs * sizeof (slang_struct));\r
- if (x->structs == NULL)\r
- return 0;\r
- x->num_structs = y->num_structs;\r
- for (i = 0; i < x->num_structs; i++)\r
- slang_struct_construct (x->structs + i);\r
- for (i = 0; i < x->num_structs; i++)\r
- if (!slang_struct_copy (x->structs + i, y->structs + i))\r
- return 0;\r
- x->outer_scope = y->outer_scope;\r
- return 1;\r
-}\r
-\r
-static slang_struct *slang_struct_scope_find (slang_struct_scope *stru, const char *name,\r
- int all_scopes)\r
-{\r
- unsigned int i;\r
- for (i = 0; i < stru->num_structs; i++)\r
- if (slang_string_compare (name, stru->structs[i].name) == 0)\r
- return stru->structs + i;\r
- if (all_scopes && stru->outer_scope != NULL)\r
- return slang_struct_scope_find (stru->outer_scope, name, 1);\r
- return NULL;\r
-}\r
-\r
-/* slang_struct */\r
-/* XXX mem! */\r
-static void slang_struct_construct (slang_struct *stru)\r
-{\r
- stru->name = NULL;\r
- stru->fields = (slang_variable_scope *) slang_alloc_malloc (sizeof (slang_variable_scope));\r
- slang_variable_scope_construct (stru->fields);\r
- stru->structs = (slang_struct_scope *) slang_alloc_malloc (sizeof (slang_struct_scope));\r
- slang_struct_scope_construct (stru->structs);\r
-}\r
-\r
-static void slang_struct_destruct (slang_struct *stru)\r
-{\r
- slang_alloc_free (stru->name);\r
- slang_variable_scope_destruct (stru->fields);\r
- slang_alloc_free (stru->fields);\r
- slang_struct_scope_destruct (stru->structs);\r
- slang_alloc_free (stru->structs);\r
-}\r
-\r
-static int slang_struct_copy (slang_struct *x, const slang_struct *y)\r
-{\r
- slang_struct_destruct (x);\r
- slang_struct_construct (x);\r
- if (y->name != NULL)\r
- {\r
- x->name = slang_string_duplicate (y->name);\r
- if (x->name == NULL)\r
- return 0;\r
- }\r
- if (!slang_variable_scope_copy (x->fields, y->fields))\r
- return 0;\r
- if (!slang_struct_scope_copy (x->structs, y->structs))\r
- return 0;\r
- return 1;\r
-}\r
-\r
-static int slang_struct_equal (const slang_struct *x, const slang_struct *y)\r
-{\r
- unsigned int i;\r
- if (x->fields->num_variables != y->fields->num_variables)\r
- return 0;\r
- for (i = 0; i < x->fields->num_variables; i++)\r
- {\r
- slang_variable *varx = x->fields->variables + i;\r
- slang_variable *vary = y->fields->variables + i;\r
- if (slang_string_compare (varx->name, vary->name) != 0)\r
- return 0;\r
- if (!slang_type_specifier_equal (&varx->type.specifier, &vary->type.specifier))\r
- return 0;\r
- if (varx->type.specifier.type == slang_spec_array)\r
- {\r
- /* TODO compare array sizes */\r
- }\r
- }\r
- return 1;\r
-}\r
-\r
-/* slang_function */\r
-/* XXX mem! */\r
-static void slang_function_construct (slang_function *func)\r
-{\r
- func->kind = slang_func_ordinary;\r
- slang_variable_construct (&func->header);\r
- func->parameters = (slang_variable_scope *) slang_alloc_malloc (sizeof (slang_variable_scope));\r
- slang_variable_scope_construct (func->parameters);\r
- func->body = NULL;\r
-}\r
-\r
-static void slang_function_destruct (slang_function *func)\r
-{\r
- slang_variable_destruct (&func->header);\r
- slang_variable_scope_destruct (func->parameters);\r
- slang_alloc_free (func->parameters);\r
- if (func->body != NULL)\r
- {\r
- slang_operation_destruct (func->body);\r
- slang_alloc_free (func->body);\r
- }\r
-}\r
-\r
-/* slang_function_scope */\r
-\r
-static void slang_function_scope_construct (slang_function_scope *scope)\r
-{\r
- scope->functions = NULL;\r
- scope->num_functions = 0;\r
- scope->outer_scope = NULL;\r
-}\r
-\r
-static void slang_function_scope_destruct (slang_function_scope *scope)\r
-{\r
- unsigned int i;\r
- for (i = 0; i < scope->num_functions; i++)\r
- slang_function_destruct (scope->functions + i);\r
- slang_alloc_free (scope->functions);\r
-}\r
-\r
-static slang_function *slang_function_scope_find (slang_function_scope *funcs, slang_function *fun,\r
- int all_scopes)\r
-{\r
- unsigned int i;\r
- for (i = 0; i < funcs->num_functions; i++)\r
- {\r
- slang_function *f = funcs->functions + i;\r
- unsigned int j;\r
- if (slang_string_compare (fun->header.name, f->header.name) != 0)\r
- continue;\r
- if (fun->param_count != f->param_count)\r
- continue;\r
- for (j = 0; j < fun->param_count; j++)\r
- {\r
- if (!slang_type_specifier_equal (&fun->parameters->variables[j].type.specifier,\r
- &f->parameters->variables[j].type.specifier))\r
- {\r
- break;\r
- }\r
- }\r
- if (j == fun->param_count)\r
- return f;\r
- }\r
- if (all_scopes && funcs->outer_scope != NULL)\r
- return slang_function_scope_find (funcs->outer_scope, fun, 1);\r
- return NULL;\r
-}\r
-\r
-/* slang_translation_unit */\r
-/* XXX mem! */\r
-static void slang_translation_unit_construct (slang_translation_unit *unit)\r
-{\r
- unit->globals = (slang_variable_scope *) slang_alloc_malloc (sizeof (slang_variable_scope));\r
- slang_variable_scope_construct (unit->globals);\r
- slang_function_scope_construct (&unit->functions);\r
- unit->structs = (slang_struct_scope *) slang_alloc_malloc (sizeof (slang_struct_scope));\r
- slang_struct_scope_construct (unit->structs);\r
-}\r
-\r
-static void slang_translation_unit_destruct (slang_translation_unit *unit)\r
-{\r
- slang_variable_scope_destruct (unit->globals);\r
- slang_alloc_free (unit->globals);\r
- slang_function_scope_destruct (&unit->functions);\r
- slang_struct_scope_destruct (unit->structs);\r
- slang_alloc_free (unit->structs);\r
-}\r
-\r
-static int parse_identifier (const byte **I, char **id)\r
-{\r
- *id = slang_string_duplicate ((const char *) *I);\r
- if (*id == NULL)\r
- return 0;\r
- *I += strlen ((const char *) *I) + 1;\r
- return 1;\r
-}\r
-\r
-static int parse_number (const byte **I, int *number)\r
-{\r
- const int radix = (int) (*(*I)++);\r
- *number = 0;\r
- while (**I != '\0')\r
- {\r
- int digit;\r
- if (**I >= '0' && **I <= '9')\r
- digit = (int) (**I - '0');\r
- else if (**I >= 'A' && **I <= 'Z')\r
- digit = (int) (**I - 'A') + 10;\r
- else\r
- digit = (int) (**I - 'a') + 10;\r
- *number = *number * radix + digit;\r
- (*I)++;\r
- }\r
- (*I)++;\r
- return 1;\r
-}\r
-\r
-static int parse_float (const byte **I, float *number)\r
-{\r
- char *integral = NULL;\r
- char *fractional = NULL;\r
- char *exponent = NULL;\r
- char *whole = NULL;\r
-\r
- if (!parse_identifier (I, &integral))\r
- return 0;\r
-\r
- if (!parse_identifier (I, &fractional))\r
- {\r
- slang_alloc_free (integral);\r
- return 0;\r
- }\r
-\r
- if (!parse_identifier (I, &exponent))\r
- {\r
- slang_alloc_free (fractional);\r
- slang_alloc_free (integral);\r
- return 0;\r
- }\r
-\r
- whole = (char *) (slang_alloc_malloc ((strlen (integral) + strlen (fractional) + strlen (\r
- exponent) + 3) * sizeof (char)));\r
- if (whole == NULL)\r
- {\r
- slang_alloc_free (exponent);\r
- slang_alloc_free (fractional);\r
- slang_alloc_free (integral);\r
- return 0;\r
- }\r
-\r
- slang_string_copy (whole, integral);\r
- slang_string_concat (whole, ".");\r
- slang_string_concat (whole, fractional);\r
- slang_string_concat (whole, "E");\r
- slang_string_concat (whole, exponent);\r
-\r
- *number = (float) (atof (whole));\r
-\r
- slang_alloc_free (whole);\r
- slang_alloc_free (exponent);\r
- slang_alloc_free (fractional);\r
- slang_alloc_free (integral);\r
- return 1;\r
-}\r
-\r
-/* revision number - increment after each change affecting emitted output */\r
-#define REVISION 2\r
-\r
-static int check_revision (const byte **I)\r
-{\r
- if (**I != REVISION)\r
- return 0;\r
- (*I)++;\r
- return 1;\r
-}\r
-\r
-static int parse_statement (const byte **, slang_operation *, slang_variable_scope *,\r
- slang_struct_scope *);\r
-static int parse_expression (const byte **, slang_operation *, slang_variable_scope *,\r
- slang_struct_scope *);\r
-\r
-/* type qualifier */\r
-#define TYPE_QUALIFIER_NONE 0\r
-#define TYPE_QUALIFIER_CONST 1\r
-#define TYPE_QUALIFIER_ATTRIBUTE 2\r
-#define TYPE_QUALIFIER_VARYING 3\r
-#define TYPE_QUALIFIER_UNIFORM 4\r
-#define TYPE_QUALIFIER_FIXEDOUTPUT 5\r
-#define TYPE_QUALIFIER_FIXEDINPUT 6\r
-\r
-static int parse_type_qualifier (const byte **I, slang_type_qualifier *qual)\r
-{\r
- switch (*(*I)++)\r
- {\r
- case TYPE_QUALIFIER_NONE:\r
- *qual = slang_qual_none;\r
- break;\r
- case TYPE_QUALIFIER_CONST:\r
- *qual = slang_qual_const;\r
- break;\r
- case TYPE_QUALIFIER_ATTRIBUTE:\r
- *qual = slang_qual_attribute;\r
- break;\r
- case TYPE_QUALIFIER_VARYING:\r
- *qual = slang_qual_varying;\r
- break;\r
- case TYPE_QUALIFIER_UNIFORM:\r
- *qual = slang_qual_uniform;\r
- break;\r
- case TYPE_QUALIFIER_FIXEDOUTPUT:\r
- *qual = slang_qual_fixedoutput;\r
- break;\r
- case TYPE_QUALIFIER_FIXEDINPUT:\r
- *qual = slang_qual_fixedinput;\r
- break;\r
- default:\r
- return 0;\r
- }\r
- return 1;\r
-}\r
-\r
-/* type specifier */\r
-#define TYPE_SPECIFIER_VOID 0\r
-#define TYPE_SPECIFIER_BOOL 1\r
-#define TYPE_SPECIFIER_BVEC2 2\r
-#define TYPE_SPECIFIER_BVEC3 3\r
-#define TYPE_SPECIFIER_BVEC4 4\r
-#define TYPE_SPECIFIER_INT 5\r
-#define TYPE_SPECIFIER_IVEC2 6\r
-#define TYPE_SPECIFIER_IVEC3 7\r
-#define TYPE_SPECIFIER_IVEC4 8\r
-#define TYPE_SPECIFIER_FLOAT 9\r
-#define TYPE_SPECIFIER_VEC2 10\r
-#define TYPE_SPECIFIER_VEC3 11\r
-#define TYPE_SPECIFIER_VEC4 12\r
-#define TYPE_SPECIFIER_MAT2 13\r
-#define TYPE_SPECIFIER_MAT3 14\r
-#define TYPE_SPECIFIER_MAT4 15\r
-#define TYPE_SPECIFIER_SAMPLER1D 16\r
-#define TYPE_SPECIFIER_SAMPLER2D 17\r
-#define TYPE_SPECIFIER_SAMPLER3D 18\r
-#define TYPE_SPECIFIER_SAMPLERCUBE 19\r
-#define TYPE_SPECIFIER_SAMPLER1DSHADOW 20\r
-#define TYPE_SPECIFIER_SAMPLER2DSHADOW 21\r
-#define TYPE_SPECIFIER_STRUCT 22\r
-#define TYPE_SPECIFIER_TYPENAME 23\r
-\r
-/* structure field */\r
-#define FIELD_NONE 0\r
-#define FIELD_NEXT 1\r
-#define FIELD_ARRAY 2\r
-\r
-static int parse_type_specifier (const byte **I, slang_type_specifier *spec,\r
- slang_struct_scope *structs, slang_variable_scope *scope)\r
-{\r
- switch (*(*I)++)\r
- {\r
- case TYPE_SPECIFIER_VOID:\r
- spec->type = slang_spec_void;\r
- break;\r
- case TYPE_SPECIFIER_BOOL:\r
- spec->type = slang_spec_bool;\r
- break;\r
- case TYPE_SPECIFIER_BVEC2:\r
- spec->type = slang_spec_bvec2;\r
- break;\r
- case TYPE_SPECIFIER_BVEC3:\r
- spec->type = slang_spec_bvec3;\r
- break;\r
- case TYPE_SPECIFIER_BVEC4:\r
- spec->type = slang_spec_bvec4;\r
- break;\r
- case TYPE_SPECIFIER_INT:\r
- spec->type = slang_spec_int;\r
- break;\r
- case TYPE_SPECIFIER_IVEC2:\r
- spec->type = slang_spec_ivec2;\r
- break;\r
- case TYPE_SPECIFIER_IVEC3:\r
- spec->type = slang_spec_ivec3;\r
- break;\r
- case TYPE_SPECIFIER_IVEC4:\r
- spec->type = slang_spec_ivec4;\r
- break;\r
- case TYPE_SPECIFIER_FLOAT:\r
- spec->type = slang_spec_float;\r
- break;\r
- case TYPE_SPECIFIER_VEC2:\r
- spec->type = slang_spec_vec2;\r
- break;\r
- case TYPE_SPECIFIER_VEC3:\r
- spec->type = slang_spec_vec3;\r
- break;\r
- case TYPE_SPECIFIER_VEC4:\r
- spec->type = slang_spec_vec4;\r
- break;\r
- case TYPE_SPECIFIER_MAT2:\r
- spec->type = slang_spec_mat2;\r
- break;\r
- case TYPE_SPECIFIER_MAT3:\r
- spec->type = slang_spec_mat3;\r
- break;\r
- case TYPE_SPECIFIER_MAT4:\r
- spec->type = slang_spec_mat4;\r
- break;\r
- case TYPE_SPECIFIER_SAMPLER1D:\r
- spec->type = slang_spec_sampler1D;\r
- break;\r
- case TYPE_SPECIFIER_SAMPLER2D:\r
- spec->type = slang_spec_sampler2D;\r
- break;\r
- case TYPE_SPECIFIER_SAMPLER3D:\r
- spec->type = slang_spec_sampler3D;\r
- break;\r
- case TYPE_SPECIFIER_SAMPLERCUBE:\r
- spec->type = slang_spec_samplerCube;\r
- break;\r
- case TYPE_SPECIFIER_SAMPLER1DSHADOW:\r
- spec->type = slang_spec_sampler1DShadow;\r
- break;\r
- case TYPE_SPECIFIER_SAMPLER2DSHADOW:\r
- spec->type = slang_spec_sampler2DShadow;\r
- break;\r
- case TYPE_SPECIFIER_STRUCT:\r
- spec->type = slang_spec_struct;\r
- {\r
- char *name;\r
- if (!parse_identifier (I, &name))\r
- return 0;\r
- if (*name != '\0' && slang_struct_scope_find (structs, name, 0) != NULL)\r
- {\r
- slang_alloc_free (name);\r
- return 0; /* error: duplicate names */\r
- }\r
- spec->_struct = (slang_struct *) slang_alloc_malloc (sizeof (slang_struct));\r
- if (spec->_struct == NULL)\r
- {\r
- slang_alloc_free (name);\r
- return 0;\r
- }\r
- slang_struct_construct (spec->_struct);\r
- spec->_struct->name = name;\r
- spec->_struct->structs->outer_scope = structs;\r
- }\r
- do\r
- {\r
- slang_type_specifier sp;\r
- slang_type_specifier_construct (&sp);\r
- if (!parse_type_specifier (I, &sp, spec->_struct->structs, scope))\r
- {\r
- slang_type_specifier_destruct (&sp);\r
- return 0;\r
- }\r
- do\r
- {\r
- slang_variable *var;\r
- spec->_struct->fields->variables = (slang_variable *) slang_alloc_realloc (\r
- spec->_struct->fields->variables,\r
- spec->_struct->fields->num_variables * sizeof (slang_variable),\r
- (spec->_struct->fields->num_variables + 1) * sizeof (slang_variable));\r
- if (spec->_struct->fields->variables == NULL)\r
- {\r
- slang_type_specifier_destruct (&sp);\r
- return 0;\r
- }\r
- var = spec->_struct->fields->variables + spec->_struct->fields->num_variables;\r
- spec->_struct->fields->num_variables++;\r
- slang_variable_construct (var);\r
- if (!slang_type_specifier_copy (&var->type.specifier, &sp))\r
- {\r
- slang_type_specifier_destruct (&sp);\r
- return 0;\r
- }\r
- if (!parse_identifier (I, &var->name))\r
- {\r
- slang_type_specifier_destruct (&sp);\r
- return 0;\r
- }\r
- switch (*(*I)++)\r
- {\r
- case FIELD_NONE:\r
- break;\r
- case FIELD_ARRAY:\r
- var->array_size = (slang_operation *) slang_alloc_malloc (sizeof (\r
- slang_operation));\r
- if (var->array_size == NULL)\r
- {\r
- slang_type_specifier_destruct (&sp);\r
- return 0;\r
- }\r
- slang_operation_construct (var->array_size);\r
- if (!parse_expression (I, var->array_size, scope, structs))\r
- {\r
- slang_type_specifier_destruct (&sp);\r
- return 0;\r
- }\r
- break;\r
- default:\r
- slang_type_specifier_destruct (&sp);\r
- return 0;\r
- }\r
- }\r
- while (*(*I)++ != FIELD_NONE);\r
- }\r
- while (*(*I)++ != FIELD_NONE);\r
- if (*spec->_struct->name != '\0')\r
- {\r
- slang_struct *s;\r
- structs->structs = (slang_struct *) slang_alloc_realloc (structs->structs,\r
- structs->num_structs * sizeof (slang_struct),\r
- (structs->num_structs + 1) * sizeof (slang_struct));\r
- if (structs->structs == NULL)\r
- return 0;\r
- s = structs->structs + structs->num_structs;\r
- structs->num_structs++;\r
- slang_struct_construct (s);\r
- if (!slang_struct_copy (s, spec->_struct))\r
- return 0;\r
- }\r
- break;\r
- case TYPE_SPECIFIER_TYPENAME:\r
- spec->type = slang_spec_struct;\r
- {\r
- char *name;\r
- slang_struct *stru;\r
- if (!parse_identifier (I, &name))\r
- return 0;\r
- stru = slang_struct_scope_find (structs, name, 1);\r
- slang_alloc_free (name);\r
- if (stru == NULL)\r
- return 0;\r
- spec->_struct = (slang_struct *) slang_alloc_malloc (sizeof (slang_struct));\r
- if (spec->_struct == NULL)\r
- return 0;\r
- slang_struct_construct (spec->_struct);\r
- if (!slang_struct_copy (spec->_struct, stru))\r
- return 0;\r
- }\r
- break;\r
- default:\r
- return 0;\r
- }\r
- return 1;\r
-}\r
-\r
-static int parse_fully_specified_type (const byte **I, slang_fully_specified_type *type,\r
- slang_struct_scope *structs, slang_variable_scope *scope)\r
-{\r
- if (!parse_type_qualifier (I, &type->qualifier))\r
- return 0;\r
- return parse_type_specifier (I, &type->specifier, structs, scope);\r
-}\r
-\r
-/* operation */\r
-#define OP_END 0\r
-#define OP_BLOCK_BEGIN_NO_NEW_SCOPE 1\r
-#define OP_BLOCK_BEGIN_NEW_SCOPE 2\r
-#define OP_DECLARE 3\r
-#define OP_ASM 4\r
-#define OP_BREAK 5\r
-#define OP_CONTINUE 6\r
-#define OP_DISCARD 7\r
-#define OP_RETURN 8\r
-#define OP_EXPRESSION 9\r
-#define OP_IF 10\r
-#define OP_WHILE 11\r
-#define OP_DO 12\r
-#define OP_FOR 13\r
-#define OP_PUSH_VOID 14\r
-#define OP_PUSH_BOOL 15\r
-#define OP_PUSH_INT 16\r
-#define OP_PUSH_FLOAT 17\r
-#define OP_PUSH_IDENTIFIER 18\r
-#define OP_SEQUENCE 19\r
-#define OP_ASSIGN 20\r
-#define OP_ADDASSIGN 21\r
-#define OP_SUBASSIGN 22\r
-#define OP_MULASSIGN 23\r
-#define OP_DIVASSIGN 24\r
-/*#define OP_MODASSIGN 25*/\r
-/*#define OP_LSHASSIGN 26*/\r
-/*#define OP_RSHASSIGN 27*/\r
-/*#define OP_ORASSIGN 28*/\r
-/*#define OP_XORASSIGN 29*/\r
-/*#define OP_ANDASSIGN 30*/\r
-#define OP_SELECT 31\r
-#define OP_LOGICALOR 32\r
-#define OP_LOGICALXOR 33\r
-#define OP_LOGICALAND 34\r
-/*#define OP_BITOR 35*/\r
-/*#define OP_BITXOR 36*/\r
-/*#define OP_BITAND 37*/\r
-#define OP_EQUAL 38\r
-#define OP_NOTEQUAL 39\r
-#define OP_LESS 40\r
-#define OP_GREATER 41\r
-#define OP_LESSEQUAL 42\r
-#define OP_GREATEREQUAL 43\r
-/*#define OP_LSHIFT 44*/\r
-/*#define OP_RSHIFT 45*/\r
-#define OP_ADD 46\r
-#define OP_SUBTRACT 47\r
-#define OP_MULTIPLY 48\r
-#define OP_DIVIDE 49\r
-/*#define OP_MODULUS 50*/\r
-#define OP_PREINCREMENT 51\r
-#define OP_PREDECREMENT 52\r
-#define OP_PLUS 53\r
-#define OP_MINUS 54\r
-/*#define OP_COMPLEMENT 55*/\r
-#define OP_NOT 56\r
-#define OP_SUBSCRIPT 57\r
-#define OP_CALL 58\r
-#define OP_FIELD 59\r
-#define OP_POSTINCREMENT 60\r
-#define OP_POSTDECREMENT 61\r
-\r
-static int parse_child_operation (const byte **I, slang_operation *oper, int statement,\r
- slang_variable_scope *scope, slang_struct_scope *structs)\r
-{\r
- oper->children = (slang_operation *) slang_alloc_realloc (oper->children,\r
- oper->num_children * sizeof (slang_operation),\r
- (oper->num_children + 1) * sizeof (slang_operation));\r
- if (oper->children == NULL)\r
- return 0;\r
- slang_operation_construct (oper->children + oper->num_children);\r
- oper->num_children++;\r
- if (statement)\r
- return parse_statement (I, oper->children + oper->num_children - 1, scope, structs);\r
- return parse_expression (I, oper->children + oper->num_children - 1, scope, structs);\r
-}\r
-\r
-static int parse_declaration (const byte **, slang_variable_scope *, slang_struct_scope *,\r
- slang_function_scope *);\r
-\r
-static int parse_statement (const byte **I, slang_operation *oper, slang_variable_scope *scope,\r
- slang_struct_scope *structs)\r
-{\r
- oper->locals->outer_scope = scope;\r
- switch (*(*I)++)\r
- {\r
- case OP_BLOCK_BEGIN_NO_NEW_SCOPE:\r
- oper->type = slang_oper_block_no_new_scope;\r
- while (**I != OP_END)\r
- if (!parse_child_operation (I, oper, 1, scope, structs))\r
- return 0;\r
- (*I)++;\r
- break;\r
- case OP_BLOCK_BEGIN_NEW_SCOPE:\r
- oper->type = slang_oper_block_new_scope;\r
- while (**I != OP_END)\r
- if (!parse_child_operation (I, oper, 1, oper->locals, structs))\r
- return 0;\r
- (*I)++;\r
- break;\r
- case OP_DECLARE:\r
- oper->type = slang_oper_variable_decl;\r
- {\r
- const unsigned int first_var = scope->num_variables;\r
- if (!parse_declaration (I, scope, structs, NULL))\r
- return 0;\r
- if (first_var < scope->num_variables)\r
- {\r
- const unsigned int num_vars = scope->num_variables - first_var;\r
- unsigned int i;\r
- oper->children = (slang_operation *) slang_alloc_malloc (num_vars * sizeof (\r
- slang_operation));\r
- if (oper->children == NULL)\r
- return 0;\r
- for (i = 0; i < num_vars; i++)\r
- slang_operation_construct (oper->children + i);\r
- oper->num_children = num_vars;\r
- for (i = first_var; i < scope->num_variables; i++)\r
- {\r
- slang_operation *o = oper->children + i - first_var;\r
- o->type = slang_oper_identifier;\r
- o->locals->outer_scope = scope;\r
- o->identifier = slang_string_duplicate (scope->variables[i].name);\r
- if (o->identifier == NULL)\r
- return 0;\r
- }\r
- }\r
- }\r
- break;\r
- case OP_ASM:\r
- oper->type = slang_oper_asm;\r
- if (!parse_identifier (I, &oper->identifier))\r
- return 0;\r
- while (**I != OP_END)\r
- if (!parse_child_operation (I, oper, 0, scope, structs))\r
- return 0;\r
- (*I)++;\r
- break;\r
- case OP_BREAK:\r
- oper->type = slang_oper_break;\r
- break;\r
- case OP_CONTINUE:\r
- oper->type = slang_oper_continue;\r
- break;\r
- case OP_DISCARD:\r
- oper->type = slang_oper_discard;\r
- break;\r
- case OP_RETURN:\r
- oper->type = slang_oper_return;\r
- if (!parse_child_operation (I, oper, 0, scope, structs))\r
- return 0;\r
- break;\r
- case OP_EXPRESSION:\r
- oper->type = slang_oper_expression;\r
- if (!parse_child_operation (I, oper, 0, scope, structs))\r
- return 0;\r
- break;\r
- case OP_IF:\r
- oper->type = slang_oper_if;\r
- if (!parse_child_operation (I, oper, 0, scope, structs))\r
- return 0;\r
- if (!parse_child_operation (I, oper, 1, scope, structs))\r
- return 0;\r
- if (!parse_child_operation (I, oper, 1, scope, structs))\r
- return 0;\r
- break;\r
- case OP_WHILE:\r
- oper->type = slang_oper_while;\r
- if (!parse_child_operation (I, oper, 1, oper->locals, structs))\r
- return 0;\r
- if (!parse_child_operation (I, oper, 1, oper->locals, structs))\r
- return 0;\r
- break;\r
- case OP_DO:\r
- oper->type = slang_oper_do;\r
- if (!parse_child_operation (I, oper, 1, scope, structs))\r
- return 0;\r
- if (!parse_child_operation (I, oper, 0, scope, structs))\r
- return 0;\r
- break;\r
- case OP_FOR:\r
- oper->type = slang_oper_for;\r
- if (!parse_child_operation (I, oper, 1, oper->locals, structs))\r
- return 0;\r
- if (!parse_child_operation (I, oper, 1, oper->locals, structs))\r
- return 0;\r
- if (!parse_child_operation (I, oper, 0, oper->locals, structs))\r
- return 0;\r
- if (!parse_child_operation (I, oper, 1, oper->locals, structs))\r
- return 0;\r
- break;\r
- default:\r
- return 0;\r
- }\r
- return 1;\r
-}\r
-\r
-static int handle_trinary_expression (slang_operation *op, slang_operation **ops,\r
- unsigned int *num_ops)\r
-{\r
- op->num_children = 3;\r
- op->children = (slang_operation *) slang_alloc_malloc (3 * sizeof (slang_operation));\r
- if (op->children == NULL)\r
- return 0;\r
- op->children[0] = (*ops)[*num_ops - 4];\r
- op->children[1] = (*ops)[*num_ops - 3];\r
- op->children[2] = (*ops)[*num_ops - 2];\r
- (*ops)[*num_ops - 4] = (*ops)[*num_ops - 1];\r
- *num_ops -= 3;\r
- *ops = (slang_operation *) slang_alloc_realloc (*ops, (*num_ops + 3) * sizeof (slang_operation),\r
- *num_ops * sizeof (slang_operation));\r
- if (*ops == NULL)\r
- return 0;\r
- return 1;\r
-}\r
-\r
-static int handle_binary_expression (slang_operation *op, slang_operation **ops,\r
- unsigned int *num_ops)\r
-{\r
- op->num_children = 2;\r
- op->children = (slang_operation *) slang_alloc_malloc (2 * sizeof (slang_operation));\r
- if (op->children == NULL)\r
- return 0;\r
- op->children[0] = (*ops)[*num_ops - 3];\r
- op->children[1] = (*ops)[*num_ops - 2];\r
- (*ops)[*num_ops - 3] = (*ops)[*num_ops - 1];\r
- *num_ops -= 2;\r
- *ops = (slang_operation *) slang_alloc_realloc (*ops, (*num_ops + 2) * sizeof (slang_operation),\r
- *num_ops * sizeof (slang_operation));\r
- if (*ops == NULL)\r
- return 0;\r
- return 1;\r
-}\r
-\r
-static int handle_unary_expression (slang_operation *op, slang_operation **ops, \r
- unsigned int *num_ops)\r
-{\r
- op->num_children = 1;\r
- op->children = (slang_operation *) slang_alloc_malloc (sizeof (slang_operation));\r
- if (op->children == NULL)\r
- return 0;\r
- op->children[0] = (*ops)[*num_ops - 2];\r
- (*ops)[*num_ops - 2] = (*ops)[*num_ops - 1];\r
- (*num_ops)--;\r
- *ops = (slang_operation *) slang_alloc_realloc (*ops, (*num_ops + 1) * sizeof (slang_operation),\r
- *num_ops * sizeof (slang_operation));\r
- if (*ops == NULL)\r
- return 0;\r
- return 1;\r
-}\r
-\r
-static int parse_expression (const byte **I, slang_operation *oper, slang_variable_scope *scope,\r
- slang_struct_scope *structs)\r
-{\r
- slang_operation *ops = NULL;\r
- unsigned int num_ops = 0;\r
- int number;\r
-\r
- while (**I != OP_END)\r
- {\r
- slang_operation *op;\r
- const unsigned int op_code = *(*I)++;\r
- ops = (slang_operation *) slang_alloc_realloc (ops,\r
- num_ops * sizeof (slang_operation), (num_ops + 1) * sizeof (slang_operation));\r
- if (ops == NULL)\r
- return 0;\r
- op = ops + num_ops;\r
- num_ops++;\r
- slang_operation_construct (op);\r
- op->locals->outer_scope = scope;\r
- switch (op_code)\r
- {\r
- case OP_PUSH_VOID:\r
- op->type = slang_oper_void;\r
- break;\r
- case OP_PUSH_BOOL:\r
- op->type = slang_oper_literal_bool;\r
- if (!parse_number (I, &number))\r
- return 0;\r
- op->literal = (float) number;\r
- break;\r
- case OP_PUSH_INT:\r
- op->type = slang_oper_literal_int;\r
- if (!parse_number (I, &number))\r
- return 0;\r
- op->literal = (float) number;\r
- break;\r
- case OP_PUSH_FLOAT:\r
- op->type = slang_oper_literal_float;\r
- if (!parse_float (I, &op->literal))\r
- return 0;\r
- break;\r
- case OP_PUSH_IDENTIFIER:\r
- op->type = slang_oper_identifier;\r
- if (!parse_identifier (I, &op->identifier))\r
- return 0;\r
- break;\r
- case OP_SEQUENCE:\r
- op->type = slang_oper_sequence;\r
- if (!handle_binary_expression (op, &ops, &num_ops))\r
- return 0;\r
- break;\r
- case OP_ASSIGN:\r
- op->type = slang_oper_assign;\r
- if (!handle_binary_expression (op, &ops, &num_ops))\r
- return 0;\r
- break;\r
- case OP_ADDASSIGN:\r
- op->type = slang_oper_addassign;\r
- if (!handle_binary_expression (op, &ops, &num_ops))\r
- return 0;\r
- break;\r
- case OP_SUBASSIGN:\r
- op->type = slang_oper_subassign;\r
- if (!handle_binary_expression (op, &ops, &num_ops))\r
- return 0;\r
- break;\r
- case OP_MULASSIGN:\r
- op->type = slang_oper_mulassign;\r
- if (!handle_binary_expression (op, &ops, &num_ops))\r
- return 0;\r
- break;\r
- case OP_DIVASSIGN:\r
- op->type = slang_oper_divassign;\r
- if (!handle_binary_expression (op, &ops, &num_ops))\r
- return 0;\r
- break;\r
- /*case OP_MODASSIGN:*/\r
- /*case OP_LSHASSIGN:*/\r
- /*case OP_RSHASSIGN:*/\r
- /*case OP_ORASSIGN:*/\r
- /*case OP_XORASSIGN:*/\r
- /*case OP_ANDASSIGN:*/\r
- case OP_SELECT:\r
- op->type = slang_oper_select;\r
- if (!handle_trinary_expression (op, &ops, &num_ops))\r
- return 0;\r
- break;\r
- case OP_LOGICALOR:\r
- op->type = slang_oper_logicalor;\r
- if (!handle_binary_expression (op, &ops, &num_ops))\r
- return 0;\r
- break;\r
- case OP_LOGICALXOR:\r
- op->type = slang_oper_logicalxor;\r
- if (!handle_binary_expression (op, &ops, &num_ops))\r
- return 0;\r
- break;\r
- case OP_LOGICALAND:\r
- op->type = slang_oper_logicaland;\r
- if (!handle_binary_expression (op, &ops, &num_ops))\r
- return 0;\r
- break;\r
- /*case OP_BITOR:*/\r
- /*case OP_BITXOR:*/\r
- /*case OP_BITAND:*/\r
- case OP_EQUAL:\r
- op->type = slang_oper_equal;\r
- if (!handle_binary_expression (op, &ops, &num_ops))\r
- return 0;\r
- break;\r
- case OP_NOTEQUAL:\r
- op->type = slang_oper_notequal;\r
- if (!handle_binary_expression (op, &ops, &num_ops))\r
- return 0;\r
- break;\r
- case OP_LESS:\r
- op->type = slang_oper_less;\r
- if (!handle_binary_expression (op, &ops, &num_ops))\r
- return 0;\r
- break;\r
- case OP_GREATER:\r
- op->type = slang_oper_greater;\r
- if (!handle_binary_expression (op, &ops, &num_ops))\r
- return 0;\r
- break;\r
- case OP_LESSEQUAL:\r
- op->type = slang_oper_lessequal;\r
- if (!handle_binary_expression (op, &ops, &num_ops))\r
- return 0;\r
- break;\r
- case OP_GREATEREQUAL:\r
- op->type = slang_oper_greaterequal;\r
- if (!handle_binary_expression (op, &ops, &num_ops))\r
- return 0;\r
- break;\r
- /*case OP_LSHIFT:*/\r
- /*case OP_RSHIFT:*/\r
- case OP_ADD:\r
- op->type = slang_oper_add;\r
- if (!handle_binary_expression (op, &ops, &num_ops))\r
- return 0;\r
- break;\r
- case OP_SUBTRACT:\r
- op->type = slang_oper_subtract;\r
- if (!handle_binary_expression (op, &ops, &num_ops))\r
- return 0;\r
- break;\r
- case OP_MULTIPLY:\r
- op->type = slang_oper_multiply;\r
- if (!handle_binary_expression (op, &ops, &num_ops))\r
- return 0;\r
- break;\r
- case OP_DIVIDE:\r
- op->type = slang_oper_divide;\r
- if (!handle_binary_expression (op, &ops, &num_ops))\r
- return 0;\r
- break;\r
- /*case OP_MODULUS:*/\r
- case OP_PREINCREMENT:\r
- op->type = slang_oper_preincrement;\r
- if (!handle_unary_expression (op, &ops, &num_ops))\r
- return 0;\r
- break;\r
- case OP_PREDECREMENT:\r
- op->type = slang_oper_predecrement;\r
- if (!handle_unary_expression (op, &ops, &num_ops))\r
- return 0;\r
- break;\r
- case OP_PLUS:\r
- op->type = slang_oper_plus;\r
- if (!handle_unary_expression (op, &ops, &num_ops))\r
- return 0;\r
- break;\r
- case OP_MINUS:\r
- op->type = slang_oper_minus;\r
- if (!handle_unary_expression (op, &ops, &num_ops))\r
- return 0;\r
- break;\r
- case OP_NOT:\r
- op->type = slang_oper_not;\r
- if (!handle_unary_expression (op, &ops, &num_ops))\r
- return 0;\r
- break;\r
- /*case OP_COMPLEMENT:*/\r
- case OP_SUBSCRIPT:\r
- op->type = slang_oper_subscript;\r
- if (!handle_binary_expression (op, &ops, &num_ops))\r
- return 0;\r
- break;\r
- case OP_CALL:\r
- op->type = slang_oper_call;\r
- if (!parse_identifier (I, &op->identifier))\r
- return 0;\r
- while (**I != OP_END)\r
- if (!parse_child_operation (I, op, 0, scope, structs))\r
- return 0;\r
- (*I)++;\r
- break;\r
- case OP_FIELD:\r
- op->type = slang_oper_field;\r
- if (!parse_identifier (I, &op->identifier))\r
- return 0;\r
- if (!handle_unary_expression (op, &ops, &num_ops))\r
- return 0;\r
- break;\r
- case OP_POSTINCREMENT:\r
- op->type = slang_oper_postincrement;\r
- if (!handle_unary_expression (op, &ops, &num_ops))\r
- return 0;\r
- break;\r
- case OP_POSTDECREMENT:\r
- op->type = slang_oper_postdecrement;\r
- if (!handle_unary_expression (op, &ops, &num_ops))\r
- return 0;\r
- break;\r
- default:\r
- return 0;\r
- }\r
- }\r
- (*I)++;\r
- *oper = *ops;\r
- slang_alloc_free (ops);\r
- return 1;\r
-}\r
-\r
-/* parameter qualifier */\r
-#define PARAM_QUALIFIER_IN 0\r
-#define PARAM_QUALIFIER_OUT 1\r
-#define PARAM_QUALIFIER_INOUT 2\r
-\r
-/* function parameter array presence */\r
-#define PARAMETER_ARRAY_NOT_PRESENT 0\r
-#define PARAMETER_ARRAY_PRESENT 1\r
-\r
-static int parse_parameter_declaration (const byte **I, slang_variable *param,\r
- slang_struct_scope *structs, slang_variable_scope *scope)\r
-{\r
- if (!parse_type_qualifier (I, ¶m->type.qualifier))\r
- return 0;\r
- switch (*(*I)++)\r
- {\r
- case PARAM_QUALIFIER_IN:\r
- if (param->type.qualifier != slang_qual_const && param->type.qualifier != slang_qual_none)\r
- return 0; /* error: type qualifier is invalid */\r
- break;\r
- case PARAM_QUALIFIER_OUT:\r
- if (param->type.qualifier == slang_qual_none)\r
- param->type.qualifier = slang_qual_out;\r
- else\r
- return 0; /* error: type qualifier is invalid */\r
- break;\r
- case PARAM_QUALIFIER_INOUT:\r
- if (param->type.qualifier == slang_qual_none)\r
- param->type.qualifier = slang_qual_inout;\r
- else\r
- return 0; /* error: type qualifier is invalid */\r
- break;\r
- default:\r
- return 0;\r
- }\r
- if (!parse_type_specifier (I, ¶m->type.specifier, structs, scope))\r
- return 0;\r
- if (!parse_identifier (I, ¶m->name))\r
- return 0;\r
- if (*(*I)++ == PARAMETER_ARRAY_PRESENT)\r
- {\r
- param->array_size = (slang_operation *) slang_alloc_malloc (sizeof (slang_operation));\r
- slang_operation_construct (param->array_size);\r
- if (!parse_expression (I, param->array_size, scope, structs))\r
- return 0;\r
- }\r
- return 1;\r
-}\r
-\r
-/* function type */\r
-#define FUNCTION_ORDINARY 0\r
-#define FUNCTION_CONSTRUCTOR 1\r
-#define FUNCTION_OPERATOR 2\r
-\r
-/* function parameter */\r
-#define PARAMETER_NONE 0\r
-#define PARAMETER_NEXT 1\r
-\r
-/* operator type */\r
-#define OPERATOR_ASSIGN 1\r
-#define OPERATOR_ADDASSIGN 2\r
-#define OPERATOR_SUBASSIGN 3\r
-#define OPERATOR_MULASSIGN 4\r
-#define OPERATOR_DIVASSIGN 5\r
-/*#define OPERATOR_MODASSIGN 6*/\r
-/*#define OPERATOR_LSHASSIGN 7*/\r
-/*#define OPERATOR_RSHASSIGN 8*/\r
-/*#define OPERATOR_ANDASSIGN 9*/\r
-/*#define OPERATOR_XORASSIGN 10*/\r
-/*#define OPERATOR_ORASSIGN 11*/\r
-#define OPERATOR_LOGICALXOR 12\r
-/*#define OPERATOR_BITOR 13*/\r
-/*#define OPERATOR_BITXOR 14*/\r
-/*#define OPERATOR_BITAND 15*/\r
-#define OPERATOR_EQUAL 16\r
-#define OPERATOR_NOTEQUAL 17\r
-#define OPERATOR_LESS 18\r
-#define OPERATOR_GREATER 19\r
-#define OPERATOR_LESSEQUAL 20\r
-#define OPERATOR_GREATEREQUAL 21\r
-/*#define OPERATOR_LSHIFT 22*/\r
-/*#define OPERATOR_RSHIFT 23*/\r
-#define OPERATOR_MULTIPLY 24\r
-#define OPERATOR_DIVIDE 25\r
-/*#define OPERATOR_MODULUS 26*/\r
-#define OPERATOR_INCREMENT 27\r
-#define OPERATOR_DECREMENT 28\r
-#define OPERATOR_PLUS 29\r
-#define OPERATOR_MINUS 30\r
-/*#define OPERATOR_COMPLEMENT 31*/\r
-#define OPERATOR_NOT 32\r
-\r
-/* these must match with slang_type_specifier_type enum */\r
-static const char *type_specifier_names[] = {\r
- "void",\r
- "bool",\r
- "bvec2",\r
- "bvec3",\r
- "bvec4",\r
- "int",\r
- "ivec2",\r
- "ivec3",\r
- "ivec4",\r
- "float",\r
- "vec2",\r
- "vec3",\r
- "vec4",\r
- "mat2",\r
- "mat3",\r
- "mat4",\r
- "sampler1D",\r
- "sampler2D",\r
- "sampler3D",\r
- "samplerCube",\r
- "sampler1DShadow",\r
- "sampler2DShadow"\r
-};\r
-\r
-static const struct {\r
- unsigned int o_code;\r
- const char *o_name;\r
-} operator_names[] = {\r
- { OPERATOR_INCREMENT, "++" },\r
- { OPERATOR_ADDASSIGN, "+=" },\r
- { OPERATOR_PLUS, "+" },\r
- { OPERATOR_DECREMENT, "--" },\r
- { OPERATOR_SUBASSIGN, "-=" },\r
- { OPERATOR_MINUS, "-" },\r
- { OPERATOR_NOTEQUAL, "!=" },\r
- { OPERATOR_NOT, "!" },\r
- { OPERATOR_MULASSIGN, "*=" },\r
- { OPERATOR_MULTIPLY, "*" },\r
- { OPERATOR_DIVASSIGN, "/=" },\r
- { OPERATOR_DIVIDE, "/" },\r
- { OPERATOR_LESSEQUAL, "<=" },\r
- /*{ OPERATOR_LSHASSIGN, "<<=" },*/\r
- /*{ OPERATOR_LSHIFT, "<<" },*/\r
- { OPERATOR_LESS, "<" },\r
- { OPERATOR_GREATEREQUAL, ">=" },\r
- /*{ OPERATOR_RSHASSIGN, ">>=" },*/\r
- /*{ OPERATOR_RSHIFT, ">>" },*/\r
- { OPERATOR_GREATER, ">" },\r
- { OPERATOR_EQUAL, "==" },\r
- { OPERATOR_ASSIGN, "=" },\r
- /*{ OPERATOR_MODASSIGN, "%=" },*/\r
- /*{ OPERATOR_MODULUS, "%" },*/\r
- /*{ OPERATOR_ANDASSIGN, "&=" },*/\r
- /*{ OPERATOR_BITAND, "&" },*/\r
- /*{ OPERATOR_ORASSIGN, "|=" },*/\r
- /*{ OPERATOR_BITOR, "|" },*/\r
- /*{ OPERATOR_COMPLEMENT, "~" },*/\r
- /*{ OPERATOR_XORASSIGN, "^=" },*/\r
- { OPERATOR_LOGICALXOR, "^^" }/*,*/\r
- /*{ OPERATOR_BITXOR, "^" }*/\r
-};\r
-\r
-static int parse_operator_name (const byte **I, char **pname)\r
-{\r
- unsigned int i;\r
- for (i = 0; i < sizeof (operator_names) / sizeof (*operator_names); i++)\r
- if (operator_names[i].o_code == (unsigned int) (**I))\r
- {\r
- *pname = slang_string_duplicate (operator_names[i].o_name);\r
- if (*pname == NULL)\r
- return 0;\r
- (*I)++;\r
- return 1;\r
- }\r
- return 0;\r
-}\r
-\r
-static int parse_function_prototype (const byte **I, slang_function *func,\r
- slang_struct_scope *structs, slang_variable_scope *scope)\r
-{\r
- if (!parse_fully_specified_type (I, &func->header.type, structs, scope))\r
- return 0;\r
- switch (*(*I)++)\r
- {\r
- case FUNCTION_ORDINARY:\r
- func->kind = slang_func_ordinary;\r
- if (!parse_identifier (I, &func->header.name))\r
- return 0;\r
- break;\r
- case FUNCTION_CONSTRUCTOR:\r
- func->kind = slang_func_constructor;\r
- if (func->header.type.specifier.type == slang_spec_struct)\r
- return 0;\r
- func->header.name = slang_string_duplicate (\r
- type_specifier_names[func->header.type.specifier.type]);\r
- if (func->header.name == NULL)\r
- return 0;\r
- break;\r
- case FUNCTION_OPERATOR:\r
- func->kind = slang_func_operator;\r
- if (!parse_operator_name (I, &func->header.name))\r
- return 0;\r
- break;\r
- default:\r
- return 0;\r
- }\r
- func->parameters->outer_scope = scope;\r
- while (*(*I)++ == PARAMETER_NEXT)\r
- {\r
- func->parameters->variables = (slang_variable *) slang_alloc_realloc (\r
- func->parameters->variables,\r
- func->parameters->num_variables * sizeof (slang_variable),\r
- (func->parameters->num_variables + 1) * sizeof (slang_variable));\r
- if (func->parameters->variables == NULL)\r
- return 0;\r
- slang_variable_construct (func->parameters->variables + func->parameters->num_variables);\r
- func->parameters->num_variables++;\r
- if (!parse_parameter_declaration (I, func->parameters->variables +\r
- func->parameters->num_variables - 1, structs, scope))\r
- return 0;\r
- }\r
- func->param_count = func->parameters->num_variables;\r
- return 1;\r
-}\r
-\r
-static int parse_function_definition (const byte **I, slang_function *func,\r
- slang_struct_scope *structs, slang_variable_scope *scope)\r
-{\r
- if (!parse_function_prototype (I, func, structs, scope))\r
- return 0;\r
- func->body = (slang_operation *) slang_alloc_malloc (sizeof (slang_operation));\r
- if (func->body == NULL)\r
- return 0;\r
- slang_operation_construct (func->body);\r
- if (!parse_statement (I, func->body, func->parameters, structs))\r
- return 0;\r
- return 1;\r
-}\r
-\r
-/* init declarator list */\r
-#define DECLARATOR_NONE 0\r
-#define DECLARATOR_NEXT 1\r
-\r
-/* variable declaration */\r
-#define VARIABLE_NONE 0\r
-#define VARIABLE_IDENTIFIER 1\r
-#define VARIABLE_INITIALIZER 2\r
-#define VARIABLE_ARRAY_EXPLICIT 3\r
-#define VARIABLE_ARRAY_UNKNOWN 4\r
-\r
-static int parse_init_declarator (const byte **I, const slang_fully_specified_type *type,\r
- slang_variable_scope *vars, slang_struct_scope *structs)\r
-{\r
- slang_variable *var;\r
-\r
- if (*(*I)++ == VARIABLE_NONE)\r
- return 1;\r
- vars->variables = (slang_variable *) slang_alloc_realloc (vars->variables,\r
- vars->num_variables * sizeof (slang_variable),\r
- (vars->num_variables + 1) * sizeof (slang_variable));\r
- if (vars->variables == NULL)\r
- return 0;\r
- var = vars->variables + vars->num_variables;\r
- vars->num_variables++;\r
- slang_variable_construct (var);\r
- var->type.qualifier = type->qualifier;\r
- if (!parse_identifier (I, &var->name))\r
- return 0;\r
- switch (*(*I)++)\r
- {\r
- case VARIABLE_NONE:\r
- if (!slang_type_specifier_copy (&var->type.specifier, &type->specifier))\r
- return 0;\r
- break;\r
- case VARIABLE_INITIALIZER:\r
- if (!slang_type_specifier_copy (&var->type.specifier, &type->specifier))\r
- return 0;\r
- var->initializer = (slang_operation *) slang_alloc_malloc (sizeof (slang_operation));\r
- if (var->initializer == NULL)\r
- return 0;\r
- slang_operation_construct (var->initializer);\r
- if (!parse_expression (I, var->initializer, vars, structs))\r
- return 0;\r
- break;\r
- case VARIABLE_ARRAY_UNKNOWN:\r
- var->type.specifier.type = slang_spec_array;\r
- var->type.specifier._array = (slang_type_specifier *) slang_alloc_malloc (sizeof (\r
- slang_type_specifier));\r
- if (var->type.specifier._array == NULL)\r
- return 0;\r
- slang_type_specifier_construct (var->type.specifier._array);\r
- if (!slang_type_specifier_copy (var->type.specifier._array, &type->specifier))\r
- return 0;\r
- break;\r
- case VARIABLE_ARRAY_EXPLICIT:\r
- var->type.specifier.type = slang_spec_array;\r
- var->type.specifier._array = (slang_type_specifier *) slang_alloc_malloc (sizeof (\r
- slang_type_specifier));\r
- if (var->type.specifier._array == NULL)\r
- return 0;\r
- slang_type_specifier_construct (var->type.specifier._array);\r
- if (!slang_type_specifier_copy (var->type.specifier._array, &type->specifier))\r
- return 0;\r
- var->array_size = (slang_operation *) slang_alloc_malloc (sizeof (slang_operation));\r
- if (var->array_size == NULL)\r
- return 0;\r
- slang_operation_construct (var->array_size);\r
- if (!parse_expression (I, var->array_size, vars, structs))\r
- return 0;\r
- break;\r
- default:\r
- return 0;\r
- }\r
- return 1;\r
-}\r
-\r
-static int parse_init_declarator_list (const byte **I, slang_variable_scope *vars,\r
- slang_struct_scope *structs)\r
-{\r
- slang_fully_specified_type type;\r
-\r
- slang_fully_specified_type_construct (&type);\r
- if (!parse_fully_specified_type (I, &type, structs, vars))\r
- {\r
- slang_fully_specified_type_destruct (&type);\r
- return 0;\r
- }\r
- do\r
- {\r
- if (!parse_init_declarator (I, &type, vars, structs))\r
- {\r
- slang_fully_specified_type_destruct (&type);\r
- return 0;\r
- }\r
- }\r
- while (*(*I)++ == DECLARATOR_NEXT);\r
- slang_fully_specified_type_destruct (&type);\r
- return 1;\r
-}\r
-\r
-static int parse_function (const byte **I, int definition, slang_struct_scope *structs,\r
- slang_function_scope *funcs, slang_variable_scope *scope)\r
-{\r
- slang_function parsed_func, *found_func;\r
-\r
- slang_function_construct (&parsed_func);\r
- if (definition)\r
- {\r
- if (!parse_function_definition (I, &parsed_func, structs, scope))\r
- {\r
- slang_function_destruct (&parsed_func);\r
- return 0;\r
- }\r
- }\r
- else\r
- {\r
- if (!parse_function_prototype (I, &parsed_func, structs, scope))\r
- {\r
- slang_function_destruct (&parsed_func);\r
- return 0;\r
- }\r
- }\r
- found_func = slang_function_scope_find (funcs, &parsed_func, 0);\r
- if (found_func == NULL)\r
- {\r
- funcs->functions = (slang_function *) slang_alloc_realloc (funcs->functions,\r
- funcs->num_functions * sizeof (slang_function), (funcs->num_functions + 1) * sizeof (\r
- slang_function));\r
- if (funcs->functions == NULL)\r
- {\r
- slang_function_destruct (&parsed_func);\r
- return 0;\r
- }\r
- funcs->functions[funcs->num_functions] = parsed_func;\r
- funcs->num_functions++;\r
- }\r
- else\r
- {\r
- /* TODO check function return type qualifiers and specifiers */\r
- if (definition)\r
- {\r
- if (found_func->body != NULL)\r
- {\r
- slang_function_destruct (&parsed_func);\r
- return 0; /* error: second definition */\r
- }\r
- slang_function_destruct (found_func);\r
- *found_func = parsed_func;\r
- }\r
- else\r
- {\r
- slang_function_destruct (&parsed_func);\r
- }\r
- }\r
- return 1;\r
-}\r
-\r
-/* declaration */\r
-#define DECLARATION_FUNCTION_PROTOTYPE 1\r
-#define DECLARATION_INIT_DECLARATOR_LIST 2\r
-\r
-static int parse_declaration (const byte **I, slang_variable_scope *scope,\r
- slang_struct_scope *structs, slang_function_scope *funcs)\r
-{\r
- switch (*(*I)++)\r
- {\r
- case DECLARATION_INIT_DECLARATOR_LIST:\r
- if (!parse_init_declarator_list (I, scope, structs))\r
- return 0;\r
- break;\r
- case DECLARATION_FUNCTION_PROTOTYPE:\r
- if (!parse_function (I, 0, structs, funcs, scope))\r
- return 0;\r
- break;\r
- default:\r
- return 0;\r
- }\r
- return 1;\r
-}\r
-\r
-/* external declaration */\r
-#define EXTERNAL_NULL 0\r
-#define EXTERNAL_FUNCTION_DEFINITION 1\r
-#define EXTERNAL_DECLARATION 2\r
-\r
-static int parse_translation_unit (const byte **I, slang_translation_unit *unit)\r
-{\r
- while (**I != EXTERNAL_NULL)\r
- {\r
- switch (*(*I)++)\r
- {\r
- case EXTERNAL_FUNCTION_DEFINITION:\r
- if (!parse_function (I, 1, unit->structs, &unit->functions, unit->globals))\r
- return 0;\r
- break;\r
- case EXTERNAL_DECLARATION:\r
- if (!parse_declaration (I, unit->globals, unit->structs, &unit->functions))\r
- return 0;\r
- break;\r
- default:\r
- return 0;\r
- }\r
- }\r
- (*I)++;\r
- return 1;\r
-}\r
-\r
-static int compile_with_grammar (grammar id, const char *source, slang_translation_unit *unit,\r
- slang_unit_type type)\r
-{\r
- byte *prod, *I;\r
- unsigned int size;\r
-\r
- slang_translation_unit_construct (unit);\r
- unit->type = type;\r
- if (!grammar_fast_check (id, (const byte *) source, &prod, &size, 65536))\r
- return 0;\r
- I = prod;\r
- if (!check_revision (&I))\r
- {\r
- grammar_alloc_free (prod);\r
- return 0;\r
- }\r
- if (!parse_translation_unit (&I, unit))\r
- {\r
- slang_translation_unit_destruct (unit);\r
- grammar_alloc_free (prod);\r
- return 0;\r
- }\r
- grammar_alloc_free (prod);\r
- return 1;\r
-}\r
-\r
-static const char *slang_shader_syn =\r
-#include "library/slang_shader_syn.h"\r
-;\r
-\r
-int _slang_compile (const char *source, slang_translation_unit *unit, slang_unit_type type)\r
-{\r
- grammar id;\r
-\r
- id = grammar_load_from_text ((const byte *) slang_shader_syn);\r
- if (id == 0)\r
- return 0;\r
-\r
- grammar_set_reg8 (id, (const byte *) "parsing_builtin", 1);\r
- if (type == slang_unit_fragment_shader)\r
- grammar_set_reg8 (id, (const byte *) "shader_type", 1);\r
- else\r
- grammar_set_reg8 (id, (const byte *) "shader_type", 2);\r
-\r
- if (!compile_with_grammar (id, source, unit, type))\r
- {\r
- grammar_destroy (id);\r
- return 0;\r
- }\r
-\r
- grammar_destroy (id);\r
- return 1;\r
-}\r
-\r
+/*
+ * Mesa 3-D graphics library
+ * Version: 6.5.2
+ *
+ * Copyright (C) 2005-2006 Brian Paul All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included
+ * in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
+ * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+/**
+ * \file slang_compile.c
+ * slang front-end compiler
+ * \author Michal Krol
+ */
+
+#include "imports.h"
+#include "context.h"
+#include "program.h"
+#include "prog_parameter.h"
+#include "grammar_mesa.h"
+#include "slang_codegen.h"
+#include "slang_compile.h"
+#include "slang_preprocess.h"
+#include "slang_storage.h"
+#include "slang_error.h"
+#include "slang_emit.h"
+#include "slang_vartable.h"
+
+#include "slang_print.h"
+
+/*
+ * This is a straightforward implementation of the slang front-end
+ * compiler. Lots of error-checking functionality is missing but
+ * every well-formed shader source should compile successfully and
+ * execute as expected. However, some semantically ill-formed shaders
+ * may be accepted resulting in undefined behaviour.
+ */
+
+
+
+/**
+ * Allocate storage for a variable of 'size' bytes from given pool.
+ * Return the allocated address for the variable.
+ */
+static GLuint
+slang_var_pool_alloc(slang_var_pool * pool, unsigned int size)
+{
+ const GLuint addr = pool->next_addr;
+ pool->next_addr += size;
+ return addr;
+}
+
+/*
+ * slang_code_unit
+ */
+
+GLvoid
+_slang_code_unit_ctr(slang_code_unit * self,
+ struct slang_code_object_ * object)
+{
+ _slang_variable_scope_ctr(&self->vars);
+ _slang_function_scope_ctr(&self->funs);
+ _slang_struct_scope_ctr(&self->structs);
+ self->object = object;
+}
+
+GLvoid
+_slang_code_unit_dtr(slang_code_unit * self)
+{
+ slang_variable_scope_destruct(&self->vars);
+ slang_function_scope_destruct(&self->funs);
+ slang_struct_scope_destruct(&self->structs);
+}
+
+/*
+ * slang_code_object
+ */
+
+GLvoid
+_slang_code_object_ctr(slang_code_object * self)
+{
+ GLuint i;
+
+ for (i = 0; i < SLANG_BUILTIN_TOTAL; i++)
+ _slang_code_unit_ctr(&self->builtin[i], self);
+ _slang_code_unit_ctr(&self->unit, self);
+#if 01
+ _slang_assembly_file_ctr(&self->assembly);
+#endif
+ slang_machine_ctr(&self->machine);
+ self->varpool.next_addr = 0;
+ slang_atom_pool_construct(&self->atompool);
+ slang_export_data_table_ctr(&self->expdata);
+ self->expdata.atoms = &self->atompool;
+ slang_export_code_table_ctr(&self->expcode);
+ self->expcode.atoms = &self->atompool;
+}
+
+GLvoid
+_slang_code_object_dtr(slang_code_object * self)
+{
+ GLuint i;
+
+ for (i = 0; i < SLANG_BUILTIN_TOTAL; i++)
+ _slang_code_unit_dtr(&self->builtin[i]);
+ _slang_code_unit_dtr(&self->unit);
+#if 01
+ slang_assembly_file_destruct(&self->assembly);
+#endif
+ slang_machine_dtr(&self->machine);
+ slang_atom_pool_destruct(&self->atompool);
+ slang_export_data_table_dtr(&self->expdata);
+ slang_export_code_table_ctr(&self->expcode);
+}
+
+/* slang_info_log */
+
+static char *out_of_memory = "Error: Out of memory.\n";
+
+void
+slang_info_log_construct(slang_info_log * log)
+{
+ log->text = NULL;
+ log->dont_free_text = 0;
+}
+
+void
+slang_info_log_destruct(slang_info_log * log)
+{
+ if (!log->dont_free_text)
+ slang_alloc_free(log->text);
+}
+
+static int
+slang_info_log_message(slang_info_log * log, const char *prefix,
+ const char *msg)
+{
+ GLuint size;
+
+ if (log->dont_free_text)
+ return 0;
+ size = slang_string_length(msg) + 2;
+ if (prefix != NULL)
+ size += slang_string_length(prefix) + 2;
+ if (log->text != NULL) {
+ GLuint old_len = slang_string_length(log->text);
+ log->text = (char *)
+ slang_alloc_realloc(log->text, old_len + 1, old_len + size);
+ }
+ else {
+ log->text = (char *) (slang_alloc_malloc(size));
+ if (log->text != NULL)
+ log->text[0] = '\0';
+ }
+ if (log->text == NULL)
+ return 0;
+ if (prefix != NULL) {
+ slang_string_concat(log->text, prefix);
+ slang_string_concat(log->text, ": ");
+ }
+ slang_string_concat(log->text, msg);
+ slang_string_concat(log->text, "\n");
+ return 1;
+}
+
+int
+slang_info_log_print(slang_info_log * log, const char *msg, ...)
+{
+ va_list va;
+ char buf[1024];
+
+ va_start(va, msg);
+ _mesa_vsprintf(buf, msg, va);
+ va_end(va);
+ return slang_info_log_message(log, NULL, buf);
+}
+
+int
+slang_info_log_error(slang_info_log * log, const char *msg, ...)
+{
+ va_list va;
+ char buf[1024];
+
+ va_start(va, msg);
+ _mesa_vsprintf(buf, msg, va);
+ va_end(va);
+ if (slang_info_log_message(log, "Error", buf))
+ return 1;
+ slang_info_log_memory(log);
+ return 0;
+}
+
+int
+slang_info_log_warning(slang_info_log * log, const char *msg, ...)
+{
+ va_list va;
+ char buf[1024];
+
+ va_start(va, msg);
+ _mesa_vsprintf(buf, msg, va);
+ va_end(va);
+ if (slang_info_log_message(log, "Warning", buf))
+ return 1;
+ slang_info_log_memory(log);
+ return 0;
+}
+
+void
+slang_info_log_memory(slang_info_log * log)
+{
+ if (!slang_info_log_message(log, "Error", "Out of memory.")) {
+ log->dont_free_text = 1;
+ log->text = out_of_memory;
+ }
+ abort();
+}
+
+/* slang_parse_ctx */
+
+typedef struct slang_parse_ctx_
+{
+ const byte *I;
+ slang_info_log *L;
+ int parsing_builtin;
+ GLboolean global_scope; /**< Is object being declared a global? */
+ slang_atom_pool *atoms;
+ slang_unit_type type; /**< Vertex vs. Fragment */
+} slang_parse_ctx;
+
+/* slang_output_ctx */
+
+typedef struct slang_output_ctx_
+{
+ slang_variable_scope *vars;
+ slang_function_scope *funs;
+ slang_struct_scope *structs;
+ slang_assembly_file *assembly;
+ slang_var_pool *global_pool;
+ slang_machine *machine;
+ struct gl_program *program;
+ slang_var_table *vartable;
+} slang_output_ctx;
+
+/* _slang_compile() */
+
+static void
+parse_identifier_str(slang_parse_ctx * C, char **id)
+{
+ *id = (char *) C->I;
+ C->I += _mesa_strlen(*id) + 1;
+}
+
+static slang_atom
+parse_identifier(slang_parse_ctx * C)
+{
+ const char *id;
+
+ id = (const char *) C->I;
+ C->I += _mesa_strlen(id) + 1;
+ return slang_atom_pool_atom(C->atoms, id);
+}
+
+static int
+parse_number(slang_parse_ctx * C, int *number)
+{
+ const int radix = (int) (*C->I++);
+ *number = 0;
+ while (*C->I != '\0') {
+ int digit;
+ if (*C->I >= '0' && *C->I <= '9')
+ digit = (int) (*C->I - '0');
+ else if (*C->I >= 'A' && *C->I <= 'Z')
+ digit = (int) (*C->I - 'A') + 10;
+ else
+ digit = (int) (*C->I - 'a') + 10;
+ *number = *number * radix + digit;
+ C->I++;
+ }
+ C->I++;
+ if (*number > 65535)
+ slang_info_log_warning(C->L, "%d: literal integer overflow.", *number);
+ return 1;
+}
+
+static int
+parse_float(slang_parse_ctx * C, float *number)
+{
+ char *integral = NULL;
+ char *fractional = NULL;
+ char *exponent = NULL;
+ char *whole = NULL;
+
+ parse_identifier_str(C, &integral);
+ parse_identifier_str(C, &fractional);
+ parse_identifier_str(C, &exponent);
+
+ whole = (char *) (slang_alloc_malloc((_mesa_strlen(integral) +
+ _mesa_strlen(fractional) +
+ _mesa_strlen(exponent) + 3) * sizeof(char)));
+ if (whole == NULL) {
+ slang_info_log_memory(C->L);
+ return 0;
+ }
+
+ slang_string_copy(whole, integral);
+ slang_string_concat(whole, ".");
+ slang_string_concat(whole, fractional);
+ slang_string_concat(whole, "E");
+ slang_string_concat(whole, exponent);
+
+ *number = (float) (_mesa_strtod(whole, (char **) NULL));
+
+ slang_alloc_free(whole);
+ return 1;
+}
+
+/* revision number - increment after each change affecting emitted output */
+#define REVISION 3
+
+static int
+check_revision(slang_parse_ctx * C)
+{
+ if (*C->I != REVISION) {
+ slang_info_log_error(C->L, "Internal compiler error.");
+ return 0;
+ }
+ C->I++;
+ return 1;
+}
+
+static int parse_statement(slang_parse_ctx *, slang_output_ctx *,
+ slang_operation *);
+static int parse_expression(slang_parse_ctx *, slang_output_ctx *,
+ slang_operation *);
+static int parse_type_specifier(slang_parse_ctx *, slang_output_ctx *,
+ slang_type_specifier *);
+
+static GLboolean
+parse_array_len(slang_parse_ctx * C, slang_output_ctx * O, GLuint * len)
+{
+ slang_operation array_size;
+ slang_assembly_name_space space;
+ GLboolean result;
+
+ if (!slang_operation_construct(&array_size))
+ return GL_FALSE;
+ if (!parse_expression(C, O, &array_size)) {
+ slang_operation_destruct(&array_size);
+ return GL_FALSE;
+ }
+
+ space.funcs = O->funs;
+ space.structs = O->structs;
+ space.vars = O->vars;
+ result = _slang_evaluate_int(O->assembly, O->machine, &space,
+ &array_size, len, C->atoms);
+ slang_operation_destruct(&array_size);
+ return result;
+}
+
+static GLboolean
+calculate_var_size(slang_parse_ctx * C, slang_output_ctx * O,
+ slang_variable * var)
+{
+ slang_storage_aggregate agg;
+
+ if (!slang_storage_aggregate_construct(&agg))
+ return GL_FALSE;
+ if (!_slang_aggregate_variable(&agg, &var->type.specifier, var->array_len,
+ O->funs, O->structs, O->vars, O->machine,
+ O->assembly, C->atoms)) {
+ slang_storage_aggregate_destruct(&agg);
+ return GL_FALSE;
+ }
+ var->size = _slang_sizeof_aggregate(&agg);
+ slang_storage_aggregate_destruct(&agg);
+ return GL_TRUE;
+}
+
+static GLboolean
+convert_to_array(slang_parse_ctx * C, slang_variable * var,
+ const slang_type_specifier * sp)
+{
+ /* sized array - mark it as array, copy the specifier to the array element and
+ * parse the expression */
+ var->type.specifier.type = slang_spec_array;
+ var->type.specifier._array = (slang_type_specifier *)
+ slang_alloc_malloc(sizeof(slang_type_specifier));
+ if (var->type.specifier._array == NULL) {
+ slang_info_log_memory(C->L);
+ return GL_FALSE;
+ }
+ slang_type_specifier_ctr(var->type.specifier._array);
+ return slang_type_specifier_copy(var->type.specifier._array, sp);
+}
+
+/* structure field */
+#define FIELD_NONE 0
+#define FIELD_NEXT 1
+#define FIELD_ARRAY 2
+
+static GLboolean
+parse_struct_field_var(slang_parse_ctx * C, slang_output_ctx * O,
+ slang_variable * var, const slang_type_specifier * sp)
+{
+ var->a_name = parse_identifier(C);
+ if (var->a_name == SLANG_ATOM_NULL)
+ return GL_FALSE;
+
+ switch (*C->I++) {
+ case FIELD_NONE:
+ if (!slang_type_specifier_copy(&var->type.specifier, sp))
+ return GL_FALSE;
+ break;
+ case FIELD_ARRAY:
+ if (!convert_to_array(C, var, sp))
+ return GL_FALSE;
+ if (!parse_array_len(C, O, &var->array_len))
+ return GL_FALSE;
+ break;
+ default:
+ return GL_FALSE;
+ }
+
+ return calculate_var_size(C, O, var);
+}
+
+static int
+parse_struct_field(slang_parse_ctx * C, slang_output_ctx * O,
+ slang_struct * st, slang_type_specifier * sp)
+{
+ slang_output_ctx o = *O;
+
+ o.structs = st->structs;
+ if (!parse_type_specifier(C, &o, sp))
+ return 0;
+
+ do {
+ slang_variable *var = slang_variable_scope_grow(st->fields);
+ if (!var) {
+ slang_info_log_memory(C->L);
+ return 0;
+ }
+ if (!parse_struct_field_var(C, &o, var, sp))
+ return 0;
+ }
+ while (*C->I++ != FIELD_NONE);
+
+ return 1;
+}
+
+static int
+parse_struct(slang_parse_ctx * C, slang_output_ctx * O, slang_struct ** st)
+{
+ slang_atom a_name;
+ const char *name;
+
+ /* parse struct name (if any) and make sure it is unique in current scope */
+ a_name = parse_identifier(C);
+ if (a_name == SLANG_ATOM_NULL)
+ return 0;
+
+ name = slang_atom_pool_id(C->atoms, a_name);
+ if (name[0] != '\0'
+ && slang_struct_scope_find(O->structs, a_name, 0) != NULL) {
+ slang_info_log_error(C->L, "%s: duplicate type name.", name);
+ return 0;
+ }
+
+ /* set-up a new struct */
+ *st = (slang_struct *) slang_alloc_malloc(sizeof(slang_struct));
+ if (*st == NULL) {
+ slang_info_log_memory(C->L);
+ return 0;
+ }
+ if (!slang_struct_construct(*st)) {
+ slang_alloc_free(*st);
+ *st = NULL;
+ slang_info_log_memory(C->L);
+ return 0;
+ }
+ (**st).a_name = a_name;
+ (**st).structs->outer_scope = O->structs;
+
+ /* parse individual struct fields */
+ do {
+ slang_type_specifier sp;
+
+ slang_type_specifier_ctr(&sp);
+ if (!parse_struct_field(C, O, *st, &sp)) {
+ slang_type_specifier_dtr(&sp);
+ return 0;
+ }
+ slang_type_specifier_dtr(&sp);
+ }
+ while (*C->I++ != FIELD_NONE);
+
+ /* if named struct, copy it to current scope */
+ if (name[0] != '\0') {
+ slang_struct *s;
+
+ O->structs->structs =
+ (slang_struct *) slang_alloc_realloc(O->structs->structs,
+ O->structs->num_structs *
+ sizeof(slang_struct),
+ (O->structs->num_structs +
+ 1) * sizeof(slang_struct));
+ if (O->structs->structs == NULL) {
+ slang_info_log_memory(C->L);
+ return 0;
+ }
+ s = &O->structs->structs[O->structs->num_structs];
+ if (!slang_struct_construct(s))
+ return 0;
+ O->structs->num_structs++;
+ if (!slang_struct_copy(s, *st))
+ return 0;
+ }
+
+ return 1;
+}
+
+
+/* type qualifier */
+#define TYPE_QUALIFIER_NONE 0
+#define TYPE_QUALIFIER_CONST 1
+#define TYPE_QUALIFIER_ATTRIBUTE 2
+#define TYPE_QUALIFIER_VARYING 3
+#define TYPE_QUALIFIER_UNIFORM 4
+#define TYPE_QUALIFIER_FIXEDOUTPUT 5
+#define TYPE_QUALIFIER_FIXEDINPUT 6
+
+static int
+parse_type_qualifier(slang_parse_ctx * C, slang_type_qualifier * qual)
+{
+ switch (*C->I++) {
+ case TYPE_QUALIFIER_NONE:
+ *qual = slang_qual_none;
+ break;
+ case TYPE_QUALIFIER_CONST:
+ *qual = slang_qual_const;
+ break;
+ case TYPE_QUALIFIER_ATTRIBUTE:
+ *qual = slang_qual_attribute;
+ break;
+ case TYPE_QUALIFIER_VARYING:
+ *qual = slang_qual_varying;
+ break;
+ case TYPE_QUALIFIER_UNIFORM:
+ *qual = slang_qual_uniform;
+ break;
+ case TYPE_QUALIFIER_FIXEDOUTPUT:
+ *qual = slang_qual_fixedoutput;
+ break;
+ case TYPE_QUALIFIER_FIXEDINPUT:
+ *qual = slang_qual_fixedinput;
+ break;
+ default:
+ return 0;
+ }
+ return 1;
+}
+
+/* type specifier */
+#define TYPE_SPECIFIER_VOID 0
+#define TYPE_SPECIFIER_BOOL 1
+#define TYPE_SPECIFIER_BVEC2 2
+#define TYPE_SPECIFIER_BVEC3 3
+#define TYPE_SPECIFIER_BVEC4 4
+#define TYPE_SPECIFIER_INT 5
+#define TYPE_SPECIFIER_IVEC2 6
+#define TYPE_SPECIFIER_IVEC3 7
+#define TYPE_SPECIFIER_IVEC4 8
+#define TYPE_SPECIFIER_FLOAT 9
+#define TYPE_SPECIFIER_VEC2 10
+#define TYPE_SPECIFIER_VEC3 11
+#define TYPE_SPECIFIER_VEC4 12
+#define TYPE_SPECIFIER_MAT2 13
+#define TYPE_SPECIFIER_MAT3 14
+#define TYPE_SPECIFIER_MAT4 15
+#define TYPE_SPECIFIER_SAMPLER1D 16
+#define TYPE_SPECIFIER_SAMPLER2D 17
+#define TYPE_SPECIFIER_SAMPLER3D 18
+#define TYPE_SPECIFIER_SAMPLERCUBE 19
+#define TYPE_SPECIFIER_SAMPLER1DSHADOW 20
+#define TYPE_SPECIFIER_SAMPLER2DSHADOW 21
+#define TYPE_SPECIFIER_STRUCT 22
+#define TYPE_SPECIFIER_TYPENAME 23
+
+static int
+parse_type_specifier(slang_parse_ctx * C, slang_output_ctx * O,
+ slang_type_specifier * spec)
+{
+ switch (*C->I++) {
+ case TYPE_SPECIFIER_VOID:
+ spec->type = slang_spec_void;
+ break;
+ case TYPE_SPECIFIER_BOOL:
+ spec->type = slang_spec_bool;
+ break;
+ case TYPE_SPECIFIER_BVEC2:
+ spec->type = slang_spec_bvec2;
+ break;
+ case TYPE_SPECIFIER_BVEC3:
+ spec->type = slang_spec_bvec3;
+ break;
+ case TYPE_SPECIFIER_BVEC4:
+ spec->type = slang_spec_bvec4;
+ break;
+ case TYPE_SPECIFIER_INT:
+ spec->type = slang_spec_int;
+ break;
+ case TYPE_SPECIFIER_IVEC2:
+ spec->type = slang_spec_ivec2;
+ break;
+ case TYPE_SPECIFIER_IVEC3:
+ spec->type = slang_spec_ivec3;
+ break;
+ case TYPE_SPECIFIER_IVEC4:
+ spec->type = slang_spec_ivec4;
+ break;
+ case TYPE_SPECIFIER_FLOAT:
+ spec->type = slang_spec_float;
+ break;
+ case TYPE_SPECIFIER_VEC2:
+ spec->type = slang_spec_vec2;
+ break;
+ case TYPE_SPECIFIER_VEC3:
+ spec->type = slang_spec_vec3;
+ break;
+ case TYPE_SPECIFIER_VEC4:
+ spec->type = slang_spec_vec4;
+ break;
+ case TYPE_SPECIFIER_MAT2:
+ spec->type = slang_spec_mat2;
+ break;
+ case TYPE_SPECIFIER_MAT3:
+ spec->type = slang_spec_mat3;
+ break;
+ case TYPE_SPECIFIER_MAT4:
+ spec->type = slang_spec_mat4;
+ break;
+ case TYPE_SPECIFIER_SAMPLER1D:
+ spec->type = slang_spec_sampler1D;
+ break;
+ case TYPE_SPECIFIER_SAMPLER2D:
+ spec->type = slang_spec_sampler2D;
+ break;
+ case TYPE_SPECIFIER_SAMPLER3D:
+ spec->type = slang_spec_sampler3D;
+ break;
+ case TYPE_SPECIFIER_SAMPLERCUBE:
+ spec->type = slang_spec_samplerCube;
+ break;
+ case TYPE_SPECIFIER_SAMPLER1DSHADOW:
+ spec->type = slang_spec_sampler1DShadow;
+ break;
+ case TYPE_SPECIFIER_SAMPLER2DSHADOW:
+ spec->type = slang_spec_sampler2DShadow;
+ break;
+ case TYPE_SPECIFIER_STRUCT:
+ spec->type = slang_spec_struct;
+ if (!parse_struct(C, O, &spec->_struct))
+ return 0;
+ break;
+ case TYPE_SPECIFIER_TYPENAME:
+ spec->type = slang_spec_struct;
+ {
+ slang_atom a_name;
+ slang_struct *stru;
+
+ a_name = parse_identifier(C);
+ if (a_name == NULL)
+ return 0;
+
+ stru = slang_struct_scope_find(O->structs, a_name, 1);
+ if (stru == NULL) {
+ slang_info_log_error(C->L, "undeclared type name '%s'",
+ slang_atom_pool_id(C->atoms, a_name));
+ return 0;
+ }
+
+ spec->_struct =
+ (slang_struct *) slang_alloc_malloc(sizeof(slang_struct));
+ if (spec->_struct == NULL) {
+ slang_info_log_memory(C->L);
+ return 0;
+ }
+ if (!slang_struct_construct(spec->_struct)) {
+ slang_alloc_free(spec->_struct);
+ spec->_struct = NULL;
+ return 0;
+ }
+ if (!slang_struct_copy(spec->_struct, stru))
+ return 0;
+ }
+ break;
+ default:
+ return 0;
+ }
+ return 1;
+}
+
+static int
+parse_fully_specified_type(slang_parse_ctx * C, slang_output_ctx * O,
+ slang_fully_specified_type * type)
+{
+ if (!parse_type_qualifier(C, &type->qualifier))
+ return 0;
+ if (!parse_type_specifier(C, O, &type->specifier))
+ return 0;
+ return 1;
+}
+
+/* operation */
+#define OP_END 0
+#define OP_BLOCK_BEGIN_NO_NEW_SCOPE 1
+#define OP_BLOCK_BEGIN_NEW_SCOPE 2
+#define OP_DECLARE 3
+#define OP_ASM 4
+#define OP_BREAK 5
+#define OP_CONTINUE 6
+#define OP_DISCARD 7
+#define OP_RETURN 8
+#define OP_EXPRESSION 9
+#define OP_IF 10
+#define OP_WHILE 11
+#define OP_DO 12
+#define OP_FOR 13
+#define OP_PUSH_VOID 14
+#define OP_PUSH_BOOL 15
+#define OP_PUSH_INT 16
+#define OP_PUSH_FLOAT 17
+#define OP_PUSH_IDENTIFIER 18
+#define OP_SEQUENCE 19
+#define OP_ASSIGN 20
+#define OP_ADDASSIGN 21
+#define OP_SUBASSIGN 22
+#define OP_MULASSIGN 23
+#define OP_DIVASSIGN 24
+/*#define OP_MODASSIGN 25*/
+/*#define OP_LSHASSIGN 26*/
+/*#define OP_RSHASSIGN 27*/
+/*#define OP_ORASSIGN 28*/
+/*#define OP_XORASSIGN 29*/
+/*#define OP_ANDASSIGN 30*/
+#define OP_SELECT 31
+#define OP_LOGICALOR 32
+#define OP_LOGICALXOR 33
+#define OP_LOGICALAND 34
+/*#define OP_BITOR 35*/
+/*#define OP_BITXOR 36*/
+/*#define OP_BITAND 37*/
+#define OP_EQUAL 38
+#define OP_NOTEQUAL 39
+#define OP_LESS 40
+#define OP_GREATER 41
+#define OP_LESSEQUAL 42
+#define OP_GREATEREQUAL 43
+/*#define OP_LSHIFT 44*/
+/*#define OP_RSHIFT 45*/
+#define OP_ADD 46
+#define OP_SUBTRACT 47
+#define OP_MULTIPLY 48
+#define OP_DIVIDE 49
+/*#define OP_MODULUS 50*/
+#define OP_PREINCREMENT 51
+#define OP_PREDECREMENT 52
+#define OP_PLUS 53
+#define OP_MINUS 54
+/*#define OP_COMPLEMENT 55*/
+#define OP_NOT 56
+#define OP_SUBSCRIPT 57
+#define OP_CALL 58
+#define OP_FIELD 59
+#define OP_POSTINCREMENT 60
+#define OP_POSTDECREMENT 61
+
+
+/**
+ * When parsing a compound production, this function is used to parse the
+ * children.
+ * For example, a a while-loop compound will have two children, the
+ * while condition expression and the loop body. So, this function will
+ * be called twice to parse those two sub-expressions.
+ * \param C the parsing context
+ * \param O the output context
+ * \param oper the operation we're parsing
+ * \param statement indicates whether parsing a statement, or expression
+ * \return 1 if success, 0 if error
+ */
+static int
+parse_child_operation(slang_parse_ctx * C, slang_output_ctx * O,
+ slang_operation * oper, GLboolean statement)
+{
+ slang_operation *ch;
+
+ /* grow child array */
+ ch = slang_operation_grow(&oper->num_children, &oper->children);
+ if (statement)
+ return parse_statement(C, O, ch);
+ return parse_expression(C, O, ch);
+}
+
+static int parse_declaration(slang_parse_ctx * C, slang_output_ctx * O);
+
+static int
+parse_statement(slang_parse_ctx * C, slang_output_ctx * O,
+ slang_operation * oper)
+{
+ oper->locals->outer_scope = O->vars;
+ switch (*C->I++) {
+ case OP_BLOCK_BEGIN_NO_NEW_SCOPE:
+ /* parse child statements, do not create new variable scope */
+ oper->type = slang_oper_block_no_new_scope;
+ while (*C->I != OP_END)
+ if (!parse_child_operation(C, O, oper, 1))
+ return 0;
+ C->I++;
+ break;
+ case OP_BLOCK_BEGIN_NEW_SCOPE:
+ /* parse child statements, create new variable scope */
+ {
+ slang_output_ctx o = *O;
+
+ oper->type = slang_oper_block_new_scope;
+ o.vars = oper->locals;
+ while (*C->I != OP_END)
+ if (!parse_child_operation(C, &o, oper, 1))
+ return 0;
+ C->I++;
+ }
+ break;
+ case OP_DECLARE:
+ /* local variable declaration, individual declarators are stored as
+ * children identifiers
+ */
+ oper->type = slang_oper_block_no_new_scope;
+ {
+ const unsigned int first_var = O->vars->num_variables;
+
+ /* parse the declaration, note that there can be zero or more
+ * than one declarators
+ */
+ if (!parse_declaration(C, O))
+ return 0;
+ if (first_var < O->vars->num_variables) {
+ const unsigned int num_vars = O->vars->num_variables - first_var;
+ unsigned int i;
+
+ oper->num_children = num_vars;
+ oper->children = slang_operation_new(num_vars);
+ if (oper->children == NULL) {
+ slang_info_log_memory(C->L);
+ return 0;
+ }
+ for (i = first_var; i < O->vars->num_variables; i++) {
+ slang_operation *o = &oper->children[i - first_var];
+ o->type = slang_oper_variable_decl;
+ o->locals->outer_scope = O->vars;
+ o->a_id = O->vars->variables[i]->a_name;
+ }
+ }
+ }
+ break;
+ case OP_ASM:
+ /* the __asm statement, parse the mnemonic and all its arguments
+ * as expressions
+ */
+ oper->type = slang_oper_asm;
+ oper->a_id = parse_identifier(C);
+ if (oper->a_id == SLANG_ATOM_NULL)
+ return 0;
+ while (*C->I != OP_END) {
+ if (!parse_child_operation(C, O, oper, 0))
+ return 0;
+ }
+ C->I++;
+ break;
+ case OP_BREAK:
+ oper->type = slang_oper_break;
+ break;
+ case OP_CONTINUE:
+ oper->type = slang_oper_continue;
+ break;
+ case OP_DISCARD:
+ oper->type = slang_oper_discard;
+ break;
+ case OP_RETURN:
+ oper->type = slang_oper_return;
+ if (!parse_child_operation(C, O, oper, 0))
+ return 0;
+ break;
+ case OP_EXPRESSION:
+ oper->type = slang_oper_expression;
+ if (!parse_child_operation(C, O, oper, 0))
+ return 0;
+ break;
+ case OP_IF:
+ oper->type = slang_oper_if;
+ if (!parse_child_operation(C, O, oper, 0))
+ return 0;
+ if (!parse_child_operation(C, O, oper, 1))
+ return 0;
+ if (!parse_child_operation(C, O, oper, 1))
+ return 0;
+ break;
+ case OP_WHILE:
+ {
+ slang_output_ctx o = *O;
+
+ oper->type = slang_oper_while;
+ o.vars = oper->locals;
+ if (!parse_child_operation(C, &o, oper, 1))
+ return 0;
+ if (!parse_child_operation(C, &o, oper, 1))
+ return 0;
+ }
+ break;
+ case OP_DO:
+ oper->type = slang_oper_do;
+ if (!parse_child_operation(C, O, oper, 1))
+ return 0;
+ if (!parse_child_operation(C, O, oper, 0))
+ return 0;
+ break;
+ case OP_FOR:
+ {
+ slang_output_ctx o = *O;
+
+ oper->type = slang_oper_for;
+ o.vars = oper->locals;
+ if (!parse_child_operation(C, &o, oper, 1))
+ return 0;
+ if (!parse_child_operation(C, &o, oper, 1))
+ return 0;
+ if (!parse_child_operation(C, &o, oper, 0))
+ return 0;
+ if (!parse_child_operation(C, &o, oper, 1))
+ return 0;
+ }
+ break;
+ default:
+ return 0;
+ }
+ return 1;
+}
+
+static int
+handle_nary_expression(slang_parse_ctx * C, slang_operation * op,
+ slang_operation ** ops, unsigned int *total_ops,
+ unsigned int n)
+{
+ unsigned int i;
+
+ op->children =
+ (slang_operation *) slang_alloc_malloc(n * sizeof(slang_operation));
+ if (op->children == NULL) {
+ slang_info_log_memory(C->L);
+ return 0;
+ }
+ op->num_children = n;
+
+ for (i = 0; i < n; i++)
+ op->children[i] = (*ops)[*total_ops - (n + 1 - i)];
+ (*ops)[*total_ops - (n + 1)] = (*ops)[*total_ops - 1];
+ *total_ops -= n;
+
+ *ops = (slang_operation *)
+ slang_alloc_realloc(*ops,
+ (*total_ops + n) * sizeof(slang_operation),
+ *total_ops * sizeof(slang_operation));
+ if (*ops == NULL) {
+ slang_info_log_memory(C->L);
+ return 0;
+ }
+ return 1;
+}
+
+static int
+is_constructor_name(const char *name, slang_atom a_name,
+ slang_struct_scope * structs)
+{
+ if (slang_type_specifier_type_from_string(name) != slang_spec_void)
+ return 1;
+ return slang_struct_scope_find(structs, a_name, 1) != NULL;
+}
+
+static int
+parse_expression(slang_parse_ctx * C, slang_output_ctx * O,
+ slang_operation * oper)
+{
+ slang_operation *ops = NULL;
+ unsigned int num_ops = 0;
+ int number;
+
+ while (*C->I != OP_END) {
+ slang_operation *op;
+ const unsigned int op_code = *C->I++;
+
+ /* allocate default operation, becomes a no-op if not used */
+ ops = (slang_operation *)
+ slang_alloc_realloc(ops,
+ num_ops * sizeof(slang_operation),
+ (num_ops + 1) * sizeof(slang_operation));
+ if (ops == NULL) {
+ slang_info_log_memory(C->L);
+ return 0;
+ }
+ op = &ops[num_ops];
+ if (!slang_operation_construct(op)) {
+ slang_info_log_memory(C->L);
+ return 0;
+ }
+ num_ops++;
+ op->locals->outer_scope = O->vars;
+
+ switch (op_code) {
+ case OP_PUSH_VOID:
+ op->type = slang_oper_void;
+ break;
+ case OP_PUSH_BOOL:
+ op->type = slang_oper_literal_bool;
+ if (!parse_number(C, &number))
+ return 0;
+ op->literal[0] =
+ op->literal[1] =
+ op->literal[2] =
+ op->literal[3] = (GLfloat) number;
+ break;
+ case OP_PUSH_INT:
+ op->type = slang_oper_literal_int;
+ if (!parse_number(C, &number))
+ return 0;
+ op->literal[0] =
+ op->literal[1] =
+ op->literal[2] =
+ op->literal[3] = (GLfloat) number;
+ break;
+ case OP_PUSH_FLOAT:
+ op->type = slang_oper_literal_float;
+ if (!parse_float(C, &op->literal[0]))
+ return 0;
+ op->literal[1] =
+ op->literal[2] =
+ op->literal[3] = op->literal[0];
+ break;
+ case OP_PUSH_IDENTIFIER:
+ op->type = slang_oper_identifier;
+ op->a_id = parse_identifier(C);
+ if (op->a_id == SLANG_ATOM_NULL)
+ return 0;
+ break;
+ case OP_SEQUENCE:
+ op->type = slang_oper_sequence;
+ if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
+ return 0;
+ break;
+ case OP_ASSIGN:
+ op->type = slang_oper_assign;
+ if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
+ return 0;
+ break;
+ case OP_ADDASSIGN:
+ op->type = slang_oper_addassign;
+ if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
+ return 0;
+ break;
+ case OP_SUBASSIGN:
+ op->type = slang_oper_subassign;
+ if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
+ return 0;
+ break;
+ case OP_MULASSIGN:
+ op->type = slang_oper_mulassign;
+ if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
+ return 0;
+ break;
+ case OP_DIVASSIGN:
+ op->type = slang_oper_divassign;
+ if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
+ return 0;
+ break;
+ /*case OP_MODASSIGN: */
+ /*case OP_LSHASSIGN: */
+ /*case OP_RSHASSIGN: */
+ /*case OP_ORASSIGN: */
+ /*case OP_XORASSIGN: */
+ /*case OP_ANDASSIGN: */
+ case OP_SELECT:
+ op->type = slang_oper_select;
+ if (!handle_nary_expression(C, op, &ops, &num_ops, 3))
+ return 0;
+ break;
+ case OP_LOGICALOR:
+ op->type = slang_oper_logicalor;
+ if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
+ return 0;
+ break;
+ case OP_LOGICALXOR:
+ op->type = slang_oper_logicalxor;
+ if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
+ return 0;
+ break;
+ case OP_LOGICALAND:
+ op->type = slang_oper_logicaland;
+ if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
+ return 0;
+ break;
+ /*case OP_BITOR: */
+ /*case OP_BITXOR: */
+ /*case OP_BITAND: */
+ case OP_EQUAL:
+ op->type = slang_oper_equal;
+ if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
+ return 0;
+ break;
+ case OP_NOTEQUAL:
+ op->type = slang_oper_notequal;
+ if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
+ return 0;
+ break;
+ case OP_LESS:
+ op->type = slang_oper_less;
+ if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
+ return 0;
+ break;
+ case OP_GREATER:
+ op->type = slang_oper_greater;
+ if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
+ return 0;
+ break;
+ case OP_LESSEQUAL:
+ op->type = slang_oper_lessequal;
+ if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
+ return 0;
+ break;
+ case OP_GREATEREQUAL:
+ op->type = slang_oper_greaterequal;
+ if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
+ return 0;
+ break;
+ /*case OP_LSHIFT: */
+ /*case OP_RSHIFT: */
+ case OP_ADD:
+ op->type = slang_oper_add;
+ if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
+ return 0;
+ break;
+ case OP_SUBTRACT:
+ op->type = slang_oper_subtract;
+ if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
+ return 0;
+ break;
+ case OP_MULTIPLY:
+ op->type = slang_oper_multiply;
+ if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
+ return 0;
+ break;
+ case OP_DIVIDE:
+ op->type = slang_oper_divide;
+ if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
+ return 0;
+ break;
+ /*case OP_MODULUS: */
+ case OP_PREINCREMENT:
+ op->type = slang_oper_preincrement;
+ if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
+ return 0;
+ break;
+ case OP_PREDECREMENT:
+ op->type = slang_oper_predecrement;
+ if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
+ return 0;
+ break;
+ case OP_PLUS:
+ op->type = slang_oper_plus;
+ if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
+ return 0;
+ break;
+ case OP_MINUS:
+ op->type = slang_oper_minus;
+ if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
+ return 0;
+ break;
+ case OP_NOT:
+ op->type = slang_oper_not;
+ if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
+ return 0;
+ break;
+ /*case OP_COMPLEMENT: */
+ case OP_SUBSCRIPT:
+ op->type = slang_oper_subscript;
+ if (!handle_nary_expression(C, op, &ops, &num_ops, 2))
+ return 0;
+ break;
+ case OP_CALL:
+ op->type = slang_oper_call;
+ op->a_id = parse_identifier(C);
+ if (op->a_id == SLANG_ATOM_NULL)
+ return 0;
+ while (*C->I != OP_END)
+ if (!parse_child_operation(C, O, op, 0))
+ return 0;
+ C->I++;
+
+ if (!C->parsing_builtin
+ && !slang_function_scope_find_by_name(O->funs, op->a_id, 1)) {
+ const char *id;
+
+ id = slang_atom_pool_id(C->atoms, op->a_id);
+ if (!is_constructor_name(id, op->a_id, O->structs)) {
+ slang_info_log_error(C->L, "%s: undeclared function name.", id);
+ return 0;
+ }
+ }
+ break;
+ case OP_FIELD:
+ op->type = slang_oper_field;
+ op->a_id = parse_identifier(C);
+ if (op->a_id == SLANG_ATOM_NULL)
+ return 0;
+ if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
+ return 0;
+ break;
+ case OP_POSTINCREMENT:
+ op->type = slang_oper_postincrement;
+ if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
+ return 0;
+ break;
+ case OP_POSTDECREMENT:
+ op->type = slang_oper_postdecrement;
+ if (!handle_nary_expression(C, op, &ops, &num_ops, 1))
+ return 0;
+ break;
+ default:
+ return 0;
+ }
+ }
+ C->I++;
+
+ *oper = *ops;
+ slang_alloc_free(ops);
+
+ return 1;
+}
+
+/* parameter qualifier */
+#define PARAM_QUALIFIER_IN 0
+#define PARAM_QUALIFIER_OUT 1
+#define PARAM_QUALIFIER_INOUT 2
+
+/* function parameter array presence */
+#define PARAMETER_ARRAY_NOT_PRESENT 0
+#define PARAMETER_ARRAY_PRESENT 1
+
+static int
+parse_parameter_declaration(slang_parse_ctx * C, slang_output_ctx * O,
+ slang_variable * param)
+{
+ /* parse and validate the parameter's type qualifiers (there can be
+ * two at most) because not all combinations are valid
+ */
+ if (!parse_type_qualifier(C, ¶m->type.qualifier))
+ return 0;
+ switch (*C->I++) {
+ case PARAM_QUALIFIER_IN:
+ if (param->type.qualifier != slang_qual_const
+ && param->type.qualifier != slang_qual_none) {
+ slang_info_log_error(C->L, "Invalid type qualifier.");
+ return 0;
+ }
+ break;
+ case PARAM_QUALIFIER_OUT:
+ if (param->type.qualifier == slang_qual_none)
+ param->type.qualifier = slang_qual_out;
+ else {
+ slang_info_log_error(C->L, "Invalid type qualifier.");
+ return 0;
+ }
+ break;
+ case PARAM_QUALIFIER_INOUT:
+ if (param->type.qualifier == slang_qual_none)
+ param->type.qualifier = slang_qual_inout;
+ else {
+ slang_info_log_error(C->L, "Invalid type qualifier.");
+ return 0;
+ }
+ break;
+ default:
+ return 0;
+ }
+
+ /* parse parameter's type specifier and name */
+ if (!parse_type_specifier(C, O, ¶m->type.specifier))
+ return 0;
+ param->a_name = parse_identifier(C);
+ if (param->a_name == SLANG_ATOM_NULL)
+ return 0;
+
+ /* if the parameter is an array, parse its size (the size must be
+ * explicitly defined
+ */
+ if (*C->I++ == PARAMETER_ARRAY_PRESENT) {
+ slang_type_specifier p;
+
+ slang_type_specifier_ctr(&p);
+ if (!slang_type_specifier_copy(&p, ¶m->type.specifier)) {
+ slang_type_specifier_dtr(&p);
+ return GL_FALSE;
+ }
+ if (!convert_to_array(C, param, &p)) {
+ slang_type_specifier_dtr(&p);
+ return GL_FALSE;
+ }
+ slang_type_specifier_dtr(&p);
+ if (!parse_array_len(C, O, ¶m->array_len))
+ return GL_FALSE;
+ }
+
+ /* calculate the parameter size */
+ if (!calculate_var_size(C, O, param))
+ return GL_FALSE;
+
+ /* TODO: allocate the local address here? */
+ return 1;
+}
+
+/* function type */
+#define FUNCTION_ORDINARY 0
+#define FUNCTION_CONSTRUCTOR 1
+#define FUNCTION_OPERATOR 2
+
+/* function parameter */
+#define PARAMETER_NONE 0
+#define PARAMETER_NEXT 1
+
+/* operator type */
+#define OPERATOR_ADDASSIGN 1
+#define OPERATOR_SUBASSIGN 2
+#define OPERATOR_MULASSIGN 3
+#define OPERATOR_DIVASSIGN 4
+/*#define OPERATOR_MODASSIGN 5*/
+/*#define OPERATOR_LSHASSIGN 6*/
+/*#define OPERATOR_RSHASSIGN 7*/
+/*#define OPERATOR_ANDASSIGN 8*/
+/*#define OPERATOR_XORASSIGN 9*/
+/*#define OPERATOR_ORASSIGN 10*/
+#define OPERATOR_LOGICALXOR 11
+/*#define OPERATOR_BITOR 12*/
+/*#define OPERATOR_BITXOR 13*/
+/*#define OPERATOR_BITAND 14*/
+#define OPERATOR_LESS 15
+#define OPERATOR_GREATER 16
+#define OPERATOR_LESSEQUAL 17
+#define OPERATOR_GREATEREQUAL 18
+/*#define OPERATOR_LSHIFT 19*/
+/*#define OPERATOR_RSHIFT 20*/
+#define OPERATOR_MULTIPLY 21
+#define OPERATOR_DIVIDE 22
+/*#define OPERATOR_MODULUS 23*/
+#define OPERATOR_INCREMENT 24
+#define OPERATOR_DECREMENT 25
+#define OPERATOR_PLUS 26
+#define OPERATOR_MINUS 27
+/*#define OPERATOR_COMPLEMENT 28*/
+#define OPERATOR_NOT 29
+
+static const struct
+{
+ unsigned int o_code;
+ const char *o_name;
+} operator_names[] = {
+ {OPERATOR_INCREMENT, "++"},
+ {OPERATOR_ADDASSIGN, "+="},
+ {OPERATOR_PLUS, "+"},
+ {OPERATOR_DECREMENT, "--"},
+ {OPERATOR_SUBASSIGN, "-="},
+ {OPERATOR_MINUS, "-"},
+ {OPERATOR_NOT, "!"},
+ {OPERATOR_MULASSIGN, "*="},
+ {OPERATOR_MULTIPLY, "*"},
+ {OPERATOR_DIVASSIGN, "/="},
+ {OPERATOR_DIVIDE, "/"},
+ {OPERATOR_LESSEQUAL, "<="},
+ /*{ OPERATOR_LSHASSIGN, "<<=" }, */
+ /*{ OPERATOR_LSHIFT, "<<" }, */
+ {OPERATOR_LESS, "<"},
+ {OPERATOR_GREATEREQUAL, ">="},
+ /*{ OPERATOR_RSHASSIGN, ">>=" }, */
+ /*{ OPERATOR_RSHIFT, ">>" }, */
+ {OPERATOR_GREATER, ">"},
+ /*{ OPERATOR_MODASSIGN, "%=" }, */
+ /*{ OPERATOR_MODULUS, "%" }, */
+ /*{ OPERATOR_ANDASSIGN, "&=" }, */
+ /*{ OPERATOR_BITAND, "&" }, */
+ /*{ OPERATOR_ORASSIGN, "|=" }, */
+ /*{ OPERATOR_BITOR, "|" }, */
+ /*{ OPERATOR_COMPLEMENT, "~" }, */
+ /*{ OPERATOR_XORASSIGN, "^=" }, */
+ {OPERATOR_LOGICALXOR, "^^"},
+ /*{ OPERATOR_BITXOR, "^" } */
+};
+
+static slang_atom
+parse_operator_name(slang_parse_ctx * C)
+{
+ unsigned int i;
+
+ for (i = 0; i < sizeof(operator_names) / sizeof(*operator_names); i++) {
+ if (operator_names[i].o_code == (unsigned int) (*C->I)) {
+ slang_atom atom =
+ slang_atom_pool_atom(C->atoms, operator_names[i].o_name);
+ if (atom == SLANG_ATOM_NULL) {
+ slang_info_log_memory(C->L);
+ return 0;
+ }
+ C->I++;
+ return atom;
+ }
+ }
+ return 0;
+}
+
+static int
+parse_function_prototype(slang_parse_ctx * C, slang_output_ctx * O,
+ slang_function * func)
+{
+ /* parse function type and name */
+ if (!parse_fully_specified_type(C, O, &func->header.type))
+ return 0;
+ switch (*C->I++) {
+ case FUNCTION_ORDINARY:
+ func->kind = slang_func_ordinary;
+ func->header.a_name = parse_identifier(C);
+ if (func->header.a_name == SLANG_ATOM_NULL)
+ return 0;
+ break;
+ case FUNCTION_CONSTRUCTOR:
+ func->kind = slang_func_constructor;
+ if (func->header.type.specifier.type == slang_spec_struct)
+ return 0;
+ func->header.a_name =
+ slang_atom_pool_atom(C->atoms,
+ slang_type_specifier_type_to_string
+ (func->header.type.specifier.type));
+ if (func->header.a_name == SLANG_ATOM_NULL) {
+ slang_info_log_memory(C->L);
+ return 0;
+ }
+ break;
+ case FUNCTION_OPERATOR:
+ func->kind = slang_func_operator;
+ func->header.a_name = parse_operator_name(C);
+ if (func->header.a_name == SLANG_ATOM_NULL)
+ return 0;
+ break;
+ default:
+ return 0;
+ }
+
+ /* parse function parameters */
+ while (*C->I++ == PARAMETER_NEXT) {
+ slang_variable *p = slang_variable_scope_grow(func->parameters);
+ if (!p) {
+ slang_info_log_memory(C->L);
+ return 0;
+ }
+ if (!parse_parameter_declaration(C, O, p))
+ return 0;
+ }
+
+#if 111
+ /* if the function returns a value, append a hidden __retVal 'out'
+ * parameter that corresponds to the return value.
+ */
+ if (_slang_function_has_return_value(func)) {
+ slang_variable *p = slang_variable_scope_grow(func->parameters);
+ slang_atom a_retVal = slang_atom_pool_atom(C->atoms, "__retVal");
+ assert(a_retVal);
+ p->a_name = a_retVal;
+ p->type = func->header.type;
+ p->type.qualifier = slang_qual_out;
+ }
+#endif
+
+ /* function formal parameters and local variables share the same
+ * scope, so save the information about param count in a seperate
+ * place also link the scope to the global variable scope so when a
+ * given identifier is not found here, the search process continues
+ * in the global space
+ */
+ func->param_count = func->parameters->num_variables;
+ func->parameters->outer_scope = O->vars;
+
+ return 1;
+}
+
+static int
+parse_function_definition(slang_parse_ctx * C, slang_output_ctx * O,
+ slang_function * func)
+{
+ slang_output_ctx o = *O;
+
+ if (!parse_function_prototype(C, O, func))
+ return 0;
+
+ /* create function's body operation */
+ func->body =
+ (slang_operation *) slang_alloc_malloc(sizeof(slang_operation));
+ if (func->body == NULL) {
+ slang_info_log_memory(C->L);
+ return 0;
+ }
+ if (!slang_operation_construct(func->body)) {
+ slang_alloc_free(func->body);
+ func->body = NULL;
+ slang_info_log_memory(C->L);
+ return 0;
+ }
+
+ /* to parse the body the parse context is modified in order to
+ * capture parsed variables into function's local variable scope
+ */
+ C->global_scope = GL_FALSE;
+ o.vars = func->parameters;
+ if (!parse_statement(C, &o, func->body))
+ return 0;
+
+ C->global_scope = GL_TRUE;
+ return 1;
+}
+
+static GLboolean
+initialize_global(slang_assemble_ctx * A, slang_variable * var)
+{
+#if 01
+ slang_assembly_file_restore_point point;
+#endif
+ slang_machine mach;
+ slang_assembly_local_info save_local = A->local;
+ slang_operation op_id, op_assign;
+ GLboolean result;
+
+#if 01
+ /* save the current assembly */
+ if (!slang_assembly_file_restore_point_save(A->file, &point))
+ return GL_FALSE;
+#endif
+
+ /* setup the machine */
+ mach = *A->mach;
+ mach.ip = A->file->count;
+
+ /* allocate local storage for expression */
+ A->local.ret_size = 0;
+ A->local.addr_tmp = 0;
+ A->local.swizzle_tmp = 4;
+ if (!slang_assembly_file_push_label(A->file, slang_asm_local_alloc, 20))
+ return GL_FALSE;
+ if (!slang_assembly_file_push_label(A->file, slang_asm_enter, 20))
+ return GL_FALSE;
+
+ /* construct the left side of assignment */
+ if (!slang_operation_construct(&op_id))
+ return GL_FALSE;
+ op_id.type = slang_oper_identifier;
+ op_id.a_id = var->a_name;
+
+ /* put the variable into operation's scope */
+ op_id.locals->variables =
+ (slang_variable **) slang_alloc_malloc(sizeof(slang_variable *));
+ if (op_id.locals->variables == NULL) {
+ slang_operation_destruct(&op_id);
+ return GL_FALSE;
+ }
+ op_id.locals->num_variables = 1;
+ op_id.locals->variables[0] = var;
+
+ /* construct the assignment expression */
+ if (!slang_operation_construct(&op_assign)) {
+ op_id.locals->num_variables = 0;
+ slang_operation_destruct(&op_id);
+ return GL_FALSE;
+ }
+ op_assign.type = slang_oper_assign;
+ op_assign.children =
+ (slang_operation *) slang_alloc_malloc(2 * sizeof(slang_operation));
+ if (op_assign.children == NULL) {
+ slang_operation_destruct(&op_assign);
+ op_id.locals->num_variables = 0;
+ slang_operation_destruct(&op_id);
+ return GL_FALSE;
+ }
+ op_assign.num_children = 2;
+ op_assign.children[0] = op_id;
+ op_assign.children[1] = *var->initializer;
+
+#if 0 /* this should go away */
+ /* insert the actual expression */
+ result = _slang_assemble_operation(A, &op_assign, slang_ref_forbid);
+#else
+ result = 1;
+#endif
+
+ /* carefully destroy the operations */
+ op_assign.num_children = 0;
+ slang_alloc_free(op_assign.children);
+ op_assign.children = NULL;
+ slang_operation_destruct(&op_assign);
+ op_id.locals->num_variables = 0;
+ slang_operation_destruct(&op_id);
+
+ if (!result)
+ return GL_FALSE;
+ if (!slang_assembly_file_push(A->file, slang_asm_exit))
+ return GL_FALSE;
+
+ /* execute the expression */
+#if 0
+ if (!_slang_execute2(A->file, &mach))
+ return GL_FALSE;
+#endif
+
+#if 01
+ /* restore the old assembly */
+ if (!slang_assembly_file_restore_point_load(A->file, &point))
+ return GL_FALSE;
+#endif
+ A->local = save_local;
+
+ /* now we copy the contents of the initialized variable back to the original machine */
+ _mesa_memcpy((GLubyte *) A->mach->mem + var->address,
+ (GLubyte *) mach.mem + var->address, var->size);
+
+ return GL_TRUE;
+}
+
+/* init declarator list */
+#define DECLARATOR_NONE 0
+#define DECLARATOR_NEXT 1
+
+/* variable declaration */
+#define VARIABLE_NONE 0
+#define VARIABLE_IDENTIFIER 1
+#define VARIABLE_INITIALIZER 2
+#define VARIABLE_ARRAY_EXPLICIT 3
+#define VARIABLE_ARRAY_UNKNOWN 4
+
+
+/**
+ * Parse the initializer for a variable declaration.
+ */
+static int
+parse_init_declarator(slang_parse_ctx * C, slang_output_ctx * O,
+ const slang_fully_specified_type * type)
+{
+ slang_variable *var;
+
+ /* empty init declatator (without name, e.g. "float ;") */
+ if (*C->I++ == VARIABLE_NONE)
+ return 1;
+
+ /* make room for the new variable and initialize it */
+ var = slang_variable_scope_grow(O->vars);
+ if (!var) {
+ slang_info_log_memory(C->L);
+ return 0;
+ }
+
+ /* copy the declarator qualifier type, parse the identifier */
+ var->global = C->global_scope;
+ var->type.qualifier = type->qualifier;
+ var->a_name = parse_identifier(C);
+ if (var->a_name == SLANG_ATOM_NULL)
+ return 0;
+
+ switch (*C->I++) {
+ case VARIABLE_NONE:
+ /* simple variable declarator - just copy the specifier */
+ if (!slang_type_specifier_copy(&var->type.specifier, &type->specifier))
+ return 0;
+ break;
+ case VARIABLE_INITIALIZER:
+ /* initialized variable - copy the specifier and parse the expression */
+ if (!slang_type_specifier_copy(&var->type.specifier, &type->specifier))
+ return 0;
+ var->initializer =
+ (slang_operation *) slang_alloc_malloc(sizeof(slang_operation));
+ if (var->initializer == NULL) {
+ slang_info_log_memory(C->L);
+ return 0;
+ }
+ if (!slang_operation_construct(var->initializer)) {
+ slang_alloc_free(var->initializer);
+ var->initializer = NULL;
+ slang_info_log_memory(C->L);
+ return 0;
+ }
+ if (!parse_expression(C, O, var->initializer))
+ return 0;
+ break;
+#if 0
+ case VARIABLE_ARRAY_UNKNOWN:
+ /* unsized array - mark it as array and copy the specifier to
+ the array element
+ */
+ if (!convert_to_array(C, var, &type->specifier))
+ return GL_FALSE;
+ break;
+#endif
+ case VARIABLE_ARRAY_EXPLICIT:
+ if (!convert_to_array(C, var, &type->specifier))
+ return GL_FALSE;
+ if (!parse_array_len(C, O, &var->array_len))
+ return GL_FALSE;
+ break;
+ default:
+ return 0;
+ }
+
+#if 1
+ if (C->global_scope) {
+ slang_assemble_ctx A;
+
+ A.file = O->assembly;
+ A.mach = O->machine;
+ A.atoms = C->atoms;
+ A.space.funcs = O->funs;
+ A.space.structs = O->structs;
+ A.space.vars = O->vars;
+ A.program = O->program;
+#if 0
+ A.codegen = O->codegen;
+#endif
+ A.vartable = O->vartable;
+
+ _slang_codegen_global_variable(&A, var, C->type);
+ }
+#endif
+
+ /* allocate global address space for a variable with a known size */
+ if (C->global_scope
+ && !(var->type.specifier.type == slang_spec_array
+ && var->array_len == 0)) {
+ if (!calculate_var_size(C, O, var))
+ return GL_FALSE;
+ var->address = slang_var_pool_alloc(O->global_pool, var->size);
+ }
+
+ /* initialize global variable */
+ if (C->global_scope) {
+ if (var->initializer != NULL) {
+ slang_assemble_ctx A;
+
+ A.file = O->assembly;
+ A.mach = O->machine;
+ A.atoms = C->atoms;
+ A.space.funcs = O->funs;
+ A.space.structs = O->structs;
+ A.space.vars = O->vars;
+ if (!initialize_global(&A, var))
+ return 0;
+ }
+ else {
+ _mesa_memset((GLubyte *) (O->machine->mem) + var->address, 0,
+ var->size);
+ }
+ }
+ return 1;
+}
+
+/**
+ * Parse a list of variable declarations. Each variable may have an
+ * initializer.
+ */
+static int
+parse_init_declarator_list(slang_parse_ctx * C, slang_output_ctx * O)
+{
+ slang_fully_specified_type type;
+
+ /* parse the fully specified type, common to all declarators */
+ if (!slang_fully_specified_type_construct(&type))
+ return 0;
+ if (!parse_fully_specified_type(C, O, &type)) {
+ slang_fully_specified_type_destruct(&type);
+ return 0;
+ }
+
+ /* parse declarators, pass-in the parsed type */
+ do {
+ if (!parse_init_declarator(C, O, &type)) {
+ slang_fully_specified_type_destruct(&type);
+ return 0;
+ }
+ }
+ while (*C->I++ == DECLARATOR_NEXT);
+
+ slang_fully_specified_type_destruct(&type);
+ return 1;
+}
+
+
+/**
+ * Parse a function definition or declaration.
+ * \param C parsing context
+ * \param O output context
+ * \param definition if non-zero expect a definition, else a declaration
+ * \param parsed_func_ret returns the parsed function
+ * \return GL_TRUE if success, GL_FALSE if failure
+ */
+static GLboolean
+parse_function(slang_parse_ctx * C, slang_output_ctx * O, int definition,
+ slang_function ** parsed_func_ret)
+{
+ slang_function parsed_func, *found_func;
+
+ /* parse function definition/declaration */
+ if (!slang_function_construct(&parsed_func))
+ return GL_FALSE;
+ if (definition) {
+ if (!parse_function_definition(C, O, &parsed_func)) {
+ slang_function_destruct(&parsed_func);
+ return GL_FALSE;
+ }
+ }
+ else {
+ if (!parse_function_prototype(C, O, &parsed_func)) {
+ slang_function_destruct(&parsed_func);
+ return GL_FALSE;
+ }
+ }
+
+ /* find a function with a prototype matching the parsed one - only
+ * the current scope is being searched to allow built-in function
+ * overriding
+ */
+ found_func = slang_function_scope_find(O->funs, &parsed_func, 0);
+ if (found_func == NULL) {
+ /* New function, add it to the function list */
+ O->funs->functions =
+ (slang_function *) slang_alloc_realloc(O->funs->functions,
+ O->funs->num_functions *
+ sizeof(slang_function),
+ (O->funs->num_functions +
+ 1) * sizeof(slang_function));
+ if (O->funs->functions == NULL) {
+ slang_info_log_memory(C->L);
+ slang_function_destruct(&parsed_func);
+ return GL_FALSE;
+ }
+ O->funs->functions[O->funs->num_functions] = parsed_func;
+ O->funs->num_functions++;
+
+ /* return the newly parsed function */
+ *parsed_func_ret = &O->funs->functions[O->funs->num_functions - 1];
+ }
+ else {
+ /* previously defined or declared */
+ /* TODO: check function return type qualifiers and specifiers */
+ if (definition) {
+ if (found_func->body != NULL) {
+ slang_info_log_error(C->L, "%s: function already has a body.",
+ slang_atom_pool_id(C->atoms,
+ parsed_func.header.
+ a_name));
+ slang_function_destruct(&parsed_func);
+ return GL_FALSE;
+ }
+
+ /* destroy the existing function declaration and replace it
+ * with the new one, remember to save the fixup table
+ */
+ parsed_func.fixups = found_func->fixups;
+ slang_fixup_table_init(&found_func->fixups);
+ slang_function_destruct(found_func);
+ *found_func = parsed_func;
+ }
+ else {
+ /* another declaration of the same function prototype - ignore it */
+ slang_function_destruct(&parsed_func);
+ }
+
+ /* return the found function */
+ *parsed_func_ret = found_func;
+ }
+
+ /* assemble the parsed function */
+ {
+ slang_assemble_ctx A;
+
+ A.file = O->assembly;
+ A.mach = O->machine;
+ A.atoms = C->atoms;
+ A.space.funcs = O->funs;
+ A.space.structs = O->structs;
+ A.space.vars = O->vars;
+ A.program = O->program;
+#if 0
+ A.codegen = O->codegen;
+#endif
+ A.vartable = O->vartable;
+
+ _slang_reset_error();
+
+#if 0
+ printf("*************** Assemble function %s ****\n", (char *) (*parsed_func_ret)->header.a_name);
+ slang_print_var_scope((*parsed_func_ret)->parameters,
+ (*parsed_func_ret)->param_count);
+#endif
+
+#if 0
+ if (!_slang_assemble_function(&A, *parsed_func_ret)) {
+ /* propogate the error message back through the info log */
+ C->L->text = _mesa_strdup(_slang_error_text());
+ C->L->dont_free_text = GL_FALSE;
+ return GL_FALSE;
+ }
+#endif
+
+#if 0
+ printf("**************************************\n");
+#endif
+#if 1
+ _slang_codegen_function(&A, *parsed_func_ret);
+#endif
+ }
+ return GL_TRUE;
+}
+
+/* declaration */
+#define DECLARATION_FUNCTION_PROTOTYPE 1
+#define DECLARATION_INIT_DECLARATOR_LIST 2
+
+static int
+parse_declaration(slang_parse_ctx * C, slang_output_ctx * O)
+{
+ switch (*C->I++) {
+ case DECLARATION_INIT_DECLARATOR_LIST:
+ if (!parse_init_declarator_list(C, O))
+ return 0;
+ break;
+ case DECLARATION_FUNCTION_PROTOTYPE:
+ {
+ slang_function *dummy_func;
+
+ if (!parse_function(C, O, 0, &dummy_func))
+ return 0;
+ }
+ break;
+ default:
+ return 0;
+ }
+ return 1;
+}
+
+/* external declaration */
+#define EXTERNAL_NULL 0
+#define EXTERNAL_FUNCTION_DEFINITION 1
+#define EXTERNAL_DECLARATION 2
+
+static GLboolean
+parse_code_unit(slang_parse_ctx * C, slang_code_unit * unit,
+ struct gl_program *program)
+{
+ slang_output_ctx o;
+ GLboolean success;
+
+ /* setup output context */
+ o.funs = &unit->funs;
+ o.structs = &unit->structs;
+ o.vars = &unit->vars;
+ o.assembly = &unit->object->assembly;
+ o.global_pool = &unit->object->varpool;
+ o.machine = &unit->object->machine;
+ o.program = program;
+ o.vartable = _slang_push_var_table(NULL);
+
+ /* parse individual functions and declarations */
+ while (*C->I != EXTERNAL_NULL) {
+ switch (*C->I++) {
+ case EXTERNAL_FUNCTION_DEFINITION:
+ {
+ slang_function *func;
+ success = parse_function(C, &o, 1, &func);
+ }
+ break;
+ case EXTERNAL_DECLARATION:
+ success = parse_declaration(C, &o);
+ break;
+ default:
+ success = GL_FALSE;
+ }
+
+ if (!success) {
+ /* xxx free codegen */
+ _slang_pop_var_table(o.vartable);
+ return GL_FALSE;
+ }
+ }
+ C->I++;
+
+ _slang_pop_var_table(o.vartable);
+ return GL_TRUE;
+}
+
+static GLboolean
+compile_binary(const byte * prod, slang_code_unit * unit,
+ slang_unit_type type, slang_info_log * infolog,
+ slang_code_unit * builtin, slang_code_unit * downlink,
+ struct gl_program *program)
+{
+ slang_parse_ctx C;
+
+ unit->type = type;
+
+ /* setup parse context */
+ C.I = prod;
+ C.L = infolog;
+ C.parsing_builtin = (builtin == NULL);
+ C.global_scope = GL_TRUE;
+ C.atoms = &unit->object->atompool;
+ C.type = type;
+
+ if (!check_revision(&C))
+ return GL_FALSE;
+
+ if (downlink != NULL) {
+ unit->vars.outer_scope = &downlink->vars;
+ unit->funs.outer_scope = &downlink->funs;
+ unit->structs.outer_scope = &downlink->structs;
+ }
+
+ /* parse translation unit */
+ return parse_code_unit(&C, unit, program);
+}
+
+static GLboolean
+compile_with_grammar(grammar id, const char *source, slang_code_unit * unit,
+ slang_unit_type type, slang_info_log * infolog,
+ slang_code_unit * builtin,
+ struct gl_program *program)
+{
+ byte *prod;
+ GLuint size, start, version;
+ slang_string preprocessed;
+
+ /* First retrieve the version number. */
+ if (!_slang_preprocess_version(source, &version, &start, infolog))
+ return GL_FALSE;
+
+ if (version > 110) {
+ slang_info_log_error(infolog,
+ "language version specified is not supported.");
+ return GL_FALSE;
+ }
+
+ /* Now preprocess the source string. */
+ slang_string_init(&preprocessed);
+ if (!_slang_preprocess_directives(&preprocessed, &source[start], infolog)) {
+ slang_string_free(&preprocessed);
+ slang_info_log_error(infolog, "failed to preprocess the source.");
+ return GL_FALSE;
+ }
+
+ /* Finally check the syntax and generate its binary representation. */
+ if (!grammar_fast_check(id,
+ (const byte *) (slang_string_cstr(&preprocessed)),
+ &prod, &size,
+ 65536)) {
+ char buf[1024];
+ GLint pos;
+
+ slang_string_free(&preprocessed);
+ grammar_get_last_error((byte *) (buf), sizeof(buf), &pos);
+ slang_info_log_error(infolog, buf);
+ RETURN_ERROR("syntax error (possibly in library code)", 0);
+ }
+ slang_string_free(&preprocessed);
+
+ /* Syntax is okay - translate it to internal representation. */
+ if (!compile_binary(prod, unit, type, infolog, builtin,
+ &builtin[SLANG_BUILTIN_TOTAL - 1],
+ program)) {
+ grammar_alloc_free(prod);
+ return GL_FALSE;
+ }
+ grammar_alloc_free(prod);
+ return GL_TRUE;
+}
+
+LONGSTRING static const char *slang_shader_syn =
+#include "library/slang_shader_syn.h"
+ ;
+
+static const byte slang_core_gc[] = {
+#include "library/slang_core_gc.h"
+};
+
+static const byte slang_common_builtin_gc[] = {
+#include "library/slang_common_builtin_gc.h"
+};
+
+static const byte slang_fragment_builtin_gc[] = {
+#include "library/slang_fragment_builtin_gc.h"
+};
+
+static const byte slang_vertex_builtin_gc[] = {
+#include "library/slang_vertex_builtin_gc.h"
+};
+
+#if defined(USE_X86_ASM) || defined(SLANG_X86)
+foo
+static const byte slang_builtin_vec4_gc[] = {
+#include "library/slang_builtin_vec4_gc.h"
+};
+#endif
+
+static GLboolean
+compile_object(grammar * id, const char *source, slang_code_object * object,
+ slang_unit_type type, slang_info_log * infolog,
+ struct gl_program *program)
+{
+ slang_code_unit *builtins = NULL;
+
+ /* load GLSL grammar */
+ *id = grammar_load_from_text((const byte *) (slang_shader_syn));
+ if (*id == 0) {
+ byte buf[1024];
+ int pos;
+
+ grammar_get_last_error(buf, 1024, &pos);
+ slang_info_log_error(infolog, (const char *) (buf));
+ return GL_FALSE;
+ }
+
+ /* set shader type - the syntax is slightly different for different shaders */
+ if (type == slang_unit_fragment_shader
+ || type == slang_unit_fragment_builtin)
+ grammar_set_reg8(*id, (const byte *) "shader_type", 1);
+ else
+ grammar_set_reg8(*id, (const byte *) "shader_type", 2);
+
+ /* enable language extensions */
+ grammar_set_reg8(*id, (const byte *) "parsing_builtin", 1);
+
+ /* if parsing user-specified shader, load built-in library */
+ if (type == slang_unit_fragment_shader || type == slang_unit_vertex_shader) {
+ /* compile core functionality first */
+ if (!compile_binary(slang_core_gc,
+ &object->builtin[SLANG_BUILTIN_CORE],
+ slang_unit_fragment_builtin, infolog,
+ NULL, NULL, NULL))
+ return GL_FALSE;
+
+ /* compile common functions and variables, link to core */
+ if (!compile_binary(slang_common_builtin_gc,
+ &object->builtin[SLANG_BUILTIN_COMMON],
+ slang_unit_fragment_builtin, infolog, NULL,
+ &object->builtin[SLANG_BUILTIN_CORE], NULL))
+ return GL_FALSE;
+
+ /* compile target-specific functions and variables, link to common */
+ if (type == slang_unit_fragment_shader) {
+ if (!compile_binary(slang_fragment_builtin_gc,
+ &object->builtin[SLANG_BUILTIN_TARGET],
+ slang_unit_fragment_builtin, infolog, NULL,
+ &object->builtin[SLANG_BUILTIN_COMMON], NULL))
+ return GL_FALSE;
+ }
+ else if (type == slang_unit_vertex_shader) {
+ if (!compile_binary(slang_vertex_builtin_gc,
+ &object->builtin[SLANG_BUILTIN_TARGET],
+ slang_unit_vertex_builtin, infolog, NULL,
+ &object->builtin[SLANG_BUILTIN_COMMON], NULL))
+ return GL_FALSE;
+ }
+
+#if defined(USE_X86_ASM) || defined(SLANG_X86)
+ /* compile x86 4-component vector overrides, link to target */
+ if (!compile_binary(slang_builtin_vec4_gc,
+ &object->builtin[SLANG_BUILTIN_VEC4],
+ slang_unit_fragment_builtin, infolog, NULL,
+ &object->builtin[SLANG_BUILTIN_TARGET]))
+ return GL_FALSE;
+#endif
+
+ /* disable language extensions */
+#if NEW_SLANG /* allow-built-ins */
+ grammar_set_reg8(*id, (const byte *) "parsing_builtin", 1);
+#else
+ grammar_set_reg8(*id, (const byte *) "parsing_builtin", 0);
+#endif
+ builtins = object->builtin;
+ }
+
+ /* compile the actual shader - pass-in built-in library for external shader */
+ return compile_with_grammar(*id, source, &object->unit, type, infolog,
+ builtins, program);
+}
+
+
+#if 0
+static void
+slang_create_uniforms(const slang_export_data_table *exports,
+ struct gl_program *program)
+{
+ /* XXX only add uniforms that are actually going to get used */
+ GLuint i;
+ for (i = 0; i < exports->count; i++) {
+ if (exports->entries[i].access == slang_exp_uniform) {
+ const char *name = (char *) exports->entries[i].quant.name;
+ GLint j = _mesa_add_uniform(program->Parameters, name, 4);
+ assert(j >= 0);
+ }
+ }
+}
+#endif
+
+
+static GLboolean
+compile_shader(GLcontext *ctx, slang_code_object * object,
+ slang_unit_type type, slang_info_log * infolog,
+ struct gl_shader *shader)
+{
+ struct gl_program *program = shader->Programs[0];
+ GLboolean success;
+ grammar id = 0;
+
+ assert(program);
+
+ _slang_code_object_dtr(object);
+ _slang_code_object_ctr(object);
+
+ success = compile_object(&id, shader->Source, object, type, infolog, program);
+ if (id != 0)
+ grammar_destroy(id);
+ if (!success)
+ return GL_FALSE;
+
+ if (!_slang_build_export_data_table(&object->expdata, &object->unit.vars))
+ return GL_FALSE;
+ if (!_slang_build_export_code_table(&object->expcode, &object->unit.funs,
+ &object->unit))
+ return GL_FALSE;
+
+#if NEW_SLANG
+ {
+ slang_create_uniforms(&object->expdata, shader);
+ _mesa_print_program(program);
+ _mesa_print_program_parameters(ctx, program);
+ }
+#endif
+
+ return GL_TRUE;
+}
+
+
+
+GLboolean
+_slang_compile(GLcontext *ctx, struct gl_shader *shader)
+{
+ GLboolean success;
+ slang_info_log info_log;
+ slang_code_object obj;
+ slang_unit_type type;
+
+ if (shader->Type == GL_VERTEX_SHADER) {
+ type = slang_unit_vertex_shader;
+ }
+ else {
+ assert(shader->Type == GL_FRAGMENT_SHADER);
+ type = slang_unit_fragment_shader;
+ }
+
+ /* XXX temporary hack */
+ if (!shader->Programs) {
+ GLenum progTarget;
+ if (shader->Type == GL_VERTEX_SHADER)
+ progTarget = GL_VERTEX_PROGRAM_ARB;
+ else
+ progTarget = GL_FRAGMENT_PROGRAM_ARB;
+ shader->Programs
+ = (struct gl_program **) malloc(sizeof(struct gl_program*));
+ shader->Programs[0] = _mesa_new_program(ctx, progTarget, 1);
+ shader->NumPrograms = 1;
+
+ shader->Programs[0]->Parameters = _mesa_new_parameter_list();
+ shader->Programs[0]->Varying = _mesa_new_parameter_list();
+ shader->Programs[0]->Attributes = _mesa_new_parameter_list();
+ }
+
+ slang_info_log_construct(&info_log);
+ _slang_code_object_ctr(&obj);
+
+ success = compile_shader(ctx, &obj, type, &info_log, shader);
+
+ if (success) {
+#if 0
+ slang_create_uniforms(&object->expdata, shader);
+ _mesa_print_program(program);
+ _mesa_print_program_parameters(ctx, program);
+#endif
+ }
+ else {
+ /* XXX more work on info log needed here */
+ if (info_log.text) {
+ if (shader->InfoLog) {
+ free(shader->InfoLog);
+ shader->InfoLog = NULL;
+ }
+ shader->InfoLog = strdup(info_log.text);
+ }
+ }
+
+ slang_info_log_destruct(&info_log);
+ _slang_code_object_dtr(&obj);
+
+ return success;
+}
+