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
// Copyright (c) 2016-2021 Fabian Schuiki

//! Assignment lowering to MIR.

use crate::crate_prelude::*;
use crate::{
    mir::{
        assign::*,
        lower,
        lvalue::{Lvalue, LvalueKind},
        rvalue::{BinaryBitwiseOp, IntBinaryArithOp, Rvalue, RvalueKind, ShiftOp},
    },
    ParamEnv,
};

/// Lower a procedural assign statement.
#[moore_derive::query]
pub(crate) fn mir_assignment_from_procedural<'a>(
    cx: &impl Context<'a>,
    origin: NodeId,
    lhs: NodeId,
    rhs: NodeId,
    env: ParamEnv,
    span: Span, // TODO(fschuiki): Get this from `origin` to not pollute query key
    kind: hir::AssignKind,
) -> &'a mir::Assignment<'a> {
    let lhs_mir_lv = cx.mir_lvalue(lhs, env);
    let rhs_mir = cx.mir_rvalue(rhs, env);

    let value = match kind {
        // `a = b`
        hir::AssignKind::Block(ast::AssignOp::Identity)
        | hir::AssignKind::Nonblock
        | hir::AssignKind::NonblockDelay(_) => Assignment {
            id: origin,
            env,
            span,
            ty: lhs_mir_lv.ty,
            lhs: lhs_mir_lv,
            rhs: rhs_mir,
        },
        // `a (+= -= *= /= %= &= |= ^= <<= >>= <<<= >>>=) b`
        hir::AssignKind::Block(op) => {
            let builder = lower::rvalue::Builder {
                cx,
                span,
                expr: rhs_mir.id,
                env,
            };
            let lhs_mir_rv = cx.mir_rvalue(lhs, env);
            let value = match op {
                ast::AssignOp::Identity => unreachable!(),
                ast::AssignOp::Add => AssignOp::Arith(IntBinaryArithOp::Add),
                ast::AssignOp::Sub => AssignOp::Arith(IntBinaryArithOp::Sub),
                ast::AssignOp::Mul => AssignOp::Arith(IntBinaryArithOp::Mul),
                ast::AssignOp::Div => AssignOp::Arith(IntBinaryArithOp::Div),
                ast::AssignOp::Mod => AssignOp::Arith(IntBinaryArithOp::Mod),
                ast::AssignOp::BitAnd => AssignOp::Bitwise(BinaryBitwiseOp::And),
                ast::AssignOp::BitOr => AssignOp::Bitwise(BinaryBitwiseOp::Or),
                ast::AssignOp::BitXor => AssignOp::Bitwise(BinaryBitwiseOp::Xor),
                ast::AssignOp::LogicShL => AssignOp::Shift(ShiftOp::Left, false),
                ast::AssignOp::LogicShR => AssignOp::Shift(ShiftOp::Right, false),
                ast::AssignOp::ArithShL => AssignOp::Shift(ShiftOp::Left, true),
                ast::AssignOp::ArithShR => AssignOp::Shift(ShiftOp::Right, true),
            };
            let value = match value {
                AssignOp::Arith(op) => lower::rvalue::make_int_binary_arith(
                    &builder,
                    lhs_mir_lv.ty,
                    op,
                    lhs_mir_rv,
                    rhs_mir,
                ),
                AssignOp::Bitwise(op) => lower::rvalue::make_binary_bitwise(
                    &builder,
                    lhs_mir_lv.ty,
                    op,
                    false,
                    lhs_mir_rv,
                    rhs_mir,
                ),
                AssignOp::Shift(op, arith) => lower::rvalue::make_shift(
                    &builder,
                    lhs_mir_lv.ty,
                    op,
                    arith,
                    lhs_mir_rv,
                    rhs_mir,
                ),
            };
            Assignment {
                id: origin,
                env,
                span,
                ty: lhs_mir_lv.ty,
                lhs: lhs_mir_lv,
                rhs: value,
            }
        }
    };

    cx.arena().alloc_mir_assignment(value)
}

enum AssignOp {
    Arith(IntBinaryArithOp),
    Bitwise(BinaryBitwiseOp),
    Shift(ShiftOp, bool),
}

/// Lower a concurrent assign statement.
#[moore_derive::query]
pub(crate) fn mir_assignment_from_concurrent<'a>(
    cx: &impl Context<'a>,
    Ref(assign): Ref<'a, hir::Assign>,
    env: ParamEnv,
) -> &'a mir::Assignment<'a> {
    let lhs_mir = cx.mir_lvalue(assign.lhs, env);
    let rhs_mir = cx.mir_rvalue(assign.rhs, env);
    cx.arena().alloc_mir_assignment(Assignment {
        id: assign.id,
        env,
        span: assign.span,
        ty: lhs_mir.ty,
        lhs: lhs_mir,
        rhs: rhs_mir,
    })
}

