kecc/ir/write_ir.rs
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
use std::io::{Result, Write};
use crate::ir::*;
use crate::write_base::*;
impl WriteLine for TranslationUnit {
fn write_line(&self, indent: usize, write: &mut dyn Write) -> Result<()> {
// TODO: consider KECC IR parser in the future.
let mut structs = self.structs.iter().collect::<Vec<_>>();
structs.sort_unstable_by_key(|&(name, _)| name);
for (name, struct_type) in structs {
let definition = if let Some(struct_type) = struct_type {
let fields = struct_type
.get_struct_fields()
.expect("`struct_type` must be struct type")
.as_ref()
.expect("`fields` must be `Some`");
let fields = fields.iter().format_with(", ", |field, f| {
f(&format_args!(
"{}:{}",
if let Some(name) = field.name() {
name
} else {
"%anon"
},
field.deref()
))
});
format!("{{ {fields} }}")
} else {
"opaque".to_string()
};
writeln!(write, "struct {name} : {definition}")?;
}
for (name, decl) in &self.decls {
if decl.get_variable().is_some() {
(name, decl).write_line(indent, write)?;
}
}
for (name, decl) in &self.decls {
if decl.get_function().is_some() {
writeln!(write)?;
(name, decl).write_line(indent, write)?;
}
}
Ok(())
}
}
impl WriteLine for (&String, &Declaration) {
fn write_line(&self, indent: usize, write: &mut dyn Write) -> Result<()> {
let name = self.0;
let decl = self.1;
match decl {
Declaration::Variable { dtype, initializer } => {
writeln!(
write,
"var {} @{} = {}",
dtype,
name,
if let Some(init) = initializer {
init.write_string()
} else {
"default".to_string()
}
)?;
}
Declaration::Function {
signature,
definition,
} => {
let params = signature.params.iter().format(", ");
if let Some(definition) = definition.as_ref() {
// print function definition
writeln!(write, "fun {} @{} ({}) {{", signature.ret, name, params)?;
// print meta data for function
writeln!(
write,
"init:\n bid: {}\n allocations: \n{}",
definition.bid_init,
definition
.allocations
.iter()
.enumerate()
.format_with("\n", |(i, a), f| f(&format_args!(
" %l{}:{}{}",
i,
a.deref(),
if let Some(name) = a.name() {
format!(":{name}")
} else {
"".into()
}
)))
)?;
for (id, block) in &definition.blocks {
writeln!(write, "\nblock {id}:")?;
(id, block).write_line(indent + 1, write)?;
}
writeln!(write, "}}")?;
} else {
// print declaration line only
writeln!(write, "fun {} @{} ({})", signature.ret, name, params)?;
writeln!(write)?;
}
}
}
Ok(())
}
}
impl WriteLine for (&BlockId, &Block) {
fn write_line(&self, indent: usize, write: &mut dyn Write) -> Result<()> {
for (i, phi) in self.1.phinodes.iter().enumerate() {
write_indent(indent, write)?;
writeln!(
write,
"{}:{}{}",
RegisterId::arg(*self.0, i),
phi.deref(),
if let Some(name) = phi.name() {
format!(":{name}")
} else {
"".into()
}
)?;
}
for (i, instr) in self.1.instructions.iter().enumerate() {
write_indent(indent, write)?;
writeln!(
write,
"{}:{}{} = {}",
RegisterId::temp(*self.0, i),
instr.dtype(),
if let Some(name) = instr.name() {
format!(":{name}")
} else {
"".into()
},
instr
)?;
}
write_indent(indent, write)?;
writeln!(write, "{}", self.1.exit)?;
Ok(())
}
}