# Compilation The compilation process goes through 3 steps : 1. Parsing of shader mixins 2. Generation of SPIR-V from mixins, resulting in SPIR-V mixins 3. Mixing of those SPIR-V bytecode into a final shader The previous implementation was working through the AST to do the mixin system, going for the SPIR-V mixin system helps us mix with binary data instead of textual. This also enables us to use SPIR-V Cross and Naga for cross shader compilation. ## Parsing ### Grammars Parsing is done with Eto.Parse, [the SDSL grammar](https://github.com/ykafia/SDSLParser/tree/master/src/SDSL/Parsers/Grammars/SDSLGrammar) is what defines the language. There is also a grammar for [comments](https://github.com/ykafia/SDSLParser/blob/master/src/SDSL/Parsers/Grammars/CommentGrammar.cs) and [directives](https://github.com/ykafia/SDSLParser/tree/master/src/SDSL/Parsers/Grammars/DirectiveGrammar), the latter is not used fully functional so directives are handled by [CPP.NET](https://github.com/MonoGame/CppNet). For the langage there is first a comment prepass where comments are removed, then the directive compilation and finally the resulting code is transformed into an AST ### AST The [Abstract syntax tree](https://github.com/ykafia/SDSLParser/tree/master/src/SDSL/Parsers/AST/Shader) is built right after the syntax parse tree is generated in [the GetToken method](https://github.com/ykafia/SDSLParser/blob/master/src/SDSL/Parsers/AST/Shader/ShaderToken.cs#L29). As opposed to the previous shader system, those AST are not supposed to be operated on as they will only serve to generate partial spir-v binaries. ### Semantic analysis Some semantic analysis passes include : * Type checking * Scope resolution * Declaration rules * Flow control checks * Unique ID checks * Control flow analysis The control flow analysis generates a control flow graph (CFG) that will help heavily in generating the SPIR-V assembly. ## SPIR-V Assembler The backend will use [SoftTouch.Spirv](https://github.com/SoftTouch.Spirv) to generate spir-v assembly. In this library the spir-v specification has been extended to include instructions for SDSL, helping the compiler build final spir-v shaders from mixins. In this library a `Mixer` can be created to generate a new mixin. It can do operations like `Inherit`, `Compose` and also generate functions through a small abstraction over spir-v. It's designed to have very low allocations, meaning all unecessary allocations are avoided, to make sure shader compilation doesn't hinder on performance.