fn make_trampoline(
    isa: &dyn TargetIsa,
    code_memory: &mut CodeMemory,
    fn_builder_ctx: &mut FunctionBuilderContext,
    call_id: u32,
    signature: &ir::Signature,
) -> (*const VMFunctionBody, Vec<TrapInformation>) {
    // Mostly reverse copy of the similar method from wasmtime's
    // wasmtime-jit/src/compiler.rs.
    let pointer_type = isa.pointer_type();
    let mut stub_sig = ir::Signature::new(isa.frontend_config().default_call_conv);

    // Add the `vmctx` parameter.
    stub_sig.params.push(ir::AbiParam::special(
        pointer_type,
        ir::ArgumentPurpose::VMContext,
    ));

    // Add the `call_id` parameter.
    stub_sig.params.push(ir::AbiParam::new(types::I32));

    // Add the `values_vec` parameter.
    stub_sig.params.push(ir::AbiParam::new(pointer_type));

    // Add error/trap return.
    stub_sig.returns.push(ir::AbiParam::new(types::I32));

    let value_size = 16;
    let values_vec_len = ((value_size as usize)
        * cmp::max(signature.params.len() - 1, signature.returns.len()))
        as u32;

    let mut context = Context::new();
    context.func = Function::with_name_signature(ExternalName::user(0, 0), signature.clone());
    context.func.collect_frame_layout_info();

    let ss = context.func.create_stack_slot(StackSlotData::new(
        StackSlotKind::ExplicitSlot,
        values_vec_len,
    ));

    {
        let mut builder = FunctionBuilder::new(&mut context.func, fn_builder_ctx);
        let block0 = builder.create_ebb();

        builder.append_ebb_params_for_function_params(block0);
        builder.switch_to_block(block0);
        builder.seal_block(block0);

        let values_vec_ptr_val = builder.ins().stack_addr(pointer_type, ss, 0);
        let mflags = MemFlags::trusted();
        for i in 1..signature.params.len() {
            if i == 0 {
                continue;
            }

            let val = builder.func.dfg.ebb_params(block0)[i];
            builder.ins().store(
                mflags,
                val,
                values_vec_ptr_val,
                ((i - 1) * value_size) as i32,
            );
        }

        let vmctx_ptr_val = builder.func.dfg.ebb_params(block0)[0];
        let call_id_val = builder.ins().iconst(types::I32, call_id as i64);

        let callee_args = vec![vmctx_ptr_val, call_id_val, values_vec_ptr_val];

        let new_sig = builder.import_signature(stub_sig);

        let callee_value = builder
            .ins()
            .iconst(pointer_type, stub_fn as *const VMFunctionBody as i64);
        let call = builder
            .ins()
            .call_indirect(new_sig, callee_value, &callee_args);

        let call_result = builder.func.dfg.inst_results(call)[0];
        builder.ins().trapnz(call_result, API_TRAP_CODE);

        let mflags = MemFlags::trusted();
        let mut results = Vec::new();
        for (i, r) in signature.returns.iter().enumerate() {
            let load = builder.ins().load(
                r.value_type,
                mflags,
                values_vec_ptr_val,
                (i * value_size) as i32,
            );
            results.push(load);
        }
        builder.ins().return_(&results);
        builder.finalize()
    }

    let mut code_buf: Vec<u8> = Vec::new();
    let mut reloc_sink = binemit::TrampolineRelocSink {};
    let mut trap_sink = TrapSink::new();
    let mut stackmap_sink = binemit::NullStackmapSink {};
    context
        .compile_and_emit(
            isa,
            &mut code_buf,
            &mut reloc_sink,
            &mut trap_sink,
            &mut stackmap_sink,
        )
        .map_err(|error| pretty_error(&context.func, Some(isa), error))
        .expect("compile_and_emit");

    let unwind_info = CompiledFunctionUnwindInfo::new(isa, &context);

    let traps = trap_sink.traps;

    let addr = code_memory
        .allocate_for_function(&CompiledFunction {
            body: code_buf,
            jt_offsets: context.func.jt_offsets,
            unwind_info,
        })
        .expect("allocate_for_function")
        .as_ptr();
    (addr, traps)
}
/// Create a trampoline for invoking a Callable.
Create a trampoline for invoking a Callable.
Create a trampoline for invoking a Callable .
fn make_trampoline(
    isa: &dyn TargetIsa,
    code_memory: &mut CodeMemory,
    fn_builder_ctx: &mut FunctionBuilderContext,
    call_id: u32,
    signature: &ir::Signature,
) -> (*const VMFunctionBody, Vec<TrapInformation>) {
    // Mostly reverse copy of the similar method from wasmtime's
    // wasmtime-jit/src/compiler.rs.
    let pointer_type = isa.pointer_type();
    let mut stub_sig = ir::Signature::new(isa.frontend_config().default_call_conv);
    // Add the `vmctx` parameter.
    stub_sig.params.push(ir::AbiParam::special(
        pointer_type,
        ir::ArgumentPurpose::VMContext,
    ));
    // Add the `call_id` parameter.
    stub_sig.params.push(ir::AbiParam::new(types::I32));
    // Add the `values_vec` parameter.
    stub_sig.params.push(ir::AbiParam::new(pointer_type));
    // Add error/trap return.
    stub_sig.returns.push(ir::AbiParam::new(types::I32));
    let value_size = 16;
    let values_vec_len = ((value_size as usize)
        * cmp::max(signature.params.len() - 1, signature.returns.len()))
        as u32;
    let mut context = Context::new();
    context.func = Function::with_name_signature(ExternalName::user(0, 0), signature.clone());
    context.func.collect_frame_layout_info();
    let ss = context.func.create_stack_slot(StackSlotData::new(
        StackSlotKind::ExplicitSlot,
        values_vec_len,
    ));
    {
        let mut builder = FunctionBuilder::new(&mut context.func, fn_builder_ctx);
        let block0 = builder.create_ebb();
        builder.append_ebb_params_for_function_params(block0);
        builder.switch_to_block(block0);
        builder.seal_block(block0);
        let values_vec_ptr_val = builder.ins().stack_addr(pointer_type, ss, 0);
        let mflags = MemFlags::trusted();
        for i in 1..signature.params.len() {
            if i == 0 {
                continue;
            }
            let val = builder.func.dfg.ebb_params(block0)[i];
            builder.ins().store(
                mflags,
                val,
                values_vec_ptr_val,
                ((i - 1) * value_size) as i32,
            );
        }
        let vmctx_ptr_val = builder.func.dfg.ebb_params(block0)[0];
        let call_id_val = builder.ins().iconst(types::I32, call_id as i64);
        let callee_args = vec![vmctx_ptr_val, call_id_val, values_vec_ptr_val];
        let new_sig = builder.import_signature(stub_sig);
        let callee_value = builder
            .ins()
            .iconst(pointer_type, stub_fn as *const VMFunctionBody as i64);
        let call = builder
            .ins()
            .call_indirect(new_sig, callee_value, &callee_args);
        let call_result = builder.func.dfg.inst_results(call)[0];
        builder.ins().trapnz(call_result, API_TRAP_CODE);
        let mflags = MemFlags::trusted();
        let mut results = Vec::new();
        for (i, r) in signature.returns.iter().enumerate() {
            let load = builder.ins().load(
                r.value_type,
                mflags,
                values_vec_ptr_val,
                (i * value_size) as i32,
            );
            results.push(load);
        }
        builder.ins().return_(&results);
        builder.finalize()
    }
    let mut code_buf: Vec<u8> = Vec::new();
    let mut reloc_sink = binemit::TrampolineRelocSink {};
    let mut trap_sink = TrapSink::new();
    let mut stackmap_sink = binemit::NullStackmapSink {};
    context
        .compile_and_emit(
            isa,
            &mut code_buf,
            &mut reloc_sink,
            &mut trap_sink,
            &mut stackmap_sink,
        )
        .map_err(|error| pretty_error(&context.func, Some(isa), error))
        .expect("compile_and_emit");
    let unwind_info = CompiledFunctionUnwindInfo::new(isa, &context);
    let traps = trap_sink.traps;
    let addr = code_memory
        .allocate_for_function(&CompiledFunction {
            body: code_buf,
            jt_offsets: context.func.jt_offsets,
            unwind_info,
        })
        .expect("allocate_for_function")
        .as_ptr();
    (addr, traps)
}
fn make_trampoline ( isa : & dyn TargetIsa , code_memory : & mut CodeMemory , fn_builder_ctx : & mut FunctionBuilderContext , call_id : u32 , signature : & ir :: Signature , ) -> ( * const VMFunctionBody , Vec < TrapInformation > ) { // Mostly reverse copy of the similar method from wasmtime's // wasmtime-jit/src/compiler.rs. let pointer_type = isa . pointer_type ( ) ; let mut stub_sig = ir :: Signature :: new ( isa . frontend_config ( ) . default_call_conv ) ; // Add the `vmctx` parameter. stub_sig . params . push ( ir :: AbiParam :: special ( pointer_type , ir :: ArgumentPurpose :: VMContext , ) ) ; // Add the `call_id` parameter. stub_sig . params . push ( ir :: AbiParam :: new ( types :: I32 ) ) ; // Add the `values_vec` parameter. stub_sig . params . push ( ir :: AbiParam :: new ( pointer_type ) ) ; // Add error/trap return. stub_sig . returns . push ( ir :: AbiParam :: new ( types :: I32 ) ) ; let value_size = 16 ; let values_vec_len = ( ( value_size as usize ) * cmp :: max ( signature . params . len ( ) - 1 , signature . returns . len ( ) ) ) as u32 ; let mut context = Context :: new ( ) ; context . func = Function :: with_name_signature ( ExternalName :: user ( 0 , 0 ) , signature . clone ( ) ) ; context . func . collect_frame_layout_info ( ) ; let ss = context . func . create_stack_slot ( StackSlotData :: new ( StackSlotKind :: ExplicitSlot , values_vec_len , ) ) ; { let mut builder = FunctionBuilder :: new ( & mut context . func , fn_builder_ctx ) ; let block0 = builder . create_ebb ( ) ; builder . append_ebb_params_for_function_params ( block0 ) ; builder . switch_to_block ( block0 ) ; builder . seal_block ( block0 ) ; let values_vec_ptr_val = builder . ins ( ) . stack_addr ( pointer_type , ss , 0 ) ; let mflags = MemFlags :: trusted ( ) ; for i in 1 .. signature . params . len ( ) { if i == 0 { continue ; } let val = builder . func . dfg . ebb_params ( block0 ) [ i ] ; builder . ins ( ) . store ( mflags , val , values_vec_ptr_val , ( ( i - 1 ) * value_size ) as i32 , ) ; } let vmctx_ptr_val = builder . func . dfg . ebb_params ( block0 ) [ 0 ] ; let call_id_val = builder . ins ( ) . iconst ( types :: I32 , call_id as i64 ) ; let callee_args = vec ! [ vmctx_ptr_val call_id_val values_vec_ptr_val ] ; let new_sig = builder . import_signature ( stub_sig ) ; let callee_value = builder . ins ( ) . iconst ( pointer_type , stub_fn as * const VMFunctionBody as i64 ) ; let call = builder . ins ( ) . call_indirect ( new_sig , callee_value , & callee_args ) ; let call_result = builder . func . dfg . inst_results ( call ) [ 0 ] ; builder . ins ( ) . trapnz ( call_result , API_TRAP_CODE ) ; let mflags = MemFlags :: trusted ( ) ; let mut results = Vec :: new ( ) ; for ( i , r ) in signature . returns . iter ( ) . enumerate ( ) { let load = builder . ins ( ) . load ( r . value_type , mflags , values_vec_ptr_val , ( i * value_size ) as i32 , ) ; results . push ( load ) ; } builder . ins ( ) . return_ ( & results ) ; builder . finalize ( ) } let mut code_buf : Vec < u8 > = Vec :: new ( ) ; let mut reloc_sink = binemit :: TrampolineRelocSink { } ; let mut trap_sink = TrapSink :: new ( ) ; let mut stackmap_sink = binemit :: NullStackmapSink { } ; context . compile_and_emit ( isa , & mut code_buf , & mut reloc_sink , & mut trap_sink , & mut stackmap_sink , ) . map_err ( | error | pretty_error ( & context . func , Some ( isa ) , error ) ) . expect ( " " ) ; let unwind_info = CompiledFunctionUnwindInfo :: new ( isa , & context ) ; let traps = trap_sink . traps ; let addr = code_memory . allocate_for_function ( & CompiledFunction { body : code_buf , jt_offsets : context . func . jt_offsets , unwind_info , } ) . expect ( " " ) . as_ptr ( ) ; ( addr , traps ) }
Create a trampoline for invoking a Callable.
Create a trampoline for invoking a Callable .
isa
&dyn TargetIsa
code_memory
&mut CodeMemory
fn_builder_ctx
&mut FunctionBuilderContext
call_id
u32
signature
&ir::Signature
isa
&dyn TargetIsa
code_memory
&mut CodeMemory
fn_builder_ctx
&mut FunctionBuilderContext
call_id
u32
signature
&ir::Signature