|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 OR MIT |
| 3 | + |
| 4 | +//! This file contains functions related to codegenning MIR functions into gotoc |
| 5 | +
|
| 6 | +use super::cbmc::goto_program::{Expr, Stmt, Symbol}; |
| 7 | +use super::metadata::*; |
| 8 | +use rustc_middle::mir::{HasLocalDecls, Local}; |
| 9 | +use rustc_middle::ty::{self, Instance, TyS}; |
| 10 | +use tracing::{debug, warn}; |
| 11 | + |
| 12 | +impl<'tcx> GotocCtx<'tcx> { |
| 13 | + fn codegen_declare_variables(&mut self) { |
| 14 | + let mir = self.current_fn().mir(); |
| 15 | + let ldecls = mir.local_decls(); |
| 16 | + ldecls.indices().for_each(|lc| { |
| 17 | + if Some(lc) == mir.spread_arg { |
| 18 | + // We have already added this local in the function prelude, so |
| 19 | + // skip adding it again here. |
| 20 | + return; |
| 21 | + } |
| 22 | + let base_name = self.codegen_var_base_name(&lc); |
| 23 | + let name = self.codegen_var_name(&lc); |
| 24 | + let ldata = &ldecls[lc]; |
| 25 | + let t = self.monomorphize(ldata.ty); |
| 26 | + let t = self.codegen_ty(t); |
| 27 | + let loc = self.codegen_span2(&ldata.source_info.span); |
| 28 | + let sym = |
| 29 | + Symbol::variable(name, base_name, t, self.codegen_span2(&ldata.source_info.span)); |
| 30 | + let sym_e = sym.to_expr(); |
| 31 | + self.symbol_table.insert(sym); |
| 32 | + |
| 33 | + // Index 0 represents the return value, which does not need to be |
| 34 | + // declared in the first block |
| 35 | + if lc.index() < 1 || lc.index() > mir.arg_count { |
| 36 | + self.current_fn_mut().push_onto_block(Stmt::decl(sym_e, None, loc)); |
| 37 | + } |
| 38 | + }); |
| 39 | + } |
| 40 | + |
| 41 | + pub fn codegen_function(&mut self, instance: Instance<'tcx>) { |
| 42 | + self.set_current_fn(instance); |
| 43 | + let name = self.current_fn().name(); |
| 44 | + let old_sym = self.symbol_table.lookup(&name).unwrap(); |
| 45 | + assert!(old_sym.is_function()); |
| 46 | + if old_sym.is_function_definition() { |
| 47 | + warn!("Double codegen of {:?}", old_sym); |
| 48 | + } else if self.should_skip_current_fn() { |
| 49 | + debug!("Skipping function {}", self.current_fn().readable_name()); |
| 50 | + let loc = self.codegen_span2(&self.current_fn().mir().span); |
| 51 | + let body = Stmt::assert_false( |
| 52 | + &format!( |
| 53 | + "The function {} is not currently supported by RMC", |
| 54 | + self.current_fn().readable_name() |
| 55 | + ), |
| 56 | + loc, |
| 57 | + ); |
| 58 | + self.symbol_table.update_fn_declaration_with_definition(&name, body); |
| 59 | + } else { |
| 60 | + let mir = self.current_fn().mir(); |
| 61 | + self.print_instance(instance, mir); |
| 62 | + let labels = self |
| 63 | + .current_fn() |
| 64 | + .mir() |
| 65 | + .basic_blocks() |
| 66 | + .indices() |
| 67 | + .map(|bb| format!("{:?}", bb)) |
| 68 | + .collect(); |
| 69 | + self.current_fn_mut().set_labels(labels); |
| 70 | + self.codegen_function_prelude(); |
| 71 | + self.codegen_declare_variables(); |
| 72 | + |
| 73 | + mir.basic_blocks().iter_enumerated().for_each(|(bb, bbd)| self.codegen_block(bb, bbd)); |
| 74 | + |
| 75 | + let loc = self.codegen_span2(&mir.span); |
| 76 | + let stmts = self.current_fn_mut().extract_block(); |
| 77 | + let body = Stmt::block(stmts, loc); |
| 78 | + self.symbol_table.update_fn_declaration_with_definition(&name, body); |
| 79 | + } |
| 80 | + self.reset_current_fn(); |
| 81 | + } |
| 82 | + |
| 83 | + /// MIR functions have a `spread_arg` field that specifies whether the |
| 84 | + /// final argument to the function is "spread" at the LLVM/codegen level |
| 85 | + /// from a tuple into its individual components. (Used for the "rust- |
| 86 | + /// call" ABI, necessary because dynamic trait closure cannot have an |
| 87 | + /// argument list in MIR that is both generic and variadic, so Rust |
| 88 | + /// allows a generic tuple). |
| 89 | + /// |
| 90 | + /// If `spread_arg` is Some, then the wrapped value is the local that is |
| 91 | + /// to be "spread"/untupled. However, the MIR function body itself expects |
| 92 | + /// the tuple instead of the individual components, so we need to generate |
| 93 | + /// a function prelude that _retuples_, that is, writes the components |
| 94 | + /// back to the tuple local for use in the body. |
| 95 | + /// |
| 96 | + /// See: |
| 97 | + /// https://rust-lang.zulipchat.com/#narrow/stream/182449-t-compiler.2Fhelp/topic/Determine.20untupled.20closure.20args.20from.20Instance.3F |
| 98 | + fn codegen_function_prelude(&mut self) { |
| 99 | + let mir = self.current_fn().mir(); |
| 100 | + if mir.spread_arg.is_none() { |
| 101 | + // No special tuple argument, no work to be done. |
| 102 | + return; |
| 103 | + } |
| 104 | + let spread_arg = mir.spread_arg.unwrap(); |
| 105 | + let spread_data = &mir.local_decls()[spread_arg]; |
| 106 | + let loc = self.codegen_span2(&spread_data.source_info.span); |
| 107 | + |
| 108 | + // When we codegen the function signature elsewhere, we will codegen the |
| 109 | + // untupled version. So, the tuple argument itself needs to have a |
| 110 | + // symbol declared for it outside of the function signature, we do that |
| 111 | + // here. |
| 112 | + let tup_typ = self.codegen_ty(self.monomorphize(spread_data.ty)); |
| 113 | + let tup_sym = Symbol::variable( |
| 114 | + self.codegen_var_name(&spread_arg), |
| 115 | + self.codegen_var_base_name(&spread_arg), |
| 116 | + tup_typ.clone(), |
| 117 | + loc.clone(), |
| 118 | + ); |
| 119 | + self.symbol_table.insert(tup_sym.clone()); |
| 120 | + |
| 121 | + // Get the function signature from MIR, _before_ we untuple |
| 122 | + let fntyp = self.current_fn().instance().ty(self.tcx, ty::ParamEnv::reveal_all()); |
| 123 | + let sig = match fntyp.kind() { |
| 124 | + ty::FnPtr(..) | ty::FnDef(..) => fntyp.fn_sig(self.tcx).skip_binder(), |
| 125 | + // Closures themselves will have their arguments already untupled, |
| 126 | + // see Zulip link above. |
| 127 | + ty::Closure(..) => unreachable!( |
| 128 | + "Unexpected `spread arg` set for closure, got: {:?}, {:?}", |
| 129 | + fntyp, |
| 130 | + self.current_fn().readable_name() |
| 131 | + ), |
| 132 | + _ => unreachable!( |
| 133 | + "Expected function type for `spread arg` prelude, got: {:?}, {:?}", |
| 134 | + fntyp, |
| 135 | + self.current_fn().readable_name() |
| 136 | + ), |
| 137 | + }; |
| 138 | + |
| 139 | + // Now that we have the tuple, write the individual component locals |
| 140 | + // back to it as a GotoC struct. |
| 141 | + let tupe = sig.inputs().last().unwrap(); |
| 142 | + let args: Vec<&TyS<'tcx>> = match tupe.kind() { |
| 143 | + ty::Tuple(substs) => substs.iter().map(|s| s.expect_ty()).collect(), |
| 144 | + _ => unreachable!("a function's spread argument must be a tuple"), |
| 145 | + }; |
| 146 | + |
| 147 | + // Convert each arg to a GotoC expression. |
| 148 | + let mut arg_exprs = Vec::new(); |
| 149 | + let starting_idx = sig.inputs().len(); |
| 150 | + for (arg_i, arg_t) in args.iter().enumerate() { |
| 151 | + // The components come at the end, so offset by the untupled length. |
| 152 | + let lc = Local::from_usize(arg_i + starting_idx); |
| 153 | + let (name, base_name) = self.codegen_spread_arg_name(&lc); |
| 154 | + let sym = Symbol::variable(name, base_name, self.codegen_ty(arg_t), loc.clone()); |
| 155 | + self.symbol_table.insert(sym.clone()); |
| 156 | + arg_exprs.push(sym.to_expr()); |
| 157 | + } |
| 158 | + |
| 159 | + // Finally, combine the expression into a struct. |
| 160 | + let tuple_expr = Expr::struct_expr_from_values(tup_typ, arg_exprs, &self.symbol_table) |
| 161 | + .with_location(loc.clone()); |
| 162 | + self.current_fn_mut().push_onto_block(Stmt::decl(tup_sym.to_expr(), Some(tuple_expr), loc)); |
| 163 | + } |
| 164 | + |
| 165 | + pub fn declare_function(&mut self, instance: Instance<'tcx>) { |
| 166 | + debug!("declaring {}; {:?}", instance, instance); |
| 167 | + self.set_current_fn(instance); |
| 168 | + self.ensure(&self.current_fn().name(), |ctx, fname| { |
| 169 | + let mir = ctx.current_fn().mir(); |
| 170 | + Symbol::function( |
| 171 | + fname, |
| 172 | + ctx.fn_typ(), |
| 173 | + None, |
| 174 | + Some(ctx.current_fn().readable_name().to_string()), |
| 175 | + ctx.codegen_span2(&mir.span), |
| 176 | + ) |
| 177 | + }); |
| 178 | + self.reset_current_fn(); |
| 179 | + } |
| 180 | +} |
0 commit comments