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
// Copyright (c) 2017 Fabian Schuiki
#![allow(dead_code)]

pub use self::InstKind::*;
use crate::{ty::*, unit::UnitContext, value::*};

#[derive(Debug)]
pub struct Inst {
    id: InstRef,
    /// An optional name for the instruction used when emitting assembly.
    name: Option<String>,
    /// The instruction data.
    kind: InstKind,
}

impl Inst {
    /// Create a new instruction.
    pub fn new(name: Option<String>, kind: InstKind) -> Inst {
        Inst {
            id: InstRef::new(ValueId::alloc()),
            name: name,
            kind: kind,
        }
    }

    /// Obtain a reference to this instruction.
    pub fn as_ref(&self) -> InstRef {
        self.id
    }

    /// Determine the mnemonic for this instruction. The mnemonic is a short
    /// sequence of characters that uniquely identifies the instruction in human
    /// readable assembly text.
    pub fn mnemonic(&self) -> Mnemonic {
        self.kind.mnemonic()
    }

    /// Obtain a reference to the data for this instruction. See `InstKind`.
    pub fn kind(&self) -> &InstKind {
        &self.kind
    }
}

impl Value for Inst {
    fn id(&self) -> ValueId {
        self.id.into()
    }

    fn ty(&self) -> Type {
        self.kind.ty()
    }

    fn name(&self) -> Option<&str> {
        self.name.as_ref().map(|x| x as &str)
    }

    fn is_global(&self) -> bool {
        false
    }
}

pub struct InstIter<'tf> {
    refs: std::slice::Iter<'tf, InstRef>,
    ctx: &'tf UnitContext,
}

impl<'tf> InstIter<'tf> {
    pub fn new(refs: std::slice::Iter<'tf, InstRef>, ctx: &'tf UnitContext) -> InstIter<'tf> {
        InstIter {
            refs: refs,
            ctx: ctx,
        }
    }
}

impl<'tf> std::iter::Iterator for InstIter<'tf> {
    type Item = &'tf Inst;

    fn next(&mut self) -> Option<&'tf Inst> {
        let n = self.refs.next();
        n.map(|r| self.ctx.inst(*r))
    }
}

/// A relative position of an instruction. Used to insert or move an instruction
/// to a position relative to the surrounding unit, block, or another
/// instruction.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum InstPosition {
    /// The very first position in the entity, or the first position in the
    /// first block of the function/process.
    Begin,
    /// The very last position in the entity, or the last position in the last
    /// block of the function/process.
    End,
    /// The position just before another instruction.
    Before(InstRef),
    /// The position just after another instruction.
    After(InstRef),
    /// The very first position in the block. Only valid in functions and
    /// processes.
    BlockBegin(BlockRef),
    /// The very last position in the block. Only valid in functions and
    /// processes.
    BlockEnd(BlockRef),
}

/// The different forms an instruction can take.
#[derive(Debug)]
pub enum InstKind {
    UnaryInst(UnaryOp, Type, ValueRef),
    BinaryInst(BinaryOp, Type, ValueRef, ValueRef),
    CompareInst(CompareOp, Type, ValueRef, ValueRef),
    CallInst(Type, ValueRef, Vec<ValueRef>),
    InstanceInst(Type, ValueRef, Vec<ValueRef>, Vec<ValueRef>),
    WaitInst(BlockRef, Option<ValueRef>, Vec<ValueRef>),
    ReturnInst(ReturnKind),
    BranchInst(BranchKind),
    SignalInst(Type, Option<ValueRef>),
    ProbeInst(Type, ValueRef),
    DriveInst(ValueRef, ValueRef, Option<ValueRef>),
    HaltInst,
}

impl InstKind {
    /// Get the result type of the instruction.
    pub fn ty(&self) -> Type {
        match *self {
            UnaryInst(_, ref ty, _) => ty.clone(),
            BinaryInst(_, ref ty, _, _) => ty.clone(),
            CompareInst(..) => int_ty(1),
            CallInst(ref ty, _, _) => ty.as_func().1.clone(),
            InstanceInst(..) | WaitInst(..) | ReturnInst(_) | BranchInst(_) => void_ty(),
            SignalInst(ref ty, _) => signal_ty(ty.clone()),
            ProbeInst(ref ty, _) => ty.clone(),
            DriveInst(..) => void_ty(),
            HaltInst => void_ty(),
        }
    }

    pub fn mnemonic(&self) -> Mnemonic {
        match *self {
            UnaryInst(op, _, _) => Mnemonic::Unary(op.mnemonic()),
            BinaryInst(op, _, _, _) => Mnemonic::Binary(op.mnemonic()),
            CompareInst(..) => Mnemonic::Cmp,
            CallInst(..) => Mnemonic::Call,
            InstanceInst(..) => Mnemonic::Inst,
            WaitInst(..) => Mnemonic::Wait,
            ReturnInst(..) => Mnemonic::Ret,
            BranchInst(..) => Mnemonic::Br,
            SignalInst(..) => Mnemonic::Sig,
            ProbeInst(..) => Mnemonic::Prb,
            DriveInst(..) => Mnemonic::Drv,
            HaltInst => Mnemonic::Halt,
        }
    }

