Cleanup stack space, handle multiple results, and free registers after emitting the call.
(
sig: &ABISig,
callee_context: &ContextArgs,
callee_kind: &CalleeKind,
reserved_space: u32,
ret_area: Option<RetArea>,
masm: &mut M,
context: &
| 365 | /// Cleanup stack space, handle multiple results, and free registers after |
| 366 | /// emitting the call. |
| 367 | fn cleanup<M: MacroAssembler>( |
| 368 | sig: &ABISig, |
| 369 | callee_context: &ContextArgs, |
| 370 | callee_kind: &CalleeKind, |
| 371 | reserved_space: u32, |
| 372 | ret_area: Option<RetArea>, |
| 373 | masm: &mut M, |
| 374 | context: &mut CodeGenContext<Emission>, |
| 375 | ) -> Result<()> { |
| 376 | // Free any registers holding any function references. |
| 377 | match callee_kind { |
| 378 | CalleeKind::Indirect(r) => context.free_reg(*r), |
| 379 | _ => {} |
| 380 | } |
| 381 | |
| 382 | // Free any registers used as part of the [ContextArgs]. |
| 383 | for loc in callee_context.as_slice() { |
| 384 | match loc { |
| 385 | VMContextLoc::Reg(r) => context.free_reg(*r), |
| 386 | _ => {} |
| 387 | } |
| 388 | } |
| 389 | // Deallocate the reserved space for stack arguments and for alignment, |
| 390 | // which was allocated last. |
| 391 | masm.free_stack(reserved_space)?; |
| 392 | |
| 393 | ensure!( |
| 394 | sig.params.len_without_retptr() >= callee_context.len(), |
| 395 | CodeGenError::vmcontext_arg_expected() |
| 396 | ); |
| 397 | |
| 398 | // Drop params from value stack and calculate amount of machine stack |
| 399 | // space they consumed. |
| 400 | let mut stack_consumed = 0; |
| 401 | context.drop_last( |
| 402 | sig.params.len_without_retptr() - callee_context.len(), |
| 403 | |_regalloc, v| { |
| 404 | ensure!( |
| 405 | v.is_mem() || v.is_const(), |
| 406 | CodeGenError::unexpected_value_in_value_stack() |
| 407 | ); |
| 408 | if let Val::Memory(mem) = v { |
| 409 | stack_consumed += mem.slot.size; |
| 410 | } |
| 411 | Ok(()) |
| 412 | }, |
| 413 | )?; |
| 414 | |
| 415 | if let Some(ret_area) = ret_area { |
| 416 | if stack_consumed > 0 { |
| 417 | // Perform a memory move, by shuffling the result area to |
| 418 | // higher addresses. This is needed because the result area |
| 419 | // is located after any memory addresses located on the stack, |
| 420 | // and after spilled values consumed by the call. |
| 421 | let sp = ret_area.unwrap_sp(); |
| 422 | let result_bytes = sig.results_stack_size(); |
| 423 | ensure!( |
| 424 | sp.as_u32() >= stack_consumed + result_bytes, |
nothing calls this directly
no test coverage detected