spirv-utils 0.2.1

SPIR-V Utilities library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
// Copyright 2016 James Miller
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

extern crate lalrpop_util;

use std::borrow::{Cow};
use std::env;
use std::fs::File;
use std::io::{Result, BufRead, BufReader, Read, Write};
use std::path::Path;

use lalrpop_util::ParseError;

pub mod util {
    pub mod codegen;
    pub mod parser;
}

use util::codegen::CodeFile;
use util::parser::parse_Description;

#[derive(Debug)]
pub struct Group {
    name: String,
    instructions: Vec<Instruction>
}

#[derive(Debug)]
pub struct Instruction {
    opcode: u16,
    name: String,
    params: Vec<Param>,
    group: Option<String>
}

#[derive(Debug)]
pub struct Param {
    name: String,
    ty: ParamTy,
}

#[derive(Debug)]
pub enum ParamTy {
    Single(Ty, bool),
    Repeat(Ty),
    RepeatMany(Vec<Ty>)
}

fn main() {
    println!("cargo:rerun-if-changed=build.rs");
    println!("cargo:rerun-if-changed=desc/core.desc");
    println!("cargo:rerun-if-changed=util/parser.rs");

    let dest = env::var("OUT_DIR").unwrap();
    let dest = Path::new(&dest);

    let mut input = File::open("desc/core.desc").unwrap();

    let mut buf = String::new();
    input.read_to_string(&mut buf);

    let instructions = parse_Description(&buf);

    match instructions {
        Ok(instructions) => {
            let instructions : Vec<_> = instructions.into_iter().flat_map(|group| {
                let name = group.name;
                group.instructions.into_iter().map(move |mut i| {
                    i.group = Some(name.clone());
                    i
                })
            }).collect();

            let insts_output = CodeFile::create(&dest.join("insts.rs"));
            gen_insts(&instructions, insts_output).unwrap();

            let parser_output = CodeFile::create(&dest.join("inst_parser.rs"));
            gen_parser(&instructions, parser_output).unwrap();
        }
        Err(e) => {
            let mut stderr = std::io::stderr();

            let _ = writeln!(stderr, "Error parsing core.desc: {:?}", e);
            std::process::exit(1);
        }
    }
}

