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
use super::*;
use std::sync::Arc;
use std::collections::BTreeMap;

impl Package {
    pub fn emit_mlir(&self) {
        for moddef in self.moddefs() {
            if let Component::Mod(_loc, _name, _children, _wires, _whens) = &*moddef {
                self.emit_mlir_moddef(moddef);
            }
        }
    }

    fn emit_mlir_moddef(&self, moddef: Arc<Component>) {
        let mut ports: Vec<(bool, String, Type)> = vec![];
        let mut output_ports: Vec<String> = vec![];
        let mut output_port_ssas: BTreeMap<String, String> = BTreeMap::new();
        let mut output_port_types: Vec<Type> = vec![];

        for (_path, port) in moddef.port_paths() {
            let name = port.name().to_string();
            let typ = self.type_of(port.clone()).unwrap();
            ports.push((port.is_incoming_port(), name.clone(), typ.clone()));
            if port.is_outgoing_port() {
                output_ports.push(name);
                output_port_types.push(typ);
            }
        }

        let ctx = self.context_for(moddef.clone());

        println!("hw.module @{}(", moddef.name());
        self.emit_mlir_moddef_portlist(&ports);
        println!(") {{");

        for (i, Wire(_loc, target, expr, wire_type)) in moddef.wires().iter().enumerate() {
            match wire_type {
                WireType::Direct => {
                    let ssa = expr.emit_mlir(format!("$comb{i}"), ctx.clone());
                    let target_string = target.to_string();
                    if output_ports.contains(&target_string) {
                        output_port_ssas.insert(target_string, ssa);
                    } else if let Some(Component::Node(_loc, name, typ)) = moddef.child(target).as_ref().map(|arc| &**arc) {
                        let type_name = type_to_mlir(typ.clone());
                        println!("    %{name} = comb.add {ssa} : {type_name}");
                    }
                },
                WireType::Latch => {
                    let next_ssa = expr.emit_mlir(format!("$comb{i}"), ctx.clone());
                    let target_string = target.to_string();
                    let reg = moddef.child(target).unwrap();
                    let typ = reg.type_of().unwrap();
                    let reset = reg.reset().unwrap();
                    let reset_ssa = reset.emit_mlir(format!("$reset{i}"), ctx.clone());
                    println!("    %{target_string} = seq.firreg {next_ssa} clock %_clock reset sync %_reset, {reset_ssa} : {}", type_to_mlir(typ));
                },
                _ => panic!(),
            }
        }

        let output_port_ssas: Vec<&str> = output_ports.iter().map(|output_port| {
            output_port_ssas[output_port].as_str()
        }).collect();
        let output_port_types: Vec<String> = output_port_types.iter().map(|typ| {
            type_to_mlir(typ.clone())
        }).collect();
        if output_port_ssas.len() > 0 {
            println!("    hw.output {} : {}", output_port_ssas.join(","), output_port_types.join(","));
        }
        println!("}}");
    }

    fn emit_mlir_moddef_portlist(&self, ports: &[(bool, String, Type)]) {
        println!("    in %_clock : !seq.clock,");
        print!("    in %_reset : i1");
        if ports.len() > 0 {
            println!(",");
        } else {
            println!();
        }

        for (i, (is_input, name, typ)) in ports.iter().enumerate() {
            let typ_name = type_to_mlir(typ.clone());
            if *is_input {
                print!("    in %{name} : {typ_name}");
            } else {
                print!("    out {name} : {typ_name}");
            }
            if i + 1 < ports.len() {
                println!(",");
            } else {
                println!();
            }
        }
    }
}

