charon_lib/transform/simplify_output/
index_to_function_calls.rs

1//! Desugar array/slice index operations to function calls.
2
3use crate::llbc_ast::*;
4use crate::transform::TransformCtx;
5use crate::transform::ctx::{BodyTransformCtx, LlbcStatementTransformCtx};
6use derive_generic_visitor::*;
7
8use crate::transform::ctx::LlbcPass;
9
10/// We replace some place constructors with function calls. To do that, we explore all the places
11/// in a body and deconstruct a given place access into intermediate assignments.
12///
13/// We accumulate the new assignments as statements in the visitor, and at the end we insert these
14/// statements before the one that was just explored.
15#[derive(Visitor)]
16struct IndexVisitor<'a, 'b> {
17    ctx: &'b mut LlbcStatementTransformCtx<'a>,
18    // When we visit a place, we need to know if it is being accessed mutably or not. Whenever we
19    // visit something that contains a place we push the relevant mutability on this stack.
20    // Unfortunately this requires us to be very careful to catch all the cases where we see
21    // places.
22    place_mutability_stack: Vec<bool>,
23}
24
25impl<'a, 'b> IndexVisitor<'a, 'b> {
26    /// transform `place: subplace[i]` into indexing function calls for `subplace` and `i`
27    fn transform_place(&mut self, mut_access: bool, place: &mut Place) {
28        use ProjectionElem::*;
29        // This function is naturally called recusively, so `subplace` cannot be another `Index` or `Subslice`.
30        // Hence, `subplace`, if still projecting, must be either a `Deref` or a `Field`.
31        let Some((subplace, pe @ (Index { .. } | Subslice { .. }))) = place.as_projection() else {
32            return;
33        };
34
35        let (ty, len) = match subplace.ty.kind() {
36            TyKind::Array(ty, len) => (ty.clone(), Some(len.clone())),
37            TyKind::Slice(ty) => (ty.clone(), None),
38            _ => unreachable!("Indexing can only be done on arrays or slices"),
39        };
40
41        // The built-in function to call.
42        let indexing_function = {
43            let builtin_fun = BuiltinFunId::Index(BuiltinIndexOp {
44                is_array: subplace.ty.kind().is_array(),
45                mutability: RefKind::mutable(mut_access),
46                is_range: pe.is_subslice(),
47            });
48            // Same generics as the array/slice type, except for the extra lifetime.
49            let generics = GenericArgs {
50                types: [ty.clone()].into(),
51                const_generics: len.map(|l| [l].into()).unwrap_or_default(),
52                regions: [Region::Erased].into(),
53                trait_refs: [].into(),
54            };
55            FnOperand::Regular(FnPtr::new(FnPtrKind::mk_builtin(builtin_fun), generics))
56        };
57
58        let output_inner_ty = if matches!(pe, Index { .. }) {
59            ty
60        } else {
61            TyKind::Slice(ty).into_ty()
62        };
63        let output_ty = {
64            TyKind::Ref(
65                Region::Erased,
66                output_inner_ty.clone(),
67                RefKind::mutable(mut_access),
68            )
69            .into_ty()
70        };
71
72        // Push the statements:
73        // `storage_live(tmp0)`
74        // `tmp0 = &{mut}p`
75        let input_var =
76            self.ctx
77                .borrow_to_new_var(subplace.clone(), BorrowKind::mutable(mut_access), None);
78
79        // Construct the arguments to pass to the indexing function.
80        let mut args = vec![Operand::Move(input_var)];
81        if let Subslice { from, .. } = &pe {
82            args.push(from.as_ref().clone());
83        }
84        let (last_arg, from_end) = match &pe {
85            Index {
86                offset: x,
87                from_end,
88                ..
89            }
90            | Subslice {
91                to: x, from_end, ..
92            } => (x.as_ref().clone(), *from_end),
93            _ => unreachable!(),
94        };
95        let to_idx = self
96            .ctx
97            .compute_subslice_end_idx(subplace, last_arg, from_end);
98        args.push(to_idx);
99
100        // Call the indexing function:
101        // `storage_live(tmp1)`
102        // `tmp1 = {Array,Slice}{Mut,Shared}{Index,SubSlice}(move tmp0, <other args>)`
103        let output_var = {
104            let output_var = self.ctx.fresh_var(None, output_ty);
105            let index_call = Call {
106                func: indexing_function,
107                args,
108                dest: output_var.clone(),
109            };
110            let kind = StatementKind::Call(index_call);
111            self.ctx
112                .statements
113                .push(Statement::new(self.ctx.span, kind));
114            output_var
115        };
116
117        // Update the place.
118        *place = output_var.project(ProjectionElem::Deref, output_inner_ty);
119    }
120
121    /// Calls `self.visit_inner()` with `mutability` pushed on the stack.
122    fn visit_inner_with_mutability<T>(
123        &mut self,
124        x: &mut T,
125        mutability: bool,
126    ) -> ControlFlow<Infallible>
127    where
128        T: for<'s> DriveMut<'s, BodyVisitableWrapper<Self>> + BodyVisitable,
129    {
130        self.place_mutability_stack.push(mutability);
131        self.visit_inner(x)?;
132        self.place_mutability_stack.pop();
133        Continue(())
134    }
135}
136
137/// The visitor methods.
138impl VisitBodyMut for IndexVisitor<'_, '_> {
139    /// We explore places from the inside-out --- recursion naturally happens here.
140    fn exit_place(&mut self, place: &mut Place) {
141        // We have intercepted every traversal that would reach a place and pushed the correct
142        // mutability on the stack.
143        let mut_access = *self.place_mutability_stack.last().unwrap();
144        self.transform_place(mut_access, place);
145    }
146
147    fn visit_operand(&mut self, x: &mut Operand) -> ControlFlow<Infallible> {
148        match x {
149            Operand::Move(_) => self.visit_inner_with_mutability(x, true),
150            Operand::Copy(_) => self.visit_inner_with_mutability(x, false),
151            Operand::Const(..) => self.visit_inner(x),
152        }
153    }
154
155    fn visit_call(&mut self, x: &mut Call) -> ControlFlow<Infallible> {
156        self.visit_inner_with_mutability(x, true)
157    }
158
159    fn visit_fn_operand(&mut self, x: &mut FnOperand) -> ControlFlow<Infallible> {
160        match x {
161            FnOperand::Regular(_) => self.visit_inner(x),
162            FnOperand::Dynamic(_) => self.visit_inner_with_mutability(x, true),
163        }
164    }
165
166    fn visit_rvalue(&mut self, x: &mut Rvalue) -> ControlFlow<Infallible> {
167        use Rvalue::*;
168        match x {
169            // `UniqueImmutable` de facto gives mutable access and only shows up if there is nested
170            // mutable access.
171            RawPtr {
172                kind: RefKind::Mut, ..
173            }
174            | Ref {
175                kind: BorrowKind::Mut | BorrowKind::TwoPhaseMut | BorrowKind::UniqueImmutable,
176                ..
177            } => self.visit_inner_with_mutability(x, true),
178            RawPtr {
179                kind: RefKind::Shared,
180                ..
181            }
182            | Ref {
183                kind: BorrowKind::Shared | BorrowKind::Shallow,
184                ..
185            }
186            | Discriminant(..)
187            | Len(..) => self.visit_inner_with_mutability(x, false),
188
189            Use(_) | NullaryOp(..) | UnaryOp(..) | BinaryOp(..) | Aggregate(..) | Repeat(..)
190            | ShallowInitBox(..) => self.visit_inner(x),
191        }
192    }
193
194    fn visit_llbc_block(&mut self, _: &mut llbc_ast::Block) -> ControlFlow<Infallible> {
195        ControlFlow::Continue(())
196    }
197}
198
199/// We do the following.
200///
201/// If `p` is a projection (for instance: `var`, `*var`, `var.f`, etc.), we
202/// detect:
203/// - whether it operates on a slice or an array (we keep track of the types)
204/// - whether the access might mutate the value or not (it is
205///   the case if it is in a `move`, `&mut` or at the lhs of an assignment),
206///   and do the following transformations
207///
208/// ```text
209///   // If array and mutable access:
210///   ... p[i] ...
211///      ~~>
212///   tmp0 = &mut p
213///   tmp1 = ArrayIndexMut(move p, i)
214///   ... *tmp1 ...
215///
216///   // If array and non-mutable access:
217///   ... p[i] ...
218///      ~~>
219///   tmp0 := & p
220///   tmp1 := ArrayIndexShared(move tmp0, i)
221///   ... *tmp1 ...
222///
223///   // Omitting the slice cases, which are similar
224/// ```
225///
226/// For instance, it leads to the following transformations:
227/// ```text
228///   // x : [u32; N]
229///   y : u32 = copy x[i]
230///      ~~>
231///   tmp0 : & [u32; N] := &x
232///   tmp1 : &u32 = ArrayIndexShared(move tmp0, i)
233///   y : u32 = copy (*tmp1)
234///
235///   // x : &[T; N]
236///   y : &T = & (*x)[i]
237///      ~~>
238///   tmp0 : & [T; N] := & (*x)
239///   tmp1 : &T = ArrayIndexShared(move tmp0, i)
240///   y : &T = & (*tmp1)
241///
242///   // x : [u32; N]
243///   y = &mut x[i]
244///      ~~>
245///   tmp0 : &mut [u32; N] := &mut x
246///   tmp1 : &mut u32 := ArrayIndexMut(move tmp0, i)
247///   y = &mut (*tmp)
248///
249///   // When using an index on the lhs:
250///   // y : [T; N]
251///   y[i] = x
252///      ~~>
253///   tmp0 : &mut [T; N] := &mut y;
254///   tmp1 : &mut T = ArrayIndexMut(move y, i)
255///   *tmp1 = x
256/// ```
257pub struct Transform;
258impl LlbcPass for Transform {
259    fn should_run(&self, options: &crate::options::TranslateOptions) -> bool {
260        options.index_to_function_calls
261    }
262
263    fn transform_function(&self, ctx: &mut TransformCtx, decl: &mut FunDecl) {
264        decl.transform_llbc_statements(ctx, |ctx, st: &mut Statement| {
265            let mut visitor = IndexVisitor {
266                ctx,
267                place_mutability_stack: Vec::new(),
268            };
269            use StatementKind::*;
270            match &mut st.kind {
271                Assign(..)
272                | SetDiscriminant(..)
273                | CopyNonOverlapping(_)
274                | Drop(..)
275                | Deinit(..)
276                | Call(..) => {
277                    let _ = visitor.visit_inner_with_mutability(st, true);
278                }
279                Switch(..) => {
280                    let _ = visitor.visit_inner_with_mutability(st, false);
281                }
282                Nop | Error(..) | Assert(..) | Abort(..) | StorageDead(..) | StorageLive(..)
283                | Return | Break(..) | Continue(..) | Loop(..) => {
284                    let _ = st.drive_body_mut(&mut visitor);
285                }
286            }
287        })
288    }
289}