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
// Copyright (c) 2017-2019 Fabian Schuiki
use crate::{
    argument::*,
    block::*,
    entity::{Entity, EntityContext},
    function::{Function, FunctionContext},
    inst::*,
    konst::*,
    module::{Module, ModuleContext},
    process::{Process, ProcessContext},
    ty::*,
    unit::*,
    value::*,
    visit::Visitor,
};
use std::{collections::HashMap, io::Write, rc::Rc};

/// Emits a module as human-readable assembly code that can be parsed again
/// later.
pub struct Writer<'twr> {
    sink: &'twr mut Write,
    /// A table of uniquified names assigned to values.
    name_table: HashMap<ValueId, Rc<String>>,
    /// A stack of maps that keep a counter for every string encountered in
    /// a namespace. Currently there are only two namespaces: modules and units.
    name_stack: Vec<(usize, HashMap<String, usize>)>,
}

impl<'twr> Writer<'twr> {
    /// Create a new assembly writer that will emit code into the provided sink.
    pub fn new(sink: &mut Write) -> Writer {
        Writer {
            sink: sink,
            name_table: HashMap::new(),
            name_stack: vec![(0, HashMap::new())],
        }
    }

    /// Determine a unique name for a value. Either returns the value's name, or
    /// extends it with a monotonically increasing number. If the value has no
    /// name assigned, returns a unique temporary name.
    fn uniquify(&mut self, value: &Value) -> Rc<String> {
        let id = value.id();
        if let Some(name) = self.name_table.get(&id).cloned() {
            name
        } else {
            let name = self.uniquify_name(value.name(), value.is_global());
            self.name_table.insert(id, name.clone());
            name
        }
    }

    /// Ensures that a name is unique within the current name stack, extending
    /// the name with a monotonically increasing number or using a temporary
    /// name altogether, if need be.
    fn uniquify_name(&mut self, name: Option<&str>, global: bool) -> Rc<String> {
        let prefix = if global { "@" } else { "%" };

        if let Some(name) = name {
            // First handle the easy case where the namespace at the top of the
            // stack already has a uniquification counter for the name. If that
            // is the case, increment it and append the old count to the
            // requested name.
            if let Some(index) = self.name_stack.last_mut().unwrap().1.get_mut(name) {
                let n = Rc::new(format!("{}{}{}", prefix, name, index));
                *index += 1;
                return n;
            }

            // Traverse the name stack top to bottom, looking for an existing
            // uniquification counter for the name. If we find one, store the
            // count and break out of the loop. We'll use this count to uniquify
            // the name.
            let mut index: Option<usize> = None;
            for &(_, ref names) in self.name_stack.iter().rev().skip(1) {
                if let Some(&i) = names.get(name) {
                    index = Some(i);
                    break;
                }
            }

            // Insert the name into the topmost namespace, either using the
            // existing uniquification count we'found, or 0.
            self.name_stack
                .last_mut()
                .unwrap()
                .1
                .insert(name.to_owned(), index.unwrap_or(0));

            // If we have found a uniquification count, append that to the name.
            // Otherwise just use the name as it is.
            Rc::new(
                index
                    .map(|i| format!("{}{}{}", prefix, name, i))
                    .unwrap_or_else(|| format!("{}{}", prefix, name)),
            )
        } else {
            // We arrive here if `None` was passed as a name. This case is
            // trivial. We simply increment the index for unnamed values, and
            // convert the old index to a string to be used as the value's name.
            let ref mut index = self.name_stack.last_mut().unwrap().0;
            let name = Rc::new(format!("%{}", index));
            *index += 1;
            name
        }
    }

    /// Add an empty namespace to the top of the name stack. Future temporary
    /// and uniquified names will be stored there.
    fn push(&mut self) {
        let index = self.name_stack.last().unwrap().0;
        self.name_stack.push((index, HashMap::new()))
    }

    /// Remove the topmost namespace from the name stack.
    fn pop(&mut self) {
        self.name_stack.pop();
        assert!(!self.name_stack.is_empty())
    }

    /// Write an inline value. This function is used to emit instruction
    /// arguments and generally values on the right hand side of assignments.
    fn write_value(&mut self, ctx: &Context, value: &ValueRef) -> std::io::Result<()> {
        match *value {
            ValueRef::Const(ref k) => self.write_const(k),
            _ => {
                let value = ctx.value(value);
                let name = self.uniquify(value);
                write!(self.sink, "{}", name)
            }
        }
    }

    /// Write a type.
    fn write_ty(&mut self, ty: &Type) -> std::io::Result<()> {
        write!(self.sink, "{}", ty)
    }