impl Expr {
    fn emit_mlir(&self, prefix: String, ctx: Context<Path, Type>) -> String {
        let typ: Type = self.type_of();
        let type_name = type_to_mlir(typ.clone());

        match self {
            Expr::Reference(_loc, _typ, name) => {
                format!("%{name}")
            },
            Expr::Word(_loc, _typ, _w, n) => {
                let name = format!("%{prefix}_word");
                println!("    {name} = hw.constant {n} : {type_name}");
                name
            },
            Expr::Enum(_loc, typ, _typedef, valname) => {
                let name = format!("%{prefix}_enum");
                let typedef = if let Type::Enum(typedef) = typ.get().unwrap() {
                    typedef
                } else {
                    panic!();
                };
                let v = typedef.value_of(&*valname).unwrap();
                println!("    {name} = hw.constant {v} : {type_name}");
                name
            },
            Expr::ToWord(_loc, _typ, e1) => {
                let name = format!("%{prefix}_toword");
                let e1_ssa = e1.emit_mlir(format!("{prefix}_e1"), ctx.clone());
                println!("    {name} = comb.add {e1_ssa} : {type_name}");
                name
            },
            Expr::UnOp(_loc, _typ, UnOp::Not, e1) => {
                let name = format!("%{prefix}_not");
                let e1_ssa = e1.emit_mlir(format!("{prefix}_not_e1"), ctx.clone());
                // %c-1_i8 = hw.constant -1 : i8
                // %0 = comb.xor bin %a, %c-1_i8 : i8
                println!("    %{prefix}_not_negone = hw.constant -1 : {type_name}");
                println!("    {name} = comb.xor {e1_ssa}, %{prefix}_not_negone : {type_name}");
                name
            },
            Expr::BinOp(_loc, _typ, BinOp::Add, e1, e2) => {
                let name = format!("%{prefix}_add");
                let e1_ssa = e1.emit_mlir(format!("{prefix}_add_e1"), ctx.clone());
                let e2_ssa = e2.emit_mlir(format!("{prefix}_add_e2"), ctx.clone());
                println!("    {name} = comb.add {e1_ssa}, {e2_ssa} : {type_name}");
                name
            },
            Expr::BinOp(_loc, _typ, BinOp::Sub, e1, e2) => {
                let name = format!("%{prefix}_sub");
                let e1_ssa = e1.emit_mlir(format!("{prefix}_sub_e1"), ctx.clone());
                let e2_ssa = e2.emit_mlir(format!("{prefix}_sub_e2"), ctx.clone());
                println!("    {name} = comb.sub {e1_ssa}, {e2_ssa} : {type_name}");
                name
            },
            Expr::BinOp(_loc, _typ, BinOp::And, e1, e2) => {
                let name = format!("%{prefix}_and");
                let e1_ssa = e1.emit_mlir(format!("{prefix}_and_e1"), ctx.clone());
                let e2_ssa = e2.emit_mlir(format!("{prefix}_and_e2"), ctx.clone());
                println!("    {name} = comb.and {e1_ssa}, {e2_ssa} : {type_name}");
                name
            },
            Expr::BinOp(_loc, _typ, BinOp::Or, e1, e2) => {
                let name = format!("%{prefix}_or");
                let e1_ssa = e1.emit_mlir(format!("{prefix}_or_e1"), ctx.clone());
                let e2_ssa = e2.emit_mlir(format!("{prefix}_or_e2"), ctx.clone());
                println!("    {name} = comb.or {e1_ssa}, {e2_ssa} : {type_name}");
                name
            },
            Expr::BinOp(_loc, _typ, BinOp::Xor, e1, e2) => {
                let name = format!("%{prefix}_xor");
                let e1_ssa = e1.emit_mlir(format!("{prefix}_or_e1"), ctx.clone());
                let e2_ssa = e2.emit_mlir(format!("{prefix}_or_e2"), ctx.clone());
                println!("    {name} = comb.xor {e1_ssa}, {e2_ssa} : {type_name}");
                name
            },
            Expr::BinOp(_loc, _typ, BinOp::Eq, e1, e2) => {
                let name = format!("%{prefix}_eq");
                let e1_type_name = type_to_mlir(e1.type_of());
                let e1_ssa = e1.emit_mlir(format!("{prefix}_eq_e1"), ctx.clone());
                let e2_ssa = e2.emit_mlir(format!("{prefix}_eq_e2"), ctx.clone());
                // %0 = comb.icmp bin eq %a, %b : i8
                println!("    {name} = comb.icmp bin eq {e1_ssa}, {e2_ssa} : {e1_type_name}");
                name
            },
            Expr::BinOp(_loc, _typ, BinOp::Lt, e1, e2) => {
                let name = format!("%{prefix}_eq");
                let e1_type_name = type_to_mlir(e1.type_of());
                let e1_ssa = e1.emit_mlir(format!("{prefix}_lt_e1"), ctx.clone());
                let e2_ssa = e2.emit_mlir(format!("{prefix}_lt_e2"), ctx.clone());
                // %0 = comb.icmp bin ult %a, %b : i8
                println!("    {name} = comb.icmp bin ult {e1_ssa}, {e2_ssa} : {e1_type_name}");
                name
            },
            Expr::If(_loc, _typ, cond, e1, e2) => {
                let name = format!("%{prefix}_if");
                let cond_ssa = cond.emit_mlir(format!("{prefix}_if_cond"), ctx.clone());
                let e1_ssa   =   e1.emit_mlir(format!("{prefix}_if_e1"),   ctx.clone());
                let e2_ssa   =   e2.emit_mlir(format!("{prefix}_if_e2"),   ctx.clone());
                // %0 = comb.mux bin %in, %a, %b : i8
                println!("    {name} = comb.mux bin {cond_ssa}, {e1_ssa}, {e2_ssa} : {type_name}");
                name
            },
            Expr::Mux(_loc, _typ, cond, e1, e2) => {
                let name = format!("%{prefix}_mux");
                let cond_ssa = cond.emit_mlir(format!("{prefix}_mux_cond"), ctx.clone());
                let e1_ssa   =   e1.emit_mlir(format!("{prefix}_mux_e1"),   ctx.clone());
                let e2_ssa   =   e2.emit_mlir(format!("{prefix}_mux_e2"),   ctx.clone());
                // %0 = comb.mux bin %in, %a, %b : i8
                println!("    {name} = comb.mux bin {cond_ssa}, {e1_ssa}, {e2_ssa} : {type_name}");
                name
            },
            Expr::Cat(_loc, _typ, es) => {
            let name = format!("%{prefix}_cat");
                let mut es_ssas = vec![];
                let mut es_typenames = vec![];
                for (i, e) in es.iter().enumerate() {
                    let ssa = e.emit_mlir(format!("{prefix}_cat_e{i}"),ctx.clone());
                    let width = e.type_of().bitwidth();
                    let type_name = format!("i{width}");
                    es_ssas.push(ssa);
                    es_typenames.push(type_name);
                }

                println!("    {name} = comb.concat {} : {}", es_ssas.join(", "), es_typenames.join(", "));
                name
            },
            Expr::Sext(_loc, _typ, e1) => {
                let name = format!("%{prefix}_sext");
                match (typ, e1.type_of()) {
                    (Type::Word(outer_width), Type::Word(inner_width)) => {
                        assert!(outer_width >= inner_width);
                        let extension_width = outer_width - inner_width;
                        let e1_ssa = e1.emit_mlir(format!("{prefix}_sext_e1"),   ctx.clone());
                        // %c0_i7 = hw.constant 0 : i7
                        // %0 = comb.concat %c0_i7, %a : i7, i1
                        println!("    %{prefix}_sext_zero = hw.constant 0 : i{extension_width}");
                        println!("    {name} = comb.concat %{prefix}_sext_zero, {e1_ssa} : i{extension_width}, i{inner_width}");
                        name
                    },
                    _ => panic!(),
                }
            },
            /*
            Expr::Let(loc, _typ, name, e, b) => {
            let name = format!("%{prefix}_let");
                let e_ssa = e.emit_mlir(format!("{prefix}_let_{name}"), ctx.clone());
                let b_ssa = b.emit_mlir(format!("{prefix}_let_{name}"), new_ctx);

                println!("comb.add %{b_ssa} : {type_name}");
                name
            },
            */
            Expr::Idx(_loc, _typ, e1, i) => {
                let name = format!("%{prefix}_idx");
                let e1_type_name = type_to_mlir(e1.type_of());
                let e1_ssa = e1.emit_mlir(format!("{prefix}_idx_e1"), ctx.clone());
                // %0 = comb.extract %b from 0 : (i8) -> i1
                println!("    {name} = comb.extract {e1_ssa} from {i} : ({e1_type_name}) -> i1");
                name
            },
            Expr::IdxRange(_loc, _typ, e1, j, i) => {
                let name = format!("%{prefix}_idxrange");
                let e1_type_name = type_to_mlir(e1.type_of());
                let width = j - i;
                let e1_ssa = e1.emit_mlir(format!("{prefix}_idxrange_e1"), ctx.clone());
                // %0 = comb.extract %b from 0 : (i8) -> i3
                println!("    {name} = comb.extract {e1_ssa} from {i} : ({e1_type_name}) -> i{width}");
                name
            }
            _ => panic!("Can't lower expression {self:?}"),
        }
    }
}

fn type_to_mlir(typ: Type) -> String {
    match typ {
        Type::Word(n) => format!("i{n}"),
        Type::Struct(typedef) => {
            let typedef = typedef;
            let n = typedef.bitwidth();
            format!("i{n}")
        },
        Type::Enum(typedef) => {
            let n = typedef.bitwidth();
            format!("i{n}")
        }
        _ => panic!("Can't lower type to MLIR directly"),
    }
}