fn gen_insts(insts: &[Instruction], mut dest: CodeFile) -> Result<()> {

    // Generate the definition, each instruction is a struct variant
    try!(dest.write_line("#[derive(Clone, Debug)]"));
    try!(dest.start_block("pub enum Instruction {"));

    for inst in insts {
        try!(dest.write(&inst.name));
        if inst.params.len() > 0 {
            try!(dest.start_block(" {"));

            for param in &inst.params {
                let name = normalize_name(&param.name);
                let ty = param.ty.rust_type_name();
                try!(dest.write_line(&format!("{}: {},", name, ty)));
            }

            try!(dest.end_block("},"));
        } else {
            try!(dest.write_line(","));
        }

    }

    try!(dest.write_line("Unknown(u16, Box<[u32]>)"));


    try!(dest.end_block("}\n\n"));

    // Generate some methods
    try!(dest.start_block("impl Instruction {"));

    fn extract_field<'a, I: IntoIterator<Item=&'a Instruction>>(
        dest: &mut CodeFile, name: &str, pb: bool, ret_ty: &str,
        insts: I, field: &str) -> Result<()> {

        let pb = if pb { "pub " } else { "" };

        try!(dest.start_block(
            &format!("{}fn {}(&self) -> Option<{}> {{", pb, name, ret_ty)));
        try!(dest.write_line("use self::Instruction::*;"));
        try!(dest.start_block("match *self {"));

        let insts = insts.into_iter().filter(|i| {
            for p in &i.params {
                if &p.name[..] == field {
                    return true;
                }
            }
            false
        });

        let field = normalize_name(field);

        let mut first = true;
        for inst in insts {
            if first {
                first = false;
            } else {
                try!(dest.write_line(" |"));
            }

            if inst.params.len() == 1 {
                try!(dest.write(&format!("{} {{ {} }}", inst.name, field)));
            } else {
                try!(dest.write(&format!("{} {{ {}, .. }}", inst.name, field)));
            }
        }
        try!(dest.write_line(&format!(" => Some({}),", field)));
        try!(dest.write_line("_ => None"));

        try!(dest.end_block("}"));
        dest.end_block("}")
    }

    let non_types = insts.iter().filter(|i| {
        let group : Option<&str> = i.group.as_ref().map(|g| &g[..]);
        if group == Some("Type") {
            return false;
        }

        return true;
    }).collect::<Vec<_>>();

    // Generate method for getting the id of the value this instruction defines, if any.
    try!(extract_field(&mut dest, "defines_value_inner", false, "ResultId", non_types.iter().cloned(), "result-id"));
    // Generate method for getting the id of the type of the result of this instruction, if any.
    try!(extract_field(&mut dest, "type_id_of", true, "TypeId", non_types.iter().cloned(), "result-type"));

    let types = insts.iter().filter(|i| {
        let group : Option<&str> = i.group.as_ref().map(|g| &g[..]);
        if group == Some("Type") {
            return true;
        }

        return false;
    });

    // Generate method for getting the id of the type of the instruction defines
    try!(extract_field(&mut dest, "defines_type", true, "TypeId", types, "result-type"));

    // Finally generate a method for getting all the ids used by the instruction
    let users = insts.iter().filter(|i| {
        for p in &i.params {
            if p.name.starts_with("result") {
                continue;
            }

            match p.ty {
                ParamTy::Single(ty, _) |
                ParamTy::Repeat(ty) => {
                    match ty {
                        Ty::Id | Ty::TypeId | Ty::ValueId => return true,
                        _ => ()
                    }
                }
                ParamTy::RepeatMany(ref tys) => {
                    for &ty in tys {
                        match ty {
                            Ty::Id | Ty::TypeId | Ty::ValueId => return true,
                            _ => ()
                        }
                    }
                }
            }
        }

        return false;
    });

    try!(dest.start_block("pub fn uses(&self) -> Vec<Id> {"));
    try!(dest.write_line("use self::Instruction::*;"));
    try!(dest.start_block("match *self {"));

    for u in users {
        let params = u.params.iter().filter(|p| {
            if p.name.starts_with("result") {
                return false;
            }

            match p.ty {
                ParamTy::Single(ty, _) |
                ParamTy::Repeat(ty) => {
                    match ty {
                        Ty::Id | Ty::TypeId | Ty::ValueId => return true,
                        _ => ()
                    }
                }
                ParamTy::RepeatMany(ref tys) => {
                    for &ty in tys {
                        match ty {
                            Ty::Id | Ty::TypeId | Ty::ValueId => return true,
                            _ => ()
                        }
                    }
                }
            }

            false
        }).collect::<Vec<_>>();

        try!(dest.start_block(&format!("{} {{", u.name)));
        let mut i = 0;
        for p in &params {
            let name = normalize_name(&p.name);
            match p.ty {
                ParamTy::Single(..) => {
                    try!(dest.write_line(&format!("{},", name)));
                }
                ParamTy::Repeat(..) |
                ParamTy::RepeatMany(..) => {
                    try!(dest.write_line(&format!("ref {},", name)));
                }
            }
            i += 1;
        }

        if i != u.params.len() {
            try!(dest.write_line(".."));
        }
        try!(dest.new_block("} => {"));
        try!(dest.write_line("let mut ids = Vec::new();"));

        for p in params {
            let name = normalize_name(&p.name);
            match p.ty {
                ParamTy::Single(..) => {
                    try!(dest.write_line(&format!("ids.push({}.into());", name)));
                }
                ParamTy::Repeat(..) => {
                    try!(dest.write_line(&format!("ids.extend({}.iter().map(|i| Id::from(*i)));", name)));
                }
                ParamTy::RepeatMany(ref tys) => {
                    try!(dest.start_block(&format!("for x in {}.iter() {{", name)));
                    for (i, &ty) in tys.iter().enumerate() {
                        match ty {
                            Ty::Id | Ty::TypeId | Ty::ValueId => {
                                try!(dest.write_line(&format!("ids.push(x.{}.into());", i)));
                            }
                            _ => ()
                        }
                    }
                    try!(dest.end_block("}"));
                }
            }
        }
        try!(dest.write_line("ids"));
        try!(dest.end_block("}"));
    }

    try!(dest.write_line("_ => Vec::new()"));

    try!(dest.end_block("}"));
    try!(dest.end_block("}"));

    dest.end_block("}")

}

