Skip to main content

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                self.visit_inner(x)
191            }
192        }
193    }
194
195    fn visit_llbc_block(&mut self, _: &mut llbc_ast::Block) -> ControlFlow<Infallible> {
196        ControlFlow::Continue(())
197    }
198}
199
200/// We do the following.
201///
202/// If `p` is a projection (for instance: `var`, `*var`, `var.f`, etc.), we
203/// detect:
204/// - whether it operates on a slice or an array (we keep track of the types)
205/// - whether the access might mutate the value or not (it is
206///   the case if it is in a `move`, `&mut` or at the lhs of an assignment),
207///   and do the following transformations
208///
209/// ```text
210///   // If array and mutable access:
211///   ... p[i] ...
212///      ~~>
213///   tmp0 = &mut p
214///   tmp1 = ArrayIndexMut(move p, i)
215///   ... *tmp1 ...
216///
217///   // If array and non-mutable access:
218///   ... p[i] ...
219///      ~~>
220///   tmp0 := & p
221///   tmp1 := ArrayIndexShared(move tmp0, i)
222///   ... *tmp1 ...
223///
224///   // Omitting the slice cases, which are similar
225/// ```
226///
227/// For instance, it leads to the following transformations:
228/// ```text
229///   // x : [u32; N]
230///   y : u32 = copy x[i]
231///      ~~>
232///   tmp0 : & [u32; N] := &x
233///   tmp1 : &u32 = ArrayIndexShared(move tmp0, i)
234///   y : u32 = copy (*tmp1)
235///
236///   // x : &[T; N]
237///   y : &T = & (*x)[i]
238///      ~~>
239///   tmp0 : & [T; N] := & (*x)
240///   tmp1 : &T = ArrayIndexShared(move tmp0, i)
241///   y : &T = & (*tmp1)
242///
243///   // x : [u32; N]
244///   y = &mut x[i]
245///      ~~>
246///   tmp0 : &mut [u32; N] := &mut x
247///   tmp1 : &mut u32 := ArrayIndexMut(move tmp0, i)
248///   y = &mut (*tmp)
249///
250///   // When using an index on the lhs:
251///   // y : [T; N]
252///   y[i] = x
253///      ~~>
254///   tmp0 : &mut [T; N] := &mut y;
255///   tmp1 : &mut T = ArrayIndexMut(move y, i)
256///   *tmp1 = x
257/// ```
258pub struct Transform;
259impl LlbcPass for Transform {
260    fn should_run(&self, options: &crate::options::TranslateOptions) -> bool {
261        options.index_to_function_calls
262    }
263
264    fn transform_function(&self, ctx: &mut TransformCtx, decl: &mut FunDecl) {
265        decl.transform_llbc_statements(ctx, |ctx, st: &mut Statement| {
266            let mut visitor = IndexVisitor {
267                ctx,
268                place_mutability_stack: Vec::new(),
269            };
270            use StatementKind::*;
271            match &mut st.kind {
272                Assign(..) | SetDiscriminant(..) | CopyNonOverlapping(_) | Drop(..) | Call(..) => {
273                    let _ = visitor.visit_inner_with_mutability(st, true);
274                }
275                Switch(..) | PlaceMention(..) => {
276                    let _ = visitor.visit_inner_with_mutability(st, false);
277                }
278                Nop
279                | Error(..)
280                | InlineAsm { .. }
281                | Assert { .. }
282                | Abort(..)
283                | StorageDead(..)
284                | StorageLive(..)
285                | Return
286                | Break(..)
287                | Continue(..)
288                | Loop(..) => {
289                    let _ = st.drive_body_mut(&mut visitor);
290                }
291            }
292        })
293    }
294}