diff options
Diffstat (limited to 'src/Spv.Generator/Module.cs')
-rw-r--r-- | src/Spv.Generator/Module.cs | 191 |
1 files changed, 95 insertions, 96 deletions
diff --git a/src/Spv.Generator/Module.cs b/src/Spv.Generator/Module.cs index 34ad6036..5e1a9123 100644 --- a/src/Spv.Generator/Module.cs +++ b/src/Spv.Generator/Module.cs @@ -15,30 +15,30 @@ namespace Spv.Generator private uint _bound; // Follow spec order here while keeping it as simple as possible. - private List<Capability> _capabilities; - private List<string> _extensions; - private Dictionary<DeterministicStringKey, Instruction> _extInstImports; + private readonly List<Capability> _capabilities; + private readonly List<string> _extensions; + private readonly Dictionary<DeterministicStringKey, Instruction> _extInstImports; private AddressingModel _addressingModel; private MemoryModel _memoryModel; - private List<Instruction> _entrypoints; - private List<Instruction> _executionModes; - private List<Instruction> _debug; - private List<Instruction> _annotations; + private readonly List<Instruction> _entrypoints; + private readonly List<Instruction> _executionModes; + private readonly List<Instruction> _debug; + private readonly List<Instruction> _annotations; // In the declaration block. - private Dictionary<TypeDeclarationKey, Instruction> _typeDeclarations; + private readonly Dictionary<TypeDeclarationKey, Instruction> _typeDeclarations; // In the declaration block. - private List<Instruction> _globals; + private readonly List<Instruction> _globals; // In the declaration block. - private Dictionary<ConstantKey, Instruction> _constants; + private readonly Dictionary<ConstantKey, Instruction> _constants; // In the declaration block, for function that aren't defined in the module. - private List<Instruction> _functionsDeclarations; + private readonly List<Instruction> _functionsDeclarations; - private List<Instruction> _functionsDefinitions; + private readonly List<Instruction> _functionsDefinitions; - private GeneratorPool<Instruction> _instPool; - private GeneratorPool<LiteralInteger> _integerPool; + private readonly GeneratorPool<Instruction> _instPool; + private readonly GeneratorPool<LiteralInteger> _integerPool; public Module(uint version, GeneratorPool<Instruction> instPool = null, GeneratorPool<LiteralInteger> integerPool = null) { @@ -143,7 +143,7 @@ namespace Spv.Generator _entrypoints.Add(entryPoint); } - public void AddExecutionMode(Instruction function, ExecutionMode mode, params Operand[] parameters) + public void AddExecutionMode(Instruction function, ExecutionMode mode, params IOperand[] parameters) { Debug.Assert(function.Opcode == Op.OpFunction); @@ -225,7 +225,7 @@ namespace Spv.Generator _constants.Add(key, constant); } - public Instruction ExtInst(Instruction resultType, Instruction set, LiteralInteger instruction, params Operand[] parameters) + public Instruction ExtInst(Instruction resultType, Instruction set, LiteralInteger instruction, params IOperand[] parameters) { Instruction result = NewInstruction(Op.OpExtInst, GetNewId(), resultType); @@ -262,104 +262,103 @@ namespace Spv.Generator // Estimate the size needed for the generated code, to avoid expanding the MemoryStream. int sizeEstimate = 1024 + _functionsDefinitions.Count * 32; - using (MemoryStream stream = new MemoryStream(sizeEstimate)) - { - BinaryWriter writer = new BinaryWriter(stream, System.Text.Encoding.ASCII); + using MemoryStream stream = new(sizeEstimate); - // Header - writer.Write(MagicNumber); - writer.Write(_version); - writer.Write(GeneratorId); - writer.Write(_bound); - writer.Write(0u); + BinaryWriter writer = new(stream, System.Text.Encoding.ASCII); - // 1. - foreach (Capability capability in _capabilities) - { - Instruction capabilityInstruction = NewInstruction(Op.OpCapability); + // Header + writer.Write(MagicNumber); + writer.Write(_version); + writer.Write(GeneratorId); + writer.Write(_bound); + writer.Write(0u); - capabilityInstruction.AddOperand(capability); - capabilityInstruction.Write(writer); - } + // 1. + foreach (Capability capability in _capabilities) + { + Instruction capabilityInstruction = NewInstruction(Op.OpCapability); - // 2. - foreach (string extension in _extensions) - { - Instruction extensionInstruction = NewInstruction(Op.OpExtension); + capabilityInstruction.AddOperand(capability); + capabilityInstruction.Write(writer); + } - extensionInstruction.AddOperand(extension); - extensionInstruction.Write(writer); - } + // 2. + foreach (string extension in _extensions) + { + Instruction extensionInstruction = NewInstruction(Op.OpExtension); - // 3. - foreach (Instruction extInstImport in _extInstImports.Values) - { - extInstImport.Write(writer); - } + extensionInstruction.AddOperand(extension); + extensionInstruction.Write(writer); + } - // 4. - Instruction memoryModelInstruction = NewInstruction(Op.OpMemoryModel); - memoryModelInstruction.AddOperand(_addressingModel); - memoryModelInstruction.AddOperand(_memoryModel); - memoryModelInstruction.Write(writer); + // 3. + foreach (Instruction extInstImport in _extInstImports.Values) + { + extInstImport.Write(writer); + } - // 5. - foreach (Instruction entrypoint in _entrypoints) - { - entrypoint.Write(writer); - } + // 4. + Instruction memoryModelInstruction = NewInstruction(Op.OpMemoryModel); + memoryModelInstruction.AddOperand(_addressingModel); + memoryModelInstruction.AddOperand(_memoryModel); + memoryModelInstruction.Write(writer); - // 6. - foreach (Instruction executionMode in _executionModes) - { - executionMode.Write(writer); - } + // 5. + foreach (Instruction entrypoint in _entrypoints) + { + entrypoint.Write(writer); + } - // 7. - // TODO: Order debug information correctly. - foreach (Instruction debug in _debug) - { - debug.Write(writer); - } + // 6. + foreach (Instruction executionMode in _executionModes) + { + executionMode.Write(writer); + } - // 8. - foreach (Instruction annotation in _annotations) - { - annotation.Write(writer); - } + // 7. + // TODO: Order debug information correctly. + foreach (Instruction debug in _debug) + { + debug.Write(writer); + } - // Ensure that everything is in the right order in the declarations section. - List<Instruction> declarations = new List<Instruction>(); - declarations.AddRange(_typeDeclarations.Values); - declarations.AddRange(_globals); - declarations.AddRange(_constants.Values); - declarations.Sort((Instruction x, Instruction y) => x.Id.CompareTo(y.Id)); + // 8. + foreach (Instruction annotation in _annotations) + { + annotation.Write(writer); + } - // 9. - foreach (Instruction declaration in declarations) - { - declaration.Write(writer); - } + // Ensure that everything is in the right order in the declarations section. + List<Instruction> declarations = new(); + declarations.AddRange(_typeDeclarations.Values); + declarations.AddRange(_globals); + declarations.AddRange(_constants.Values); + declarations.Sort((Instruction x, Instruction y) => x.Id.CompareTo(y.Id)); - // 10. - foreach (Instruction functionDeclaration in _functionsDeclarations) - { - functionDeclaration.Write(writer); - } + // 9. + foreach (Instruction declaration in declarations) + { + declaration.Write(writer); + } - // 11. - foreach (Instruction functionDefinition in _functionsDefinitions) - { - functionDefinition.Write(writer); - } + // 10. + foreach (Instruction functionDeclaration in _functionsDeclarations) + { + functionDeclaration.Write(writer); + } - _instPool.Clear(); - _integerPool.Clear(); + // 11. + foreach (Instruction functionDefinition in _functionsDefinitions) + { + functionDefinition.Write(writer); + } - LiteralInteger.UnregisterPool(); + _instPool.Clear(); + _integerPool.Clear(); - return stream.ToArray(); - } + LiteralInteger.UnregisterPool(); + + return stream.ToArray(); } } } |