[qfcc] Build namespaces for non-instanced blocks

Each interface type (in, out, uniform etc) gets its own namespace, and
non-instanced blocks get a namespace (their block name) within the
interface namespace.

The defs for the block members are currently "allocated" to be at offset
-1, but the idea is to allow layout qualifiers to know if the member has
already been located.
This commit is contained in:
Bill Currie 2024-09-12 13:33:42 +09:00
parent 1dc31f83af
commit 99be6f6e41
4 changed files with 48 additions and 5 deletions

View file

@ -36,6 +36,7 @@ typedef struct type_s type_t;
typedef struct symbol_s symbol_t; typedef struct symbol_s symbol_t;
typedef struct symtab_s symtab_t; typedef struct symtab_s symtab_t;
typedef struct language_s language_t; typedef struct language_s language_t;
typedef struct defspace_s defspace_t;
void glsl_init_comp (void); void glsl_init_comp (void);
void glsl_init_vert (void); void glsl_init_vert (void);
@ -73,6 +74,7 @@ typedef struct glsl_block_s {
glsl_interface_t interface; glsl_interface_t interface;
symtab_t *attributes; symtab_t *attributes;
symtab_t *members; symtab_t *members;
defspace_t *space;
symbol_t *instance_name; symbol_t *instance_name;
} glsl_block_t; } glsl_block_t;

View file

@ -90,6 +90,7 @@ typedef enum {
stab_union, stab_union,
stab_enum, stab_enum,
stab_label, stab_label,
stab_block,
} stab_type_e; } stab_type_e;
typedef struct symtab_s { typedef struct symtab_s {

View file

@ -33,6 +33,7 @@
#include "QF/va.h" #include "QF/va.h"
#include "tools/qfcc/include/def.h" #include "tools/qfcc/include/def.h"
#include "tools/qfcc/include/defspace.h"
#include "tools/qfcc/include/diagnostic.h" #include "tools/qfcc/include/diagnostic.h"
#include "tools/qfcc/include/glsl-lang.h" #include "tools/qfcc/include/glsl-lang.h"
#include "tools/qfcc/include/shared.h" #include "tools/qfcc/include/shared.h"
@ -63,6 +64,16 @@ glsl_block_clear (void)
interfaces[i] = Hash_NewTable (127, block_get_key, 0, 0, 0); interfaces[i] = Hash_NewTable (127, block_get_key, 0, 0, 0);
} }
} }
for (auto i = glsl_in; i < glsl_num_interfaces; i++) {
auto name = glsl_interface_names[i];
if (symtab_lookup (current_symtab, name)) {
internal_error (0, "%s already declared", name);
}
auto sym = new_symbol (name);
sym->sy_type = sy_namespace;
sym->namespace = new_symtab (nullptr, stab_block);
symtab_addsymbol (current_symtab, sym);
}
} }
static const expr_t * static const expr_t *
@ -77,6 +88,13 @@ block_sym_ref (symbol_t *sym, void *data)
return expr; return expr;
} }
static int
glsl_block_alloc_loc (defspace_t *space, int size, int alignment)
{
// XXX
return -1;
}
glsl_block_t * glsl_block_t *
glsl_create_block (specifier_t spec, symbol_t *block_sym) glsl_create_block (specifier_t spec, symbol_t *block_sym)
{ {
@ -96,8 +114,10 @@ glsl_create_block (specifier_t spec, symbol_t *block_sym)
.interface = interface, .interface = interface,
.attributes = glsl_optimize_attributes (spec.attributes), .attributes = glsl_optimize_attributes (spec.attributes),
.members = new_symtab (current_symtab, stab_struct), .members = new_symtab (current_symtab, stab_struct),
.space = defspace_new (ds_backed),
}; };
block->members->data = block; block->members->data = block;
block->space->alloc_aligned = glsl_block_alloc_loc;
Hash_Add (block_tab, block); Hash_Add (block_tab, block);
for (auto sym = block->members->symbols; sym; sym = sym->next) { for (auto sym = block->members->symbols; sym; sym = sym->next) {
auto def = new_def (sym->name, nullptr, nullptr, spec.storage); auto def = new_def (sym->name, nullptr, nullptr, spec.storage);
@ -111,6 +131,12 @@ glsl_create_block (specifier_t spec, symbol_t *block_sym)
void void
glsl_finish_block (glsl_block_t *block) glsl_finish_block (glsl_block_t *block)
{ {
for (auto sym = block->members->symbols; sym; sym = sym->next) {
if (sym->sy_type == sy_var) {
//FIXME sc_extern isn't correct (problem with unsized arrays)
sym->def = new_def (sym->name, sym->type, block->space, sc_extern);
}
}
} }
void void
@ -120,6 +146,13 @@ glsl_declare_block_instance (glsl_block_t *block, symbol_t *instance_name)
// error recovery // error recovery
return; return;
} }
auto interface_name = glsl_interface_names[block->interface];
auto interface_sym = symtab_lookup (current_symtab, interface_name);
if (!interface_sym) {
internal_error (0, "%s interface not defined", interface_name);
}
auto interface = interface_sym->namespace;
if (instance_name) { if (instance_name) {
block->instance_name = instance_name; block->instance_name = instance_name;
auto type = new_type (); auto type = new_type ();
@ -139,6 +172,16 @@ glsl_declare_block_instance (glsl_block_t *block, symbol_t *instance_name)
glsl_sc_from_iftype (block->interface), glsl_sc_from_iftype (block->interface),
current_symtab); current_symtab);
} else { } else {
auto block_sym = symtab_lookup (interface, block->name->name);
if (block_sym) {
error (0, "%s block %s redeclared", interface_name,
block_sym->name);
} else {
block_sym = block->name;
block_sym->sy_type = sy_namespace;
block_sym->namespace = block->members;
symtab_addsymbol (interface, block->name);
}
for (auto sym = block->members->symbols; sym; sym = sym->next) { for (auto sym = block->members->symbols; sym; sym = sym->next) {
auto new = new_symbol (sym->name); auto new = new_symbol (sym->name);
new->sy_type = sy_convert; new->sy_type = sy_convert;

View file

@ -619,8 +619,8 @@ block_declaration
: IDENTIFIER '{' : IDENTIFIER '{'
{ {
auto spec = $<spec>0; auto spec = $<spec>0;
auto block_sym = $IDENTIFIER; auto sym = $IDENTIFIER;
auto block = glsl_create_block (spec, block_sym); auto block = glsl_create_block (spec, sym);
if (block) { if (block) {
current_symtab = block->members; current_symtab = block->members;
} }
@ -631,10 +631,7 @@ block_declaration
auto block = $<block>3; auto block = $<block>3;
if (block) { if (block) {
current_symtab = block->members->parent; current_symtab = block->members->parent;
auto sym = $IDENTIFIER;
glsl_finish_block (block); glsl_finish_block (block);
sym->sy_type = sy_namespace;
sym->namespace = current_symtab;
} }
$$ = block; $$ = block;
} }