fn gen_parser(insts: &[Instruction], mut dest: CodeFile) -> Result<()> {
    try!(dest.start_block(
        "pub fn parse_raw_instruction(raw_inst: RawInstruction) -> Result<Instruction> {"));
    try!(dest.start_block(
        "let op = if let Some(op) = desc::Op::from(raw_inst.opcode) {"));
    try!(dest.write_line("op"));
    try!(dest.new_block("} else {"));
    try!(dest.write_line("return Err(ParseError::UnknownOpcode(raw_inst.opcode));"));
    try!(dest.end_block("};"));

    try!(dest.write_line("let mut p = InstructionParser { params: &raw_inst.params };"));

    try!(dest.start_block("let inst = match op {"));

    for inst in insts {
        if inst.params.len() == 0 {
            try!(dest.write_line(&format!(
                "Op::{name} => Instruction::{name},", name=inst.name)));
            continue;
        }

        try!(dest.start_block(&format!("Op::{} => {{", inst.name)));

        for param in &inst.params {
            let name = normalize_name(&param.name);

            if let ParamTy::Single(ty, true) = param.ty {
                if ty.is_id() {
                    let ty_name = ty.rust_type_name(false);
                    try!(dest.write_line(&format!("let mut {} = {}(0);", name, ty_name)));
                    try!(dest.start_block("if p.has_words() {"));
                    try!(dest.write_line(&format!(
                        "{} = try!(p.parse::<{}>());\n", name, ty_name)));
                    try!(dest.end_block("}"));
                    continue;
                }
            }

            if let ParamTy::RepeatMany(ref tys) = param.ty {
                let tys : Vec<_> = tys.iter().map(|ty| ty.rust_type_name(false)).collect();
                let ty = tys.join(", ");
                try!(dest.write_line(&format!(
                    "let mut {} : Vec<({})> = Vec::new();", name, ty)));
                try!(dest.start_block("while p.has_words() {"));
                try!(dest.start_block(&format!("{}.push((", name)));
                for ty in &tys {
                    try!(dest.write_line(&format!("try!(p.parse::<{}>()),", ty)));
                }
                try!(dest.end_block("));"));
                try!(dest.end_block("}"));
                try!(dest.write_line(&format!(
                    "let {name} = {name}.into_boxed_slice();", name=name)));

                continue;
            }

            let ty = param.ty.rust_type_name();

            try!(dest.write_line(&format!("let {} = try!(p.parse::<{}>());", name, ty)));
        }

        try!(dest.start_block(&format!("Instruction::{} {{", inst.name)));
        for param in &inst.params {
            let name = normalize_name(&param.name);
            try!(dest.write_line(&format!("{name}: {name},", name=name)));
        }
        try!(dest.end_block("}"));
        try!(dest.end_block("}"));
    }

    try!(dest.write_line("_ => Instruction::Unknown(op as u16, p.params.to_owned().into_boxed_slice())\n"));

    try!(dest.end_block("};"));
    try!(dest.write_line("Ok(inst)"));

    dest.end_block("}")
}

fn normalize_name<'a>(s: &'a str) -> Cow<'a, str> {
    if s.contains('-') {
        s.replace("-", "_").into()
    } else {
        s.into()
    }
}

#[derive(Copy, Clone, Debug)]
pub enum Ty {
    Id,
    ResultType,
    ResultId,
    TypeId,
    ValueId,
    String,
    Number,
    Bool,
    Decoration,
    ExecutionMode,
    ImageOperands,
    SrcLang,
    ExecutionModel,
    AddressingModel,
    MemoryModel,
    StorageClass,
    Dim,
    SamplerAddressingMode,
    SamplerFilterMode,
    ImageFormat,
    ImageChannelOrder,
    ImageChannelDatatype,
    FPRoundingMode,
    AccessQualifier,
    BuiltIn,
    GroupOperation,
    Capability,
    FPFastMathMode,
    SelectionControl,
    LoopControl,
    FunctionControl,
    MemoryOrdering,
    MemoryAccess,
    KernelProfilingInfo
}

