Skip to main content

llvm_assembler/formats/llvm/writer/
mod.rs

1use gaia_types::writer::TextWriter;
2use oak_core::source::ToSource;
3use oak_llvm_ir::ast::LLirRoot;
4use oak_pretty_print::{to_doc::AsDocument, FormatConfig};
5use std::fmt::Write;
6
7#[derive(Debug)]
8pub struct LLvmWriter<W> {
9    writer: TextWriter<W>,
10}
11
12impl<W: Write> LLvmWriter<W> {
13    pub fn new(writer: TextWriter<W>) -> Self {
14        Self { writer }
15    }
16
17    pub fn write_ast(&mut self, ast: &LLirRoot) -> Result<(), std::fmt::Error> {
18        let source = ast.to_source_string();
19        self.writer.write(&source)?;
20        Ok(())
21    }
22
23    pub fn write_doc(&mut self, ast: &LLirRoot) -> Result<(), std::fmt::Error> {
24        let doc = ast.as_document();
25        let config = FormatConfig::default();
26        let source = doc.render(config);
27        self.writer.write(&source)?;
28        Ok(())
29    }
30
31    pub fn finish(self) -> W {
32        self.writer.finish()
33    }
34}