About Social Code
aboutsummaryrefslogtreecommitdiff
path: root/src/compiler/rust/nir_instr_printer.rs
blob: a02b4119cfc68c51d3fec70259672245568c528e (plain)
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
// Copyright © 2024 Collabora, Ltd.
// SPDX-License-Identifier: MIT

use std::io;

use crate::bindings;
use crate::bindings::nir_instr;
use crate::memstream::MemStream;

/// A memstream that holds the printed NIR instructions.
pub struct NirInstrPrinter {
    stream: MemStream,
}

impl NirInstrPrinter {
    pub fn new() -> io::Result<Self> {
        Ok(Self {
            stream: MemStream::new()?,
        })
    }

    /// Prints the given NIR instruction.
    pub fn instr_to_string(&mut self, instr: &nir_instr) -> io::Result<String> {
        unsafe { bindings::nir_print_instr(instr, self.stream.c_file()) };
        self.stream.take_utf8_string_lossy()
    }
}