From 9b2b841a550fd21e048bacedcb51a6cfd847b12c Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Sat, 31 Aug 2024 01:15:07 +0900 Subject: [PATCH] [qfcc] Redo interface blocks as namespaces There's no direct support for namespaces in Ruamoko yet, nor even in qfcc, but glsl's blocks bring in a bit of foundation for them, even the concept of "using" (for blocks with no instance name). The members don't get locations allocated to them yet, but fstrianglest.vert compiles and links correctly otherwise. Also, there's no error checking yet. --- tools/qfcc/include/glsl-lang.h | 13 +++ tools/qfcc/include/symtab.h | 4 +- tools/qfcc/source/Makemodule.am | 1 + tools/qfcc/source/expr_assign.c | 1 + tools/qfcc/source/glsl-block.c | 143 ++++++++++++++++++++++++++++++ tools/qfcc/source/glsl-builtins.c | 19 ++-- tools/qfcc/source/glsl-parse.y | 37 ++++---- tools/qfcc/source/struct.c | 2 +- 8 files changed, 197 insertions(+), 23 deletions(-) create mode 100644 tools/qfcc/source/glsl-block.c diff --git a/tools/qfcc/include/glsl-lang.h b/tools/qfcc/include/glsl-lang.h index ed1540d59..a51cdb7ab 100644 --- a/tools/qfcc/include/glsl-lang.h +++ b/tools/qfcc/include/glsl-lang.h @@ -45,4 +45,17 @@ extern language_t lang_glsl_tese; extern language_t lang_glsl_geom; extern language_t lang_glsl_frag; +typedef struct glsl_block_s { + struct glsl_block_s *next; + const char *name; + struct symtab_s *members; + struct symbol_s *instance_name; +} glsl_block_t; + +struct specifier_s; + +void glsl_block_clear (void); +void glsl_declare_block (struct specifier_s spec, struct symbol_s *block_sym, + struct symbol_s *instance_name); + #endif//__glsl_lang_h diff --git a/tools/qfcc/include/symtab.h b/tools/qfcc/include/symtab.h index ee21c292f..a42f4e1ce 100644 --- a/tools/qfcc/include/symtab.h +++ b/tools/qfcc/include/symtab.h @@ -59,6 +59,7 @@ typedef enum { sy_class, ///< symbol refers to a class sy_convert, ///< symbol refers to a conversion function sy_macro, ///< symbol refers to a macro definition + sy_namespace, ///< symbol refers to a namespace definition } sy_type_e; typedef struct symconv_s { @@ -74,7 +75,7 @@ typedef struct symbol_s { sy_type_e sy_type; ///< symbol type const struct type_s *type; ///< type of object to which symbol refers struct param_s *params; ///< the parameters if a function - unsigned no_auto_init; ///< skip for non-designated initializers + bool no_auto_init:1; ///< skip for non-designated initializers union { int offset; ///< sy_var (in a struct/union/macro) struct def_s *def; ///< sy_var @@ -83,6 +84,7 @@ typedef struct symbol_s { struct metafunc_s *metafunc;///< sy_func symconv_t convert; ///< sy_convert struct rua_macro_s *macro; ///< sy_macro + struct symtab_s *namespace; ///< sy_namespace }; } symbol_t; diff --git a/tools/qfcc/source/Makemodule.am b/tools/qfcc/source/Makemodule.am index ebd7cc642..dcc3346d6 100644 --- a/tools/qfcc/source/Makemodule.am +++ b/tools/qfcc/source/Makemodule.am @@ -41,6 +41,7 @@ qfcc_SOURCES = \ tools/qfcc/source/evaluate.c \ tools/qfcc/source/flow.c \ tools/qfcc/source/function.c \ + tools/qfcc/source/glsl-block.c \ tools/qfcc/source/glsl-builtins.c \ tools/qfcc/source/glsl-parse.y \ tools/qfcc/source/grab.c \ diff --git a/tools/qfcc/source/expr_assign.c b/tools/qfcc/source/expr_assign.c index 9b417938f..053be4b5c 100644 --- a/tools/qfcc/source/expr_assign.c +++ b/tools/qfcc/source/expr_assign.c @@ -107,6 +107,7 @@ is_lvalue (const expr_t *expr) case sy_convert: break; case sy_macro: + case sy_namespace: break; } break; diff --git a/tools/qfcc/source/glsl-block.c b/tools/qfcc/source/glsl-block.c new file mode 100644 index 000000000..4aa1d270f --- /dev/null +++ b/tools/qfcc/source/glsl-block.c @@ -0,0 +1,143 @@ +/* + glsl-block.c + + GLSL specific block handling + + Copyright (C) 2024 Bill Currie + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to: + + Free Software Foundation, Inc. + 59 Temple Place - Suite 330 + Boston, MA 02111-1307, USA + +*/ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include "QF/alloc.h" +#include "QF/hash.h" +#include "QF/va.h" + +#include "tools/qfcc/include/def.h" +#include "tools/qfcc/include/diagnostic.h" +#include "tools/qfcc/include/glsl-lang.h" +#include "tools/qfcc/include/shared.h" +#include "tools/qfcc/include/strpool.h" +#include "tools/qfcc/include/symtab.h" + +ALLOC_STATE (glsl_block_t, blocks); + +static hashtab_t *input_blocks; +static hashtab_t *output_blocks; +static hashtab_t *uniform_blocks; +static hashtab_t *buffer_blocks; +static hashtab_t *shared_blocks; + +static const char * +block_get_key (const void *_b, void *) +{ + auto b = (const glsl_block_t *) _b; + return b->name; +} + +void +glsl_block_clear (void) +{ + if (input_blocks) { + Hash_FlushTable (input_blocks); + Hash_FlushTable (output_blocks); + Hash_FlushTable (uniform_blocks); + Hash_FlushTable (buffer_blocks); + Hash_FlushTable (shared_blocks); + } else { + input_blocks = Hash_NewTable (127, block_get_key, 0, 0, 0); + output_blocks = Hash_NewTable (127, block_get_key, 0, 0, 0); + uniform_blocks = Hash_NewTable (127, block_get_key, 0, 0, 0); + buffer_blocks = Hash_NewTable (127, block_get_key, 0, 0, 0); + shared_blocks = Hash_NewTable (127, block_get_key, 0, 0, 0); + } +} + +static const expr_t * +block_sym_ref (symbol_t *sym, void *data) +{ + return new_symbol_expr (data); +} + +void +glsl_declare_block (specifier_t spec, symbol_t *block_sym, + symbol_t *instance_name) +{ + hashtab_t *block_tab = nullptr; + switch (spec.storage) { + case sc_in: + block_tab = input_blocks; + break; + case sc_out: + block_tab = output_blocks; + break; + case sc_uniform: + block_tab = uniform_blocks; + break; + case sc_buffer: + block_tab = buffer_blocks; + break; + case sc_shared: + block_tab = shared_blocks; + break; + case sc_global: + case sc_system: + case sc_extern: + case sc_static: + case sc_param: + case sc_local: + case sc_argument: + break; + } + if (!block_tab) { + error (0, "invalid storage for block"); + return; + } + glsl_block_t *block; + ALLOC (64, glsl_block_t, blocks, block); + *block = (glsl_block_t) { + .name = save_string (block_sym->name), + .members = block_sym->namespace, + .instance_name = instance_name, + }; + Hash_Add (block_tab, block); + for (auto sym = block->members->symbols; sym; sym = sym->next) { + auto def = new_def (sym->name, nullptr, nullptr, spec.storage); + def->type = sym->type; + sym->sy_type = sy_var; + sym->def = def; + } + if (instance_name) { + //namespace_add (instance_name, block->members); + } else { + for (auto sym = block->members->symbols; sym; sym = sym->next) { + auto new = new_symbol (sym->name); + new->sy_type = sy_convert; + new->convert = (symconv_t) { + .conv = block_sym_ref, + .data = sym, + }; + + symtab_addsymbol (current_symtab, new); + } + } +} diff --git a/tools/qfcc/source/glsl-builtins.c b/tools/qfcc/source/glsl-builtins.c index cd697231b..611d19837 100644 --- a/tools/qfcc/source/glsl-builtins.c +++ b/tools/qfcc/source/glsl-builtins.c @@ -823,44 +823,51 @@ glsl_parse_vars (const char *var_src) glsl_parse_string (var_src); } +static void +glsl_init_common (void) +{ + glsl_block_clear (); + glsl_parse_vars (glsl_system_constants); +} + void glsl_init_comp (void) { - glsl_parse_vars (glsl_system_constants); + glsl_init_common (); glsl_parse_vars (glsl_compute_vars); } void glsl_init_vert (void) { - glsl_parse_vars (glsl_system_constants); + glsl_init_common (); glsl_parse_vars (glsl_Vulkan_vertex_vars); } void glsl_init_tesc (void) { - glsl_parse_vars (glsl_system_constants); + glsl_init_common (); glsl_parse_vars (glsl_tesselation_control_vars); } void glsl_init_tese (void) { - glsl_parse_vars (glsl_system_constants); + glsl_init_common (); glsl_parse_vars (glsl_tesselation_evaluation_vars); } void glsl_init_geom (void) { - glsl_parse_vars (glsl_system_constants); + glsl_init_common (); glsl_parse_vars (glsl_geometry_vars); } void glsl_init_frag (void) { - glsl_parse_vars (glsl_system_constants); + glsl_init_common (); glsl_parse_vars (glsl_fragment_vars); } diff --git a/tools/qfcc/source/glsl-parse.y b/tools/qfcc/source/glsl-parse.y index 7a0a4fcc6..bdd95b658 100644 --- a/tools/qfcc/source/glsl-parse.y +++ b/tools/qfcc/source/glsl-parse.y @@ -162,7 +162,7 @@ int yylex (YYSTYPE *yylval, YYLTYPE *yylloc); %token LOW_PRECISION DISCARD COHERENT %type variable_identifier -%type block_declaration +%type block_declaration %type expression primary_exprsssion assignment_expression %type for_init_statement conditionopt expressionopt else %type conditional_expression unary_expression postfix_expression @@ -546,17 +546,23 @@ declaration { auto spec = $1; auto block = $2; - for (auto s = block->symbols; s; s = s->next) { - auto b_spec = spec_merge (spec, (specifier_t) { - .type = s->type, - .sym = new_symbol (s->name), - }); - b_spec.storage = sc_extern; - declare_symbol (b_spec, nullptr, current_symtab); - } + glsl_declare_block (spec, block, nullptr); } | type_qualifier block_declaration IDENTIFIER ';' + { + auto spec = $1; + auto block = $2; + auto instance_name = $3; + glsl_declare_block (spec, block, instance_name); + } | type_qualifier block_declaration IDENTIFIER array_specifier ';' + { + auto spec = $1; + auto block = $2; + auto instance_name = $3; + instance_name->type = $4; + glsl_declare_block (spec, block, instance_name); + } | type_qualifier ';' | type_qualifier IDENTIFIER ';' | type_qualifier IDENTIFIER identifier_list ';' @@ -565,15 +571,16 @@ declaration block_declaration : IDENTIFIER '{' { - int op = 's';//FIXME 'b' might be better (for block) - auto sym = $1; - current_symtab = start_struct (&op, sym, current_symtab); + auto block = new_symtab (current_symtab, stab_struct); + current_symtab = block; } struct_declaration_list '}' { - auto block = current_symtab; - current_symtab = block->parent; - $$ = block; + auto sym = $1; + sym->sy_type = sy_namespace; + sym->namespace = current_symtab; + current_symtab = sym->namespace->parent; + $$ = sym; } ; diff --git a/tools/qfcc/source/struct.c b/tools/qfcc/source/struct.c index 2b96f7900..ecef5f4c1 100644 --- a/tools/qfcc/source/struct.c +++ b/tools/qfcc/source/struct.c @@ -212,7 +212,7 @@ build_struct (int su, symbol_t *tag, symtab_t *symtab, const type_t *type, s = s->next; s->offset += offset; s->table = symtab; - s->no_auto_init = 1; + s->no_auto_init = true; Hash_Add (symtab->tab, s); } }