Repository: dotnet/runtime
File: src/coreclr/vm/interpexec.cpp (C++)

Code before:
                    returnOffset = ip[1];
                    callArgsOffset = ip[2];
                    methodSlot = ip[3];

                    MethodDesc *pMD = (MethodDesc*)pMethod->pDataItems[methodSlot];

                    OBJECTREF *pThisArg = LOCAL_VAR_ADDR(callArgsOffset, OBJECTREF);
                    NULL_CHECK(*pThisArg);

                    // Interpreter-TODO
                    // This needs to be optimized, not operating at MethodDesc level, rather with ftnptr
                    // slots containing the interpreter IR pointer
                    targetMethod = pMD->GetMethodDescOfVirtualizedCode(pThisArg, pMD->GetMethodTable());
                    ip += 4;
                    goto CALL_INTERP_METHOD;
                }

                case INTOP_CALLI_TAIL:
                case INTOP_CALLI:
                {
                    isTailcall = (*ip == INTOP_CALLI_TAIL);
                    returnOffset = ip[1];
                    callArgsOffset = ip[2];
                    int32_t calliFunctionPointerVar = ip[3];
                    int32_t calliCookie = ip[4];
                    bool isPInvoke = ip[5] != 0;

                    CallStubHeader *pCallStub = (CallStubHeader*)pMethod->pDataItems[calliCookie];
                    ip += 6;

                    // Save current execution state for when we return from called method
                    pFrame->ip = ip;

                    // Interpreter-FIXME: isTailcall
                    if (isPInvoke)
                    {
                        InvokePInvokeCalliStub(LOCAL_VAR(calliFunctionPointerVar, PCODE), pCallStub, stack, pFrame, stack + callArgsOffset, stack + returnOffset);
                    }
                    else
                    {
                        InvokeCalliStub(LOCAL_VAR(calliFunctionPointerVar, PCODE), pCallStub, stack + callArgsOffset, stack + returnOffset);
                    }

                    break;
                }

                case INTOP_CALL_PINVOKE:
                {
                    // This opcode handles p/invokes that don't use a managed wrapper for marshaling. These
                    //  calls are special in that they need an InlinedCallFrame in order for proper EH to happen


Review comment (suggestion):
The magic number 5 for the isPInvoke parameter index and the increment of 6 for the instruction pointer make the code harder to maintain. Consider defining named constants for these values to improve readability.

Code after:
                }

                case INTOP_CALL_HELPER_P_PS:
                {
                    void* helperArg1 = pMethod->pDataItems[ip[4]];
                    void* helperArg2 = LOCAL_VAR(ip[2], void*);

                    MethodDesc *pILTargetMethod = NULL;
                    HELPER_FTN_P_PP helperFtn = GetPossiblyIndirectHelper<HELPER_FTN_P_PP>(pMethod, ip[3], &pILTargetMethod);
                    if (pILTargetMethod != NULL)
                    {
                        returnOffset = ip[1];
                        int stackOffset = pMethod->allocaSize;
                        callArgsOffset = stackOffset;

                        // Pass arguments to the target method
                        LOCAL_VAR(stackOffset, void*) = helperArg1;
                        LOCAL_VAR(stackOffset + INTERP_STACK_SLOT_SIZE, void*) = helperArg2;

                        targetMethod = pILTargetMethod;
                        ip += 5;
                        goto CALL_INTERP_METHOD;
                    }

                    _ASSERTE(helperFtn != NULL);
                    LOCAL_VAR(ip[1], void*) = helperFtn(helperArg1, helperArg2);
                    ip += 5;
                    break;
                }

                case INTOP_CALL_HELPER_P_SP:
                {
                    HELPER_FTN_P_PP helperFtn = GetPossiblyIndirectHelper<HELPER_FTN_P_PP>(pMethod, ip[3]);
                    void* helperArg = pMethod->pDataItems[ip[4]];

                    LOCAL_VAR(ip[1], void*) = helperFtn(LOCAL_VAR(ip[2], void*), helperArg);
                    ip += 5;
                    break;
                }

                case INTOP_CALL_HELPER_P_G:
                {
                    InterpGenericLookup *pLookup = (InterpGenericLookup*)&pMethod->pDataItems[ip[4]];
                    void* helperArg = DoGenericLookup(LOCAL_VAR(ip[2], void*), pLookup);

                    MethodDesc *pILTargetMethod = NULL;
                    HELPER_FTN_P_P helperFtn = GetPossiblyIndirectHelper<HELPER_FTN_P_P>(pMethod, ip[3], &pILTargetMethod);
                    if (pILTargetMethod != NULL)
                    {
                        returnOffset = ip[1];
                        int stackOffset = pMethod->allocaSize;