impl Ty {
    pub fn from_str(s: &str) -> Option<Ty> {
        use self::Ty::*;
        let ty = match s {
            "id" => Id,
            "result-type" => ResultType,
            "type-id" => TypeId,
            "result-id" => ResultId,
            "value-id" => ValueId,
            "string" => String,
            "num" => Number,
            "bool" => Bool,

            "decoration" => Decoration,
            "execution-mode" => ExecutionMode,
            "image-operands" => ImageOperands,

            "src-lang" => SrcLang,
            "execution-model" => ExecutionModel,
            "addressing-model" => AddressingModel,
            "memory-model" => MemoryModel,
            "storage-class" => StorageClass,
            "dim" => Dim,
            "sampler-addressing-mode" => SamplerAddressingMode,
            "sampler-filter-mode" => SamplerFilterMode,
            "image-format" => ImageFormat,
            "image-channel-order" => ImageChannelOrder,
            "image-channel-datatype" => ImageChannelDatatype,
            "fp-rounding-mode" => FPRoundingMode,
            "access-qualifier" => AccessQualifier,
            "builtin" => BuiltIn,
            "group-operation" => GroupOperation,
            "capability" => Capability,

            "fp-fast-math-mode" => FPFastMathMode,
            "selection-control" => SelectionControl,
            "loop-control" => LoopControl,
            "function-control" => FunctionControl,
            "memory-ordering" => MemoryOrdering,
            "memory-access" => MemoryAccess,
            "kernel-profiling-info" => KernelProfilingInfo,

            _ => return None
        };

        Some(ty)
    }

    pub fn is_id(&self) -> bool {
        match *self {
            Ty::Id |
            Ty::ResultId |
            Ty::ResultType | Ty::TypeId |
            Ty::ValueId => true,
            _ => false
        }
    }

    pub fn rust_type_name(&self, opt: bool) -> Cow<'static, str> {
        use self::Ty::*;
        if opt {
            let name = self.rust_type_name(false);
            if self.is_id() {
                return name;
            }
            match *self {
                MemoryAccess | ImageOperands => name,
                _ => {
                    let name = format!("Option<{}>", name);
                    name.into()
                }
            }
        } else {
            let name = match *self {
                Id => "Id",
                ResultId => "ResultId",
                ResultType | TypeId => "TypeId",
                ValueId => "ValueId",
                String => "String",
                Number => "u32",
                Bool => "bool",
                Decoration => "Decoration",
                ExecutionMode => "ExecutionMode",
                ImageOperands => "ImageOperands",

                SrcLang => "desc::SrcLang",
                ExecutionModel => "desc::ExecutionModel",
                AddressingModel => "desc::AddressingModel",
                MemoryModel => "desc::MemoryModel",
                StorageClass => "desc::StorageClass",
                Dim => "desc::Dim",
                SamplerAddressingMode => "desc::SamplerAddressingMode",
                SamplerFilterMode => "desc::SamplerFilterMode",
                ImageFormat => "desc::ImageFormat",
                ImageChannelOrder => "desc::ImageChannelOrder",
                ImageChannelDatatype => "desc::ImageChannelDatatype",
                FPRoundingMode => "desc::FPRoundingMode",
                AccessQualifier => "desc::AccessQualifier",
                BuiltIn => "desc::BuiltIn",
                GroupOperation => "desc::GroupOperation",
                Capability => "desc::Capability",
                FPFastMathMode => "desc::FPFastMathMode",
                SelectionControl => "desc::SelectionControl",
                LoopControl => "desc::LoopControl",
                FunctionControl => "desc::FunctionControl",
                MemoryOrdering => "desc::MemoryOrdering",
                MemoryAccess => "desc::MemoryAccess",
                KernelProfilingInfo => "desc::KernelProfilingInfo"
            };

            name.into()
        }
    }
}

impl ParamTy {
    pub fn rust_type_name(&self) -> Cow<'static, str> {
        match *self {
            ParamTy::Single(ty, opt) => ty.rust_type_name(opt),
            ParamTy::Repeat(ty) => {
                let name = format!("Box<[{}]>", ty.rust_type_name(false));
                name.into()
            }
            ParamTy::RepeatMany(ref tys) => {
                let tys : Vec<_> = tys.iter().map(|ty| {
                    ty.rust_type_name(false)
                }).collect();
                let name = format!("Box<[({})]>", tys.join(", "));
                name.into()
            }
        }
    }
}