(arch: &A, name: N, cc: C)
| 56 | } |
| 57 | |
| 58 | pub fn register_calling_convention<A, N, C>(arch: &A, name: N, cc: C) -> Ref<CoreCallingConvention> |
| 59 | where |
| 60 | A: Architecture, |
| 61 | N: BnStrCompatible, |
| 62 | C: 'static + CallingConvention, |
| 63 | { |
| 64 | struct CustomCallingConventionContext<C> |
| 65 | where |
| 66 | C: CallingConvention, |
| 67 | { |
| 68 | raw_handle: *mut BNCallingConvention, |
| 69 | cc: C, |
| 70 | } |
| 71 | |
| 72 | // TODO: It would be nice if these callbacks were moved out to the bottom of this file (maybe in another mod) |
| 73 | extern "C" fn cb_free<C>(ctxt: *mut c_void) |
| 74 | where |
| 75 | C: CallingConvention, |
| 76 | { |
| 77 | ffi_wrap!("CallingConvention::free", unsafe { |
| 78 | let _ctxt = Box::from_raw(ctxt as *mut CustomCallingConventionContext<C>); |
| 79 | }) |
| 80 | } |
| 81 | |
| 82 | extern "C" fn cb_free_register_list(_ctxt: *mut c_void, regs: *mut u32, count: usize) { |
| 83 | ffi_wrap!("CallingConvention::free_register_list", unsafe { |
| 84 | if regs.is_null() { |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | let regs_ptr = std::ptr::slice_from_raw_parts_mut(regs, count); |
| 89 | let _regs = Box::from_raw(regs_ptr); |
| 90 | }) |
| 91 | } |
| 92 | |
| 93 | extern "C" fn cb_caller_saved<C>(ctxt: *mut c_void, count: *mut usize) -> *mut u32 |
| 94 | where |
| 95 | C: CallingConvention, |
| 96 | { |
| 97 | ffi_wrap!("CallingConvention::caller_saved_registers", unsafe { |
| 98 | let ctxt = &*(ctxt as *mut CustomCallingConventionContext<C>); |
| 99 | let mut regs: Vec<_> = ctxt |
| 100 | .cc |
| 101 | .caller_saved_registers() |
| 102 | .iter() |
| 103 | .map(|r| r.0) |
| 104 | .collect(); |
| 105 | |
| 106 | // SAFETY: `count` is an out parameter |
| 107 | *count = regs.len(); |
| 108 | let regs_ptr = regs.as_mut_ptr(); |
| 109 | std::mem::forget(regs); |
| 110 | regs_ptr |
| 111 | }) |
| 112 | } |
| 113 | |
| 114 | extern "C" fn cb_callee_saved<C>(ctxt: *mut c_void, count: *mut usize) -> *mut u32 |
| 115 | where |
no test coverage detected