-/*\r
- * Mesa 3-D graphics library\r
- * Version: 6.5\r
- *\r
- * Copyright (C) 2005-2006 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_function.c\r
- * slang front-end compiler\r
- * \author Michal Krol\r
- */\r
-\r
-#include "imports.h"\r
-#include "slang_compile.h"\r
-\r
-/* slang_fixup_table */\r
-\r
-void slang_fixup_table_init (slang_fixup_table *fix)\r
-{\r
- fix->table = NULL;\r
- fix->count = 0;\r
-}\r
-\r
-void slang_fixup_table_free (slang_fixup_table *fix)\r
-{\r
- slang_alloc_free (fix->table);\r
- slang_fixup_table_init (fix);\r
-}\r
-\r
-/* slang_function */\r
-\r
-int slang_function_construct (slang_function *func)\r
-{\r
- func->kind = slang_func_ordinary;\r
- if (!slang_variable_construct (&func->header))\r
- return 0;\r
- func->parameters = (slang_variable_scope *) slang_alloc_malloc (sizeof (slang_variable_scope));\r
- if (func->parameters == NULL)\r
- {\r
- slang_variable_destruct (&func->header);\r
- return 0;\r
- }\r
- if (!slang_variable_scope_construct (func->parameters))\r
- {\r
- slang_alloc_free (func->parameters);\r
- slang_variable_destruct (&func->header);\r
- return 0;\r
- }\r
- func->param_count = 0;\r
- func->body = NULL;\r
- func->address = ~0;\r
- slang_fixup_table_init (&func->fixups);\r
- return 1;\r
-}\r
-\r
-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
- slang_fixup_table_free (&func->fixups);\r
-}\r
-\r
-/* slang_function_scope */\r
-\r
-int slang_function_scope_construct (slang_function_scope *scope)\r
-{\r
- scope->functions = NULL;\r
- scope->num_functions = 0;\r
- scope->outer_scope = NULL;\r
- return 1;\r
-}\r
-\r
-void slang_function_scope_destruct (slang_function_scope *scope)\r
-{\r
- unsigned int i;\r
-\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
-int slang_function_scope_find_by_name (slang_function_scope *funcs, slang_atom a_name, int all_scopes)\r
-{\r
- unsigned int i;\r
-\r
- for (i = 0; i < funcs->num_functions; i++)\r
- if (a_name == funcs->functions[i].header.a_name)\r
- return 1;\r
- if (all_scopes && funcs->outer_scope != NULL)\r
- return slang_function_scope_find_by_name (funcs->outer_scope, a_name, 1);\r
- return 0;\r
-}\r
-\r
-slang_function *slang_function_scope_find (slang_function_scope *funcs, slang_function *fun,\r
- int all_scopes)\r
-{\r
- unsigned int i;\r
-\r
- for (i = 0; i < funcs->num_functions; i++)\r
- {\r
- slang_function *f = &funcs->functions[i];\r
- unsigned int j;\r
-\r
- if (fun->header.a_name != f->header.a_name)\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
- break;\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
-/*\r
- * _slang_build_export_code_table()\r
- */\r
-\r
-GLboolean _slang_build_export_code_table (slang_export_code_table *tbl, slang_function_scope *funs,\r
- slang_translation_unit *unit)\r
-{\r
- slang_atom mainAtom;\r
- GLuint i;\r
-\r
- mainAtom = slang_atom_pool_atom (tbl->atoms, "main");\r
- if (mainAtom == SLANG_ATOM_NULL)\r
- return GL_FALSE;\r
-\r
- for (i = 0; i < funs->num_functions; i++)\r
- {\r
- if (funs->functions[i].header.a_name == mainAtom)\r
- {\r
- slang_function *fun = &funs->functions[i];\r
- slang_export_code_entry *e;\r
- slang_assemble_ctx A;\r
-\r
- e = slang_export_code_table_add (tbl);\r
- if (e == NULL)\r
- return GL_FALSE;\r
- e->address = unit->assembly->count;\r
- e->name = slang_atom_pool_atom (tbl->atoms, "@main");\r
- if (e->name == SLANG_ATOM_NULL)\r
- return GL_FALSE;\r
-\r
- A.file = unit->assembly;\r
- A.mach = unit->machine;\r
- A.atoms = unit->atom_pool;\r
- A.space.funcs = &unit->functions;\r
- A.space.structs = &unit->structs;\r
- A.space.vars = &unit->globals;\r
- slang_assembly_file_push_label (unit->assembly, slang_asm_local_alloc, 20);\r
- slang_assembly_file_push_label (unit->assembly, slang_asm_enter, 20);\r
- _slang_assemble_function_call (&A, fun, NULL, 0, GL_FALSE);\r
- slang_assembly_file_push (unit->assembly, slang_asm_exit);\r
- }\r
- }\r
- return GL_TRUE;\r
-}\r
-\r
+/*
+ * Mesa 3-D graphics library
+ * Version: 6.5
+ *
+ * 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_function.c
+ * slang front-end compiler
+ * \author Michal Krol
+ */
+
+#include "imports.h"
+#include "slang_compile.h"
+
+/* slang_fixup_table */
+
+void
+slang_fixup_table_init(slang_fixup_table * fix)
+{
+ fix->table = NULL;
+ fix->count = 0;
+}
+
+void
+slang_fixup_table_free(slang_fixup_table * fix)
+{
+ slang_alloc_free(fix->table);
+ slang_fixup_table_init(fix);
+}
+
+/**
+ * Add a new fixup address to the table.
+ */
+GLboolean
+slang_fixup_save(slang_fixup_table *fixups, GLuint address)
+{
+ fixups->table = (GLuint *)
+ slang_alloc_realloc(fixups->table,
+ fixups->count * sizeof(GLuint),
+ (fixups->count + 1) * sizeof(GLuint));
+ if (fixups->table == NULL)
+ return GL_FALSE;
+ fixups->table[fixups->count] = address;
+ fixups->count++;
+ return GL_TRUE;
+}
+
+
+
+/* slang_function */
+
+int
+slang_function_construct(slang_function * func)
+{
+ func->kind = slang_func_ordinary;
+ if (!slang_variable_construct(&func->header))
+ return 0;
+
+ func->parameters = (slang_variable_scope *)
+ slang_alloc_malloc(sizeof(slang_variable_scope));
+ if (func->parameters == NULL) {
+ slang_variable_destruct(&func->header);
+ return 0;
+ }
+
+ _slang_variable_scope_ctr(func->parameters);
+ func->param_count = 0;
+ func->body = NULL;
+ func->address = ~0;
+ func->end_label = 0;
+ slang_fixup_table_init(&func->fixups);
+ return 1;
+}
+
+void
+slang_function_destruct(slang_function * func)
+{
+ slang_variable_destruct(&func->header);
+ slang_variable_scope_destruct(func->parameters);
+ slang_alloc_free(func->parameters);
+ if (func->body != NULL) {
+ slang_operation_destruct(func->body);
+ slang_alloc_free(func->body);
+ }
+ slang_fixup_table_free(&func->fixups);
+}
+
+/*
+ * slang_function_scope
+ */
+
+GLvoid
+_slang_function_scope_ctr(slang_function_scope * self)
+{
+ self->functions = NULL;
+ self->num_functions = 0;
+ self->outer_scope = NULL;
+}
+
+void
+slang_function_scope_destruct(slang_function_scope * scope)
+{
+ unsigned int i;
+
+ for (i = 0; i < scope->num_functions; i++)
+ slang_function_destruct(scope->functions + i);
+ slang_alloc_free(scope->functions);
+}
+
+
+/**
+ * Does this function have a non-void return value?
+ */
+GLboolean
+_slang_function_has_return_value(const slang_function *fun)
+{
+ return fun->header.type.specifier.type != slang_spec_void;
+}
+
+
+/**
+ * Search a list of functions for a particular function by name.
+ * \param funcs the list of functions to search
+ * \param a_name the name to search for
+ * \param all_scopes if non-zero, search containing scopes too.
+ * \return pointer to found function, or NULL.
+ */
+int
+slang_function_scope_find_by_name(slang_function_scope * funcs,
+ slang_atom a_name, int all_scopes)
+{
+ unsigned int i;
+
+ for (i = 0; i < funcs->num_functions; i++)
+ if (a_name == funcs->functions[i].header.a_name)
+ return 1;
+ if (all_scopes && funcs->outer_scope != NULL)
+ return slang_function_scope_find_by_name(funcs->outer_scope, a_name, 1);
+ return 0;
+}
+
+
+/**
+ * Search a list of functions for a particular function (for implementing
+ * function calls. Matching is done by first comparing the function's name,
+ * then the function's parameter list.
+ *
+ * \param funcs the list of functions to search
+ * \param fun the function to search for
+ * \param all_scopes if non-zero, search containing scopes too.
+ * \return pointer to found function, or NULL.
+ */
+slang_function *
+slang_function_scope_find(slang_function_scope * funcs, slang_function * fun,
+ int all_scopes)
+{
+ unsigned int i;
+
+ for (i = 0; i < funcs->num_functions; i++) {
+ slang_function *f = &funcs->functions[i];
+ const GLuint haveRetValue = 0;
+#if 0
+ = (f->header.type.specifier.type != slang_spec_void);
+#endif
+ unsigned int j;
+
+ /*
+ printf("Compare name %s to %s (ret %u, %d, %d)\n",
+ (char *) fun->header.a_name, (char *) f->header.a_name,
+ haveRetValue,
+ fun->param_count, f->param_count);
+ */
+
+ if (fun->header.a_name != f->header.a_name)
+ continue;
+ if (fun->param_count != f->param_count)
+ continue;
+ for (j = haveRetValue; j < fun->param_count; j++) {
+ if (!slang_type_specifier_equal
+ (&fun->parameters->variables[j]->type.specifier,
+ &f->parameters->variables[j]->type.specifier))
+ break;
+ }
+ if (j == fun->param_count) {
+ /*
+ printf("Found match\n");
+ */
+ return f;
+ }
+ }
+ /*
+ printf("Not found\n");
+ */
+ if (all_scopes && funcs->outer_scope != NULL)
+ return slang_function_scope_find(funcs->outer_scope, fun, 1);
+ return NULL;
+}
+
+/*
+ * _slang_build_export_code_table()
+ */
+
+GLboolean
+_slang_build_export_code_table(slang_export_code_table * tbl,
+ slang_function_scope * funs,
+ slang_code_unit * unit)
+{
+ slang_atom mainAtom;
+ GLuint i;
+
+ mainAtom = slang_atom_pool_atom(tbl->atoms, "main");
+ if (mainAtom == SLANG_ATOM_NULL)
+ return GL_FALSE;
+
+ for (i = 0; i < funs->num_functions; i++) {
+ if (funs->functions[i].header.a_name == mainAtom) {
+ slang_function *fun = &funs->functions[i];
+ slang_export_code_entry *e;
+ slang_assemble_ctx A;
+
+ e = slang_export_code_table_add(tbl);
+ if (e == NULL)
+ return GL_FALSE;
+ e->address = unit->object->assembly.count;
+ e->name = slang_atom_pool_atom(tbl->atoms, "@main");
+ if (e->name == SLANG_ATOM_NULL)
+ return GL_FALSE;
+
+ A.file = &unit->object->assembly;
+ A.mach = &unit->object->machine;
+ A.atoms = &unit->object->atompool;
+ A.space.funcs = &unit->funs;
+ A.space.structs = &unit->structs;
+ A.space.vars = &unit->vars;
+ slang_assembly_file_push_label(&unit->object->assembly,
+ slang_asm_local_alloc, 20);
+ slang_assembly_file_push_label(&unit->object->assembly,
+ slang_asm_enter, 20);
+ _slang_assemble_function_call(&A, fun, NULL, 0, GL_FALSE);
+ slang_assembly_file_push(&unit->object->assembly, slang_asm_exit);
+ }
+ }
+ return GL_TRUE;
+}