    /// Write a constant value.
    fn write_const(&mut self, konst: &ConstKind) -> std::io::Result<()> {
        match *konst {
            ConstKind::Int(ref k) => write!(self.sink, "{}", k.value()),
            ConstKind::Time(ref k) => write!(self.sink, "{}", k),
        }
    }
}

impl<'twr> Visitor for Writer<'twr> {
    fn visit_module(&mut self, module: &Module) {
        let ctx = ModuleContext::new(module);
        for (value, sep) in module
            .values()
            .zip(std::iter::once("").chain(std::iter::repeat("\n")))
        {
            write!(self.sink, "{}", sep).unwrap();
            self.visit_module_value(&ctx, value);
        }
    }

    fn visit_function(&mut self, ctx: &ModuleContext, func: &Function) {
        let ctx = FunctionContext::new(ctx, func);
        self.push();
        write!(self.sink, "func @{} (", func.name()).unwrap();
        self.visit_arguments(func.args());
        write!(self.sink, ") {} {{\n", func.return_ty()).unwrap();
        for block in func.body().blocks() {
            self.visit_block(&ctx, block);
        }
        write!(self.sink, "}}\n").unwrap();
        self.pop();
    }

    fn visit_process(&mut self, ctx: &ModuleContext, prok: &Process) {
        let ctx = ProcessContext::new(ctx, prok);
        self.push();
        write!(self.sink, "proc @{} (", prok.name()).unwrap();
        self.visit_arguments(prok.inputs());
        write!(self.sink, ") (").unwrap();
        self.visit_arguments(prok.outputs());
        write!(self.sink, ") {{\n").unwrap();
        for block in prok.body().blocks() {
            self.visit_block(&ctx, block);
        }
        write!(self.sink, "}}\n").unwrap();
        self.pop();
    }

    fn visit_entity(&mut self, ctx: &ModuleContext, entity: &Entity) {
        let ctx = EntityContext::new(ctx, entity);
        self.push();
        write!(self.sink, "entity @{} (", entity.name()).unwrap();
        self.visit_arguments(entity.inputs());
        write!(self.sink, ") (").unwrap();
        self.visit_arguments(entity.outputs());
        write!(self.sink, ") {{\n").unwrap();
        let uctx = ctx.as_unit_context();
        for inst in entity.insts() {
            self.visit_inst(uctx, inst);
        }
        write!(self.sink, "}}\n").unwrap();
        self.pop();
    }

    fn visit_arguments(&mut self, args: &[Argument]) {
        for (arg, sep) in args
            .iter()
            .zip(std::iter::once("").chain(std::iter::repeat(", ")))
        {
            write!(self.sink, "{}", sep).unwrap();
            self.visit_argument(arg);
        }
    }

    fn visit_argument(&mut self, arg: &Argument) {
        write!(self.sink, "{}", arg.ty()).unwrap();
        let name = self.uniquify(arg);
        write!(self.sink, " {}", name).unwrap();
    }

    fn visit_block(&mut self, ctx: &SequentialContext, block: &Block) {
        let name = self.uniquify(block);
        write!(self.sink, "{}:\n", name).unwrap();
        self.walk_block(ctx, block);
    }