    /// Check if this is an instantiation instruction.
    pub fn is_instance(&self) -> bool {
        match *self {
            InstanceInst(..) => true,
            _ => false,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum UnaryOp {
    Not,
}

impl UnaryOp {
    pub fn mnemonic(&self) -> UnaryMnemonic {
        match *self {
            UnaryOp::Not => UnaryMnemonic::Not,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum BinaryOp {
    Add,
    Sub,
    Mul,
    Div,
    Mod,
    Rem,
    Shl,
    Shr,
    And,
    Or,
    Xor,
}

impl BinaryOp {
    pub fn mnemonic(&self) -> BinaryMnemonic {
        match *self {
            BinaryOp::Add => BinaryMnemonic::Add,
            BinaryOp::Sub => BinaryMnemonic::Sub,
            BinaryOp::Mul => BinaryMnemonic::Mul,
            BinaryOp::Div => BinaryMnemonic::Div,
            BinaryOp::Mod => BinaryMnemonic::Mod,
            BinaryOp::Rem => BinaryMnemonic::Rem,
            BinaryOp::Shl => BinaryMnemonic::Shl,
            BinaryOp::Shr => BinaryMnemonic::Shr,
            BinaryOp::And => BinaryMnemonic::And,
            BinaryOp::Or => BinaryMnemonic::Or,
            BinaryOp::Xor => BinaryMnemonic::Xor,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum CompareOp {
    Eq,
    Neq,
    Slt,
    Sgt,
    Sle,
    Sge,
    Ult,
    Ugt,
    Ule,
    Uge,
}

impl CompareOp {
    pub fn to_str(self) -> &'static str {
        match self {
            CompareOp::Eq => "eq",
            CompareOp::Neq => "neq",
            CompareOp::Slt => "slt",
            CompareOp::Sgt => "sgt",
            CompareOp::Sle => "sle",
            CompareOp::Sge => "sge",
            CompareOp::Ult => "ult",
            CompareOp::Ugt => "ugt",
            CompareOp::Ule => "ule",
            CompareOp::Uge => "uge",
        }
    }

    pub fn from_str(s: &str) -> Option<CompareOp> {
        Some(match s {
            "eq" => CompareOp::Eq,
            "neq" => CompareOp::Neq,
            "slt" => CompareOp::Slt,
            "sgt" => CompareOp::Sgt,
            "sle" => CompareOp::Sle,
            "sge" => CompareOp::Sge,
            "ult" => CompareOp::Ult,
            "ugt" => CompareOp::Ugt,
            "ule" => CompareOp::Ule,
            "uge" => CompareOp::Uge,
            _ => return None,
        })
    }
}

/// The return instruction flavor.
#[derive(Debug, Clone)]
pub enum ReturnKind {
    /// Return from a void function.
    Void,
    /// Return from a non-void function.
    Value(Type, ValueRef),
}

/// The branch flavor.
#[derive(Debug, Clone)]
pub enum BranchKind {
    /// An unconditional branch to a block.
    Uncond(BlockRef),
    /// A conditional branch, transferring control to one block if the condition
    /// is 1, or another block if it is 0.
    Cond(ValueRef, BlockRef, BlockRef),
}

#[derive(Clone, Copy, PartialEq, Eq)]
pub enum Mnemonic {
    Unary(UnaryMnemonic),
    Binary(BinaryMnemonic),
    Call,
    Inst,
    Cmp,
    Wait,
    Ret,
    Br,
    Phi,
    Sig,
    Prb,
    Drv,
    Halt,
}

#[derive(Clone, Copy, PartialEq, Eq)]
pub enum UnaryMnemonic {
    Not,
}

#[derive(Clone, Copy, PartialEq, Eq)]
pub enum BinaryMnemonic {
    Add,
    Sub,
    Mul,
    Div,
    Mod,
    Rem,
    Shl,
    Shr,
    And,
    Or,
    Xor,
}

impl Mnemonic {
    /// Convert the mnemonic to its textual representation.
    pub fn as_str(self) -> &'static str {
        match self {
            Mnemonic::Unary(m) => m.as_str(),
            Mnemonic::Binary(m) => m.as_str(),
            Mnemonic::Call => "call",
            Mnemonic::Inst => "inst",
            Mnemonic::Cmp => "cmp",
            Mnemonic::Wait => "wait",
            Mnemonic::Ret => "ret",
            Mnemonic::Br => "br",
            Mnemonic::Phi => "phi",
            Mnemonic::Sig => "sig",
            Mnemonic::Prb => "prb",
            Mnemonic::Drv => "drv",
            Mnemonic::Halt => "halt",
        }
    }
}

impl UnaryMnemonic {
    /// Convert the unary mnemonic to its textual representation.
    pub fn as_str(self) -> &'static str {
        match self {
            UnaryMnemonic::Not => "not",
        }
    }
}

impl BinaryMnemonic {
    /// Convert the binary mnemonic to its textual representation.
    pub fn as_str(self) -> &'static str {
        match self {
            BinaryMnemonic::Add => "add",
            BinaryMnemonic::Sub => "sub",
            BinaryMnemonic::Mul => "mul",
            BinaryMnemonic::Div => "div",
            BinaryMnemonic::Mod => "mod",
            BinaryMnemonic::Rem => "rem",
            BinaryMnemonic::Shl => "shl",
            BinaryMnemonic::Shr => "shr",
            BinaryMnemonic::And => "and",
            BinaryMnemonic::Or => "or",
            BinaryMnemonic::Xor => "xor",
        }
    }
}