How it works

The source stays where the caller keeps it. A token, an identifier or a member name is a view of it, a 16-bit position and length, so a source is 65535 bytes at most; the lexer scans a copy in the workspace with zero bytes after its end, so it reads ahead without asking where the source ends. Three passes write arrays of fixed records that refer to each other by 16-bit index, never by pointer, all carved from the caller's workspace by each compilation, which clears none of it: the emitter's hash of types and constants says which slots are taken with a sparse set, which nothing the workspace held can fake.

A token's kind is 32 bits. A punctuator's is its characters, packed from the first in the low byte ('(', '+' | '=' << 8), made from the bytes one at a time, so the same number on a big-endian host; keywords and the built-in types are past them. Every identifier is interned: one name for each text, by hash, and the token's kind is the name's index with the top bit set, so two tokens name the same thing when their kinds are equal. A name holds what the passes learn of it once: the macro it names, its keyword row (one slot of a perfect hash that tools/glslc_kwgen makes of builtins.c's keyword table as glslc is built), its innermost symbol in scope (each symbol keeps the one it hides), and its rows of the built-in functions and variables. The tables' names carry their lengths, known when compiled (builtins.c's ROW), and no type's name is made unless an error shows it.

  1. lexer.c: the preprocessor and the tokens. Macros expand on a stack of tokens: a use's arguments are expanded, put into the body, and the result scanned again with the macro turned off, each step on top of the last.
  2. parser.c: typed syntax nodes, by recursive descent. GLSL declares every name before its use, so the same pass resolves names, checks types and folds the scalar constant expressions that array sizes, case labels and layout values need.
  3. emit.c: SPIR-V, section by section, put together at the end.

builtins.c holds the tables: the built-in types, the words each version keeps (checked against glslangValidator word by word), the built-in variables and constants, and the built-in functions with the SPIR-V each becomes.

Every array has an upper bound. A shader that needs more is refused, and the error names the bound and what the shader needed (the shader has 32769 tokens after preprocessing, more than the 32768 glslc takes):

BoundValueWhat it counts
SNX_GLSLC_MAX_SOURCE65535bytes of source
SNX_GLSLC_MAX_TOKENS32768tokens after preprocessing
SNX_GLSLC_MAX_NODES32768syntax nodes; no construct makes more than it has tokens
SNX_GLSLC_MAX_MACROS512macros, the predefined ones the shader reads included
SNX_GLSLC_MAX_NAMES8192distinct names the preprocessor reads, the predefined macros' included
SNX_GLSLC_MAX_MACRO_TOKENS8192tokens of every macro's parameters and body
SNX_GLSLC_MAX_EXPANSION4096tokens one macro use expands to
SNX_GLSLC_MAX_MACRO_DEPTH32macros expanding inside each other
SNX_GLSLC_MAX_IF_DEPTH64#if nested
SNX_GLSLC_MAX_DEPTH96expressions and statements nested
SNX_GLSLC_MAX_SCOPES64scopes nested
SNX_GLSLC_MAX_TYPES2048types, the built-in ones included
SNX_GLSLC_MAX_SYMBOLS4096names declared
SNX_GLSLC_MAX_WORDS262144words of SPIR-V

glslc.h lists the others. The nesting bounds also bound the C stack each pass takes.


Made from docs/layers/gpu/glslc.md at 69237645142c. GPL-3.0-or-later.