    fn visit_inst(&mut self, ctx: &UnitContext, inst: &Inst) {
        let name = self.uniquify(inst);
        write!(self.sink, "    ").unwrap();
        if !inst.ty().is_void() || (inst.name().is_some() && inst.kind().is_instance()) {
            write!(self.sink, "{} = ", name).unwrap();
        }
        write!(self.sink, "{}", inst.mnemonic().as_str()).unwrap();
        match *inst.kind() {
            // <op> <ty> <arg>
            UnaryInst(_op, ref ty, ref arg) => {
                write!(self.sink, " ").unwrap();
                self.write_ty(ty).unwrap();
                write!(self.sink, " ").unwrap();
                self.write_value(ctx.as_context(), arg).unwrap();
            }

            // <op> <ty> <lhs> <rhs>
            BinaryInst(_op, ref ty, ref lhs, ref rhs) => {
                write!(self.sink, " ").unwrap();
                self.write_ty(ty).unwrap();
                write!(self.sink, " ").unwrap();
                self.write_value(ctx.as_context(), lhs).unwrap();
                write!(self.sink, " ").unwrap();
                self.write_value(ctx.as_context(), rhs).unwrap();
            }

            // cmp <op> <ty> <lhs> <rhs>
            CompareInst(op, ref ty, ref lhs, ref rhs) => {
                write!(self.sink, " {} ", op.to_str()).unwrap();
                self.write_ty(ty).unwrap();
                write!(self.sink, " ").unwrap();
                self.write_value(ctx.as_context(), lhs).unwrap();
                write!(self.sink, " ").unwrap();
                self.write_value(ctx.as_context(), rhs).unwrap();
            }

            // call <target> (<args...>)
            CallInst(_, ref target, ref args) => {
                write!(self.sink, " ").unwrap();
                self.write_value(ctx.as_context(), target).unwrap();
                write!(self.sink, " (").unwrap();
                for (arg, sep) in args
                    .iter()
                    .zip(std::iter::once("").chain(std::iter::repeat(", ")))
                {
                    write!(self.sink, "{}", sep).unwrap();
                    self.write_value(ctx.as_context(), arg).unwrap();
                }
                write!(self.sink, ")").unwrap();
            }

            // inst <target> (<inputs...>) (<outputs...>)
            InstanceInst(_, ref target, ref ins, ref outs) => {
                write!(self.sink, " ").unwrap();
                self.write_value(ctx.as_context(), target).unwrap();
                write!(self.sink, " (").unwrap();
                for (arg, sep) in ins
                    .iter()
                    .zip(std::iter::once("").chain(std::iter::repeat(", ")))
                {
                    write!(self.sink, "{}", sep).unwrap();
                    self.write_value(ctx.as_context(), arg).unwrap();
                }
                write!(self.sink, ") (").unwrap();
                for (arg, sep) in outs
                    .iter()
                    .zip(std::iter::once("").chain(std::iter::repeat(", ")))
                {
                    write!(self.sink, "{}", sep).unwrap();
                    self.write_value(ctx.as_context(), arg).unwrap();
                }
                write!(self.sink, ")").unwrap();
            }

            // wait <target> [for <time>] (<signals...>)
            WaitInst(target, ref time, ref signals) => {
                write!(self.sink, " ").unwrap();
                self.write_value(ctx.as_context(), &target.into()).unwrap();
                if let Some(ref time) = *time {
                    write!(self.sink, " for ").unwrap();
                    self.write_value(ctx.as_context(), time).unwrap();
                }
                for signal in signals {
                    write!(self.sink, ", ").unwrap();
                    self.write_value(ctx.as_context(), signal).unwrap();
                }
            }

            // ret
            ReturnInst(ReturnKind::Void) => (),

            // ret <type> <value>
            ReturnInst(ReturnKind::Value(ref ty, ref value)) => {
                write!(self.sink, " ").unwrap();
                self.write_ty(ty).unwrap();
                write!(self.sink, " ").unwrap();
                self.write_value(ctx.as_context(), value).unwrap();
            }

            // br label <target>
            BranchInst(BranchKind::Uncond(target)) => {
                write!(self.sink, " label ").unwrap();
                self.write_value(ctx.as_context(), &target.into()).unwrap();
            }

            // br <cond> label <ifTrue> <ifFalse>
            BranchInst(BranchKind::Cond(ref cond, if_true, if_false)) => {
                write!(self.sink, " ").unwrap();
                self.write_value(ctx.as_context(), cond).unwrap();
                write!(self.sink, " label ").unwrap();
                self.write_value(ctx.as_context(), &if_true.into()).unwrap();
                write!(self.sink, " ").unwrap();
                self.write_value(ctx.as_context(), &if_false.into())
                    .unwrap();
            }

            // sig <type> [<init>]
            SignalInst(ref ty, ref init) => {
                write!(self.sink, " ").unwrap();
                self.write_ty(ty).unwrap();
                if let Some(ref init) = *init {
                    write!(self.sink, " ").unwrap();
                    self.write_value(ctx.as_context(), init).unwrap();
                }
            }

            // prb <signal>
            ProbeInst(_, ref signal) => {
                write!(self.sink, " ").unwrap();
                self.write_value(ctx.as_context(), signal).unwrap();
            }

            // drv <signal> <value> [<delay>]
            DriveInst(ref signal, ref value, ref delay) => {
                write!(self.sink, " ").unwrap();
                self.write_value(ctx.as_context(), signal).unwrap();
                write!(self.sink, " ").unwrap();
                self.write_value(ctx.as_context(), value).unwrap();
                if let Some(ref delay) = *delay {
                    write!(self.sink, " ").unwrap();
                    self.write_value(ctx.as_context(), delay).unwrap();
                }
            }

            // halt
            HaltInst => (),
        }
        write!(self.sink, "\n").unwrap();
    }
}