/// Simplify an MIR assignment to potentially multiple simple MIR assignments.
///
/// This eliminates assignments to compound `Lvalue` objects, for example
/// concatenations, and replaces them with multiple assignments to each of the
/// individual concatenation fields.
#[moore_derive::query]
pub(crate) fn mir_simplify_assignment<'a>(
    cx: &impl Context<'a>,
    Ref(assign): Ref<'a, mir::Assignment<'a>>,
) -> Vec<&'a mir::Assignment<'a>> {
    let mut v = vec![];
    simplify(cx, assign, assign.lhs, assign.rhs, &mut v);
    v
}

/// Inner function called recursively to simplify assignments.
fn simplify<'a>(
    cx: &impl Context<'a>,
    root: &'a Assignment<'a>,
    lhs: &'a Lvalue<'a>,
    rhs: &'a Rvalue<'a>,
    into: &mut Vec<&'a Assignment<'a>>,
) {
    trace!("Simplifying {:?}", root);
    match lhs.kind {
        LvalueKind::Transmute(value) => simplify(cx, root, value, rhs, into),
        LvalueKind::Concat(ref values) if values.len() == 1 => {
            let mut a = root.clone();
            a.lhs = values[0];
            let a = cx.arena().alloc_mir_assignment(a);
            simplify(cx, a, a.lhs, a.rhs, into);
        }
        LvalueKind::Concat(ref values) => {
            let mut base = 0;
            for value in values.iter().rev() {
                // The value must be of a simple bit vector type, as enforced
                // by the casting logic.
                let sbvt = value.ty.simple_bit_vector(cx, value.span);
                trace!(
                    "- Splitting subassignment to `{}` ({}) at {}",
                    value.span.extract(),
                    sbvt,
                    base
                );

                // Slice the relevant part of the assignment out from the RHS.
                let builder = lower::rvalue::Builder {
                    cx,
                    span: rhs.span,
                    expr: rhs.id,
                    env: rhs.env,
                };
                let base_rv = builder.constant_u32(base as u32);
                let index = builder.build(
                    sbvt.to_unpacked(cx),
                    RvalueKind::Index {
                        value: rhs,
                        base: base_rv,
                        length: sbvt.size,
                    },
                );

                // Formulate a new assignment.
                let mut a = root.clone();
                a.lhs = value;
                a.rhs = index;
                let a = cx.arena().alloc_mir_assignment(a);
                simplify(cx, a, a.lhs, a.rhs, into);

                base += sbvt.size;
            }
        }
        LvalueKind::Index {
            value,
            base,
            length,
        } => match value.kind {
            // If we index into a concatenation, we simply add the (shifted)
            // index to each concatenated value, and emit an individual
            // assignment for each.
            LvalueKind::Concat(ref values) => {
                trace!("Refactoring index into concatenation:\n{:?}", lhs);
                let mut shift = 0;
                for value in values.iter().rev() {
                    // The value must be of a simple bit vector type, as enforced
                    // by the casting logic.
                    let sbvt = value.ty.simple_bit_vector(cx, value.span);

                    // Compute the shifted assignment index.
                    let rbuilder = lower::rvalue::Builder {
                        cx,
                        span: value.span,
                        expr: value.id,
                        env: value.env,
                    };
                    let shift_rv = rbuilder.constant_u32(shift as u32);
                    let base_shifted = rbuilder.build(
                        base.ty,
                        RvalueKind::IntBinaryArith {
                            op: IntBinaryArithOp::Sub,
                            domain: base.ty.domain(),
                            sign: base.ty.sign(),
                            lhs: base,
                            rhs: shift_rv,
                        },
                    );

                    // Add a slice around this concatenated value.
                    let lbuilder = lower::lvalue::Builder {
                        cx,
                        span: value.span,
                        expr: value.id,
                        env: value.env,
                    };
                    let index = lbuilder.build(
                        lhs.ty,
                        LvalueKind::Index {
                            value,
                            base: base_shifted,
                            length,
                        },
                    );

                    // Formulate a new assignment.
                    let mut a = root.clone();
                    a.lhs = index;
                    a.rhs = rhs;
                    let a = cx.arena().alloc_mir_assignment(a);
                    simplify(cx, a, a.lhs, a.rhs, into);

                    shift += sbvt.size;
                }
            }
            _ => {
                into.push(root);
            }
        },
        _ => {
            into.push(root);
        }
    }
}