machine-check-machine 0.7.1

Utility crate for the formal verification tool machine-check
Documentation
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
use machine_check_common::{ir_common::IrTypeArray, Signedness};
use proc_macro2::Span;
use std::fmt::Debug;
use syn::{
    punctuated::Punctuated, token::Paren, Expr, ExprCall, ExprLit, ExprPath, Lit, LitBool, LitInt,
};

use crate::{
    util::{create_expr_ident, create_expr_path, path_matches_global_names},
    wir::WSpan,
};

use super::{IntoSyn, WIdent, WMckBinary, WMckUnary, WPath, WPathSegment, WStdBinary, WStdUnary};

#[derive(Clone, Debug, Hash)]
pub enum WExprHighCall {
    Call(WCall),
    StdUnary(WStdUnary),
    StdBinary(WStdBinary),
    MckExt(WHighMckExt),
    MckNew(WHighMckNew),
    BooleanNew(bool),
    StdInto(WHighStdInto),
    StdClone(WIdent),
    ArrayRead(WArrayRead),
    ArrayWrite(WArrayWrite),
    Phi(WPhi),
    PhiTaken(WPhiTaken),
    PhiNotTaken,
}

#[derive(Clone, Hash)]
pub enum WExprCall {
    Call(WCall),
    MckUnary(WMckUnary),
    MckBinary(WMckBinary),
    MckExt(WMckExt),
    MckNew(WMckNew),
    BooleanNew(bool),
    StdClone(WIdent),
    ArrayRead(WArrayRead),
    ArrayWrite(WArrayWrite),
    Phi(WPhi),
    PhiTaken(WPhiTaken),
    PhiNotTaken,
}

#[derive(Clone, Debug, Hash)]
pub struct WPhi {
    pub condition: WIdent,
    pub then_ident: WIdent,
    pub else_ident: WIdent,
}

#[derive(Clone, Debug, Hash)]
pub struct WPhiTaken {
    pub ident: WIdent,
    pub condition: WIdent,
}

#[derive(Clone, Debug, Hash)]
pub enum WHighMckNew {
    Bitvector(Signedness, Option<u32>, i128),
    BitvectorArray(IrTypeArray, WIdent),
}

#[derive(Clone, Debug, Hash)]
pub enum WMckNew {
    Bitvector(mck::concr::ConcreteBitvector<mck::misc::RBound>),
    BitvectorArray(IrTypeArray, WIdent),
}

#[derive(Clone, Debug, Hash)]
pub struct WBitvectorNew {}

#[derive(Clone, Debug, Hash)]
pub struct WArrayNew {
    pub ty: IrTypeArray,
    pub fill_element: WIdent,
}

#[derive(Clone, Debug, Hash)]
pub struct WHighMckExt {
    pub width: Option<u32>,
    pub from: WIdent,
}

#[derive(Clone, Debug, Hash)]
pub struct WMckExt {
    pub signed: bool,
    pub width: u32,
    pub from: WIdent,
}

#[derive(Clone, Debug, Hash)]
pub struct WHighStdInto {
    pub signedness: Signedness,
    pub width: Option<u32>,
    pub from: WIdent,
}

#[derive(Clone, Debug, Hash)]
pub struct WArrayRead {
    pub base: WIdent,
    pub index: WIdent,
}
#[derive(Clone, Debug, Hash)]
pub struct WArrayWrite {
    pub base: WIdent,
    pub index: WIdent,
    pub element: WIdent,
}

pub const MCK_HIGH_EXT: &str = "::machine_check::Ext::ext";
pub const MCK_HIGH_BITVECTOR_NEW: &str = "::machine_check::Bitvector::new";
pub const MCK_HIGH_UNSIGNED_NEW: &str = "::machine_check::Unsigned::new";
pub const MCK_HIGH_SIGNED_NEW: &str = "::machine_check::Signed::new";
pub const MCK_HIGH_BITVECTOR_ARRAY_NEW: &str = "::machine_check::BitvectorArray::new_filled";

