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

Code before:
//   The managed calling convention for Wasm passes the stack pointer as the first arg.
//
void Compiler::lvaInitWasmStackPtrArg(unsigned* curVarNum)
{
    LclVarDsc* varDsc = lvaGetDesc(*curVarNum);
    varDsc->lvType    = TYP_I_IMPL;
    varDsc->lvIsParam = 1;
    varDsc->lvOnFrame = true;
    lvaWasmSpArg      = *curVarNum;
    (*curVarNum)++;
}

//-----------------------------------------------------------------------------
// lvaInitWasmStackPtr: set up the wasm stack pointer variable
//
// Allocates the stack pointer for methods where it is not an argument (RPI).
//
void Compiler::lvaAllocWasmStackPtr()
{
    if (lvaWasmSpArg == BAD_VAR_NUM)
    {
        lvaWasmSpArg                     = lvaGrabTemp(false DEBUGARG("SP"));
        lvaGetDesc(lvaWasmSpArg)->lvType = TYP_I_IMPL;
    }
}

//-----------------------------------------------------------------------------
// lvaInitWasmPortableEntryPtr: set up the wasm portable entry pointer argument
//
// Arguments:
//   curVarNum - [in, out] the last used local var num
//
// Notes:
//   The managed calling convention for Wasm passes a pointer to the portable entry point as the last arg.
//   This arg is currently unused in the JIT, and we may not need to model it.
//
void Compiler::lvaInitWasmPortableEntryPtr(unsigned* curVarNum)
{
    LclVarDsc* varDsc = lvaGetDesc(*curVarNum);
    varDsc->lvType    = TYP_I_IMPL;
    varDsc->lvIsParam = 1;
    varDsc->lvOnFrame = true;
    (*curVarNum)++;
}

#endif // defined(TARGET_WASM)

//-----------------------------------------------------------------------------
// lvaInitUserArgs:
//     Initialize local var descriptions for incoming user arguments
//

Review comment (bug):
`lvaAllocWasmStackPtr` now does `lvaGrabTemp(...)` followed by manually setting `lvImplicitlyReferenced`. Since there is an existing helper `lvaGrabTempWithImplicitUse(...)` (which encapsulates this pattern and is used elsewhere), consider using it here to avoid duplicating the implicit-use bookkeeping and reduce the chance of future drift.
```suggestion
        lvaWasmSpArg      = lvaGrabTempWithImplicitUse(false DEBUGARG("SP"));
        LclVarDsc* varDsc = lvaGetDesc(lvaWasmSpArg);
        varDsc->lvType    = TYP_I_IMPL;
```

Code after:
//   The managed calling convention for Wasm passes the stack pointer as the first arg.
//
void Compiler::lvaInitWasmStackPtrArg(unsigned* curVarNum)
{
    LclVarDsc* varDsc              = lvaGetDesc(*curVarNum);
    varDsc->lvType                 = TYP_I_IMPL;
    varDsc->lvIsParam              = 1;
    varDsc->lvOnFrame              = true;
    varDsc->lvImplicitlyReferenced = 1;
    lvaWasmSpArg                   = *curVarNum;
    (*curVarNum)++;
}

//-----------------------------------------------------------------------------
// lvaInitWasmStackPtr: set up the wasm stack pointer variable
//
// Allocates the stack pointer for methods where it is not an argument (RPI).
//
void Compiler::lvaAllocWasmStackPtr()
{
    if (lvaWasmSpArg == BAD_VAR_NUM)
    {
        lvaWasmSpArg                   = lvaGrabTemp(false DEBUGARG("SP"));
        LclVarDsc* varDsc              = lvaGetDesc(lvaWasmSpArg);
        varDsc->lvType                 = TYP_I_IMPL;
        varDsc->lvImplicitlyReferenced = 1;
    }
}

//-----------------------------------------------------------------------------
// lvaInitWasmPortableEntryPtr: set up the wasm portable entry pointer argument
//
// Arguments:
//   curVarNum - [in, out] the last used local var num
//
// Notes:
//   The managed calling convention for Wasm passes a pointer to the portable entry point as the last arg.
//   This arg is currently unused in the JIT, and we may not need to model it.
//
void Compiler::lvaInitWasmPortableEntryPtr(unsigned* curVarNum)
{
    LclVarDsc* varDsc = lvaGetDesc(*curVarNum);
    varDsc->lvType    = TYP_I_IMPL;
    varDsc->lvIsParam = 1;
    varDsc->lvOnFrame = true;
    (*curVarNum)++;
}

#endif // defined(TARGET_WASM)

//-----------------------------------------------------------------------------