Repository: dotnet/runtime
File: src/coreclr/jit/codegenwasm.cpp (C++)

Code before:
#endif // DEBUG
    GenTree* target = getCallTarget(call, &params.methHnd);

    if (target != nullptr)
    {
        // A call target can not be a contained indirection
        assert(!target->isContainedIndir());

        // Codegen should have already evaluated our target node (last) and pushed it onto the stack,
        //  ready for call_indirect. Consume it.
        genConsumeReg(target);

        if (target->isContainedIntOrIImmed())
        {
            NYI_WASM("Contained int or iimmed call target");
        }
        else
        {
            params.callType = EC_INDIR_R;
            // TODO-WASM: Figure out whether we need this. I think the target should always be on stack, not in a local.
            // params.ireg     = target->GetRegNum();
        }

        // TODO-WASM: Assert disabled due to above
        // assert(genIsValidIntReg(params.ireg));

        genEmitCallWithCurrentGC(params);
    }
    else
    {
        // If we have no target and this is a call with indirection cell then
        // we do an optimization where we load the call address directly from
        // the indirection cell instead of duplicating the tree. In BuildCall
        // we ensure that get an extra register for the purpose. Note that for
        // CFG the call might have changed to
        // CORINFO_HELP_DISPATCH_INDIRECT_CALL in which case we still have the
        // indirection cell but we should not try to optimize.
        WellKnownArg indirectionCellArgKind = WellKnownArg::None;
        if (!call->IsHelperCall(compiler, CORINFO_HELP_DISPATCH_INDIRECT_CALL))
        {
            indirectionCellArgKind = call->GetIndirectionCellArgKind();
        }

        if (indirectionCellArgKind != WellKnownArg::None)
        {
            assert(call->IsR2ROrVirtualStubRelativeIndir());

            params.callType = EC_INDIR_R;
            // params.ireg     = targetAddrReg;
            genEmitCallWithCurrentGC(params);
        }

Review comment (suggestion):
```suggestion
        params.callType = EC_INDIR_R;
```
`ireg` is for native platform that do (e. g.) `call rax`. As you note, our call targets will always be on the evaluation stack instead.

Code after:
        // we do an optimization where we load the call address directly from
        // the indirection cell instead of duplicating the tree. In BuildCall
        // we ensure that get an extra register for the purpose. Note that for
        // CFG the call might have changed to
        // CORINFO_HELP_DISPATCH_INDIRECT_CALL in which case we still have the
        // indirection cell but we should not try to optimize.
        WellKnownArg indirectionCellArgKind = WellKnownArg::None;
        if (!call->IsHelperCall(compiler, CORINFO_HELP_DISPATCH_INDIRECT_CALL))
        {
            indirectionCellArgKind = call->GetIndirectionCellArgKind();
        }

        if (indirectionCellArgKind != WellKnownArg::None)
        {
            assert(call->IsR2ROrVirtualStubRelativeIndir());

            params.callType = EC_INDIR_R;
            // params.ireg     = targetAddrReg;
            genEmitCallWithCurrentGC(params);
        }
        else
        {
            // Generate a direct call to a non-virtual user defined or helper method
            assert(call->IsHelperCall() || (call->gtCallType == CT_USER_FUNC));

            assert(call->gtEntryPoint.addr == NULL);

            if (call->IsHelperCall())
            {
                NYI_WASM("Call helper statically without indirection cell");
                CorInfoHelpFunc helperNum = compiler->eeGetHelperNum(params.methHnd);
                noway_assert(helperNum != CORINFO_HELP_UNDEF);

                CORINFO_CONST_LOOKUP helperLookup = compiler->compGetHelperFtn(helperNum);
                params.addr                       = helperLookup.addr;
                assert(helperLookup.accessType == IAT_VALUE);
            }
            else
            {
                // Direct call to a non-virtual user function.
                params.addr = call->gtDirectCallAddress;
            }

            params.callType = EC_FUNC_TOKEN;
            genEmitCallWithCurrentGC(params);
        }
    }
}

/*****************************************************************************
 *  Emit a call to a helper function.