pub const BOOLEAN_NEW: &str = "::mck::forward::Boolean::new";

pub const MCK_UEXT: &str = "::mck::forward::Ext::uext";
pub const MCK_SEXT: &str = "::mck::forward::Ext::sext";
pub const MCK_BITVECTOR_NEW: &str = "::mck::forward::Bitvector::new";
pub const MCK_BITVECTOR_ARRAY_NEW: &str = "::mck::forward::Array::new_filled";

pub const STD_CLONE: &str = "::std::clone::Clone::clone";
pub const STD_INTO: &str = "::std::convert::Into::into";

pub const ARRAY_READ: &str = "::mck::forward::ReadWrite::read";
pub const ARRAY_WRITE: &str = "::mck::forward::ReadWrite::write";

pub const PHI: &str = "::mck::forward::PhiArg::phi";
pub const PHI_TAKEN: &str = "::mck::forward::PhiArg::Taken";
pub const PHI_NOT_TAKEN: &str = "::mck::forward::PhiArg::NotTaken";

#[derive(Clone, Debug, Hash)]
pub struct WCall {
    pub fn_path: WPath,
    pub args: Vec<WCallArg>,
}

#[derive(Clone, Hash)]
pub enum WCallArg {
    Ident(WIdent),
    Literal(Lit),
}

impl IntoSyn<Expr> for WExprCall {
    fn into_syn(self) -> Expr {
        let (fn_path, args) = self.call_fn_and_args();
        WCall { fn_path, args }.into_syn()
    }
}

impl WExprCall {
    pub fn call_fn_and_args(self) -> (WPath, Vec<WCallArg>) {
        let span = Span::call_site();
        let (fn_operand, args) = match self {
            WExprCall::Call(call) => return (call.fn_path, call.args),
            WExprCall::MckUnary(call) => {
                let operation = call.op.to_string();
                (operation, vec![WCallArg::Ident(call.operand)])
            }
            WExprCall::MckBinary(call) => {
                let operation = call.op.to_string();
                (
                    operation,
                    vec![WCallArg::Ident(call.a), WCallArg::Ident(call.b)],
                )
            }
            WExprCall::MckExt(call) => (
                String::from(match call.signed {
                    false => MCK_UEXT,
                    true => MCK_SEXT,
                }),
                vec![WCallArg::Ident(call.from)],
            ),
            WExprCall::MckNew(call) => match call {
                WMckNew::BitvectorArray(_type_array, ident) => (
                    String::from(MCK_BITVECTOR_ARRAY_NEW),
                    vec![WCallArg::Ident(ident)],
                ),
                WMckNew::Bitvector(bitvector) => (
                    String::from(MCK_BITVECTOR_NEW),
                    vec![WCallArg::Literal(Lit::Int(LitInt::new(
                        bitvector.to_u64().to_string().as_str(),
                        span,
                    )))],
                ),
            },
            WExprCall::BooleanNew(value) => (
                String::from(BOOLEAN_NEW),
                vec![WCallArg::Literal(Lit::Bool(LitBool { value, span }))],
            ),
            WExprCall::StdClone(from) => (String::from(STD_CLONE), vec![WCallArg::Ident(from)]),
            WExprCall::ArrayRead(read) => (
                String::from(ARRAY_READ),
                vec![WCallArg::Ident(read.base), WCallArg::Ident(read.index)],
            ),
            WExprCall::ArrayWrite(write) => (
                String::from(ARRAY_WRITE),
                vec![
                    WCallArg::Ident(write.base),
                    WCallArg::Ident(write.index),
                    WCallArg::Ident(write.element),
                ],
            ),
            WExprCall::Phi(phi) => (
                String::from(PHI),
                vec![
                    WCallArg::Ident(phi.then_ident),
                    WCallArg::Ident(phi.else_ident),
                ],
            ),
            WExprCall::PhiTaken(taken) => (
                String::from(PHI_TAKEN),
                vec![
                    WCallArg::Ident(taken.ident),
                    WCallArg::Ident(taken.condition),
                ],
            ),
            WExprCall::PhiNotTaken => (String::from(PHI_NOT_TAKEN), vec![]),
        };
        (construct_call_fn_path(fn_operand), args)
    }

    pub fn right_idents(&self) -> Vec<WIdent> {
        let mut result = Vec::new();
        let (_call_fn, args) = self.clone().call_fn_and_args();
        for arg in args {
            match arg {
                WCallArg::Ident(ident) => result.push(ident),
                WCallArg::Literal(_) => {}
            }
        }
        result
    }

    pub fn replace_ident(&mut self, original: &WIdent, replacement: &WIdent) {
        match self {
            WExprCall::Call(call) => {
                for arg in &mut call.args {
                    match arg {
                        WCallArg::Ident(ident) => {
                            replace_ident(ident, original, replacement);
                        }
                        WCallArg::Literal(_) => {}
                    }
                }
            }
            WExprCall::MckUnary(mck_unary) => {
                replace_ident(&mut mck_unary.operand, original, replacement);
            }
            WExprCall::MckBinary(mck_binary) => {
                replace_ident(&mut mck_binary.a, original, replacement);
                replace_ident(&mut mck_binary.b, original, replacement);
            }
            WExprCall::MckExt(mck_ext) => {
                replace_ident(&mut mck_ext.from, original, replacement);
            }
            WExprCall::MckNew(mck_new) => match mck_new {
                WMckNew::Bitvector(_) => {}
                WMckNew::BitvectorArray(_arr, ident) => {
                    replace_ident(ident, original, replacement);
                }
            },
            WExprCall::BooleanNew(_) => {}
            WExprCall::StdClone(ident) => {
                replace_ident(ident, original, replacement);
            }
            WExprCall::ArrayRead(array_read) => {
                replace_ident(&mut array_read.base, original, replacement);
                replace_ident(&mut array_read.index, original, replacement);
            }
            WExprCall::ArrayWrite(array_write) => {
                replace_ident(&mut array_write.base, original, replacement);
                replace_ident(&mut array_write.element, original, replacement);
                replace_ident(&mut array_write.index, original, replacement);
            }
            WExprCall::Phi(phi) => {
                replace_ident(&mut phi.condition, original, replacement);
                replace_ident(&mut phi.then_ident, original, replacement);
                replace_ident(&mut phi.else_ident, original, replacement);
            }
            WExprCall::PhiTaken(phi_taken) => {
                replace_ident(&mut phi_taken.ident, original, replacement);
                replace_ident(&mut phi_taken.condition, original, replacement);
            }
            WExprCall::PhiNotTaken => {}
        }
    }
}

fn replace_ident(ident: &mut WIdent, original: &WIdent, replacement: &WIdent) {
    if ident == original {
        *ident = replacement.clone();
    }
}

impl IntoSyn<Expr> for WExprHighCall {
    fn into_syn(self) -> Expr {
        let span = Span::call_site();
        let (fn_operand, args) = match self {
            WExprHighCall::Call(call) => return call.into_syn(),
            WExprHighCall::StdUnary(call) => {
                let operation = call.op.to_string();
                (operation, vec![WCallArg::Ident(call.operand)])
            }
            WExprHighCall::StdBinary(call) => {
                let operation = call.op.to_string();
                (
                    operation,
                    vec![WCallArg::Ident(call.a), WCallArg::Ident(call.b)],
                )
            }
            WExprHighCall::MckExt(call) => {
                (String::from(MCK_HIGH_EXT), vec![WCallArg::Ident(call.from)])
            }
            WExprHighCall::MckNew(call) => match call {
                WHighMckNew::BitvectorArray(_type_array, ident) => (
                    String::from(MCK_HIGH_BITVECTOR_ARRAY_NEW),
                    vec![WCallArg::Ident(ident)],
                ),
                WHighMckNew::Bitvector(signedness, _width, constant) => (
                    String::from(match signedness {
                        Signedness::None => MCK_HIGH_BITVECTOR_NEW,
                        Signedness::Unsigned => MCK_HIGH_UNSIGNED_NEW,
                        Signedness::Signed => MCK_HIGH_SIGNED_NEW,
                    }),
                    vec![WCallArg::Literal(Lit::Int(LitInt::new(
                        constant.to_string().as_str(),
                        span,
                    )))],
                ),
            },
            WExprHighCall::BooleanNew(value) => (
                String::from(BOOLEAN_NEW),
                vec![WCallArg::Literal(Lit::Bool(LitBool { value, span }))],
            ),
            WExprHighCall::StdInto(call) => {
                (String::from(STD_INTO), vec![WCallArg::Ident(call.from)])
            }
            WExprHighCall::StdClone(from) => (String::from(STD_CLONE), vec![WCallArg::Ident(from)]),
            WExprHighCall::ArrayRead(read) => (
                String::from(ARRAY_READ),
                vec![WCallArg::Ident(read.base), WCallArg::Ident(read.index)],
            ),
            WExprHighCall::ArrayWrite(write) => (
                String::from(ARRAY_WRITE),
                vec![
                    WCallArg::Ident(write.base),
                    WCallArg::Ident(write.index),
                    WCallArg::Ident(write.element),
                ],
            ),
            WExprHighCall::Phi(phi) => (
                String::from(PHI),
                vec![
                    WCallArg::Ident(phi.then_ident),
                    WCallArg::Ident(phi.else_ident),
                ],
            ),
            WExprHighCall::PhiTaken(taken) => (
                String::from(PHI_TAKEN),
                vec![
                    WCallArg::Ident(taken.ident),
                    WCallArg::Ident(taken.condition),
                ],
            ),
            WExprHighCall::PhiNotTaken => (String::from(PHI_NOT_TAKEN), vec![]),
        };
        let fn_path = construct_call_fn_path(fn_operand);
        WCall { fn_path, args }.into_syn()
    }
}

impl IntoSyn<Expr> for WCall {
    fn into_syn(self) -> Expr {
        let path = self.fn_path.into();

        let mut args = Punctuated::from_iter(self.args.into_iter().map(|arg| match arg {
            WCallArg::Ident(ident) => create_expr_ident(ident.into()),
            WCallArg::Literal(lit) => Expr::Lit(ExprLit {
                attrs: Vec::new(),
                lit,
            }),
        }));

        // kludge CBound
        if path_matches_global_names(&path, &["mck", "forward", "Bitvector", "new"]) {
            args.push(create_expr_path(syn_path::path!(::mck::misc::CBound)));
        }

        if path_matches_global_names(&path, &["mck", "forward", "Array", "new_filled"]) {
            args.insert(0, create_expr_path(syn_path::path!(::mck::misc::CBound)));
        }

        Expr::Call(ExprCall {
            attrs: Vec::new(),
            func: Box::new(Expr::Path(ExprPath {
                attrs: vec![],
                path,
                qself: None,
            })),
            paren_token: Paren::default(),
            args,
        })
    }
}

fn construct_call_fn_path(fn_operand: String) -> WPath {
    let span = Span::call_site();

    // construct the WPath
    let without_leading = fn_operand
        .strip_prefix("::")
        .expect("Special function operand should have a leading prefix");
    let segments: Vec<WPathSegment> = without_leading
        .split("::")
        .map(|segment| WPathSegment {
            ident: WIdent::new(String::from(segment), span),
        })
        .collect();
    WPath {
        leading_colon: Some(WSpan::from_span(span)),
        segments,
    }
}

impl Debug for WExprCall {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let (call_fn, args) = self.clone().call_fn_and_args();

        write!(f, "{:?}", call_fn)?;

        let mut franz = f.debug_tuple("");

        for arg in &args {
            franz.field(arg);
        }

        franz.finish()
    }
}

impl Debug for WCallArg {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Ident(ident) => ident.fmt(f),
            Self::Literal(lit) => write!(f, "{:?}", lit),
        }
    }
}