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

Code before:
    id->idLclCnt(localCount);
    id->idLclType(type);

    return id;
}

//-----------------------------------------------------------------------------------
// emitIns_I: Emit an instruction with an immediate operand and an encoded value type
//
void emitter::emitIns_I_Ty(instruction ins, cnsval_ssize_t imm, emitter::instWasmValueType valType)
{
    instrDesc* id  = this->emitNewInstrLclVarDecl(EA_8BYTE, imm, valType);
    insFormat  fmt = this->emitInsFormat(ins);

    id->idIns(ins);
    id->idInsFmt(fmt);

    this->dispIns(id);
    this->appendToCurIG(id);
}

emitter::instWasmValueType emitter::emitGetLclVarDeclType(instrDesc* id)
{
    if (!id->idIsLclVarDecl())
    {
        noway_assert("not a LclVarDecl instrDesc");
    }

    return static_cast<instrDescLclVarDecl*>(id)->lclType;
}

cnsval_ssize_t emitter::emitGetLclVarDeclCount(instrDesc* id)
{
    if (!id->idIsLclVarDecl())
    {
        noway_assert("not a LclVarDecl instrDesc");
    }

    return static_cast<instrDescLclVarDecl*>(id)->lclCnt;
}

emitter::insFormat emitter::emitInsFormat(instruction ins)
{
    static_assert(IF_COUNT < 255);

    const static uint8_t insFormats[] = {
#define INST(id, nm, info, fmt, opcode) fmt,
#include "instrs.h"
    };

    assert(ins < ArrLen(insFormats));

Review comment (bug):
The noway_assert macro is being used incorrectly. It should be called with a condition, not a string message. Use noway_assert(false) or unreached() instead.

Code after:
// Arguments:
//   attr        - emit attributes
//   localCount  - the count of locals in this declaration
//   type        - the type of local in the declaration
//   lclOffset   - used to provide the starting index of this local
//
// Notes:
//   `lclOffset` is stored as debug info attached to the instruction,
//    so the offset will only be used if m_debugInfoSize > 0
emitter::instrDesc* emitter::emitNewInstrLclVarDecl(emitAttr      attr,
                                                    unsigned int  localCount,
                                                    WasmValueType type,
                                                    int           lclOffset)
{
    instrDescLclVarDecl* id = static_cast<instrDescLclVarDecl*>(emitAllocAnyInstr(sizeof(instrDescLclVarDecl), attr));
    id->idLclCnt(localCount);
    id->idLclType(type);

    if (m_debugInfoSize > 0)
    {
        id->idDebugOnlyInfo()->lclOffset = lclOffset;
    }

    return id;
}

//-----------------------------------------------------------------------------------
// emitIns_I_Ty: Emit an instruction for a local variable declaration, encoding both
// a count (immediate) and a value type. This is specifically used for local variable
// declarations that require both the number of locals and their type to be encoded.
//
// Arguments:
//   ins      - instruction to emit
//   imm      - immediate value (local count)
//   valType  - value type of the local variable
//   offs     - local variable offset (= count of preceding locals) for debug info
void emitter::emitIns_I_Ty(instruction ins, unsigned int imm, WasmValueType valType, int offs)
{
    instrDesc* id  = emitNewInstrLclVarDecl(EA_8BYTE, imm, valType, offs);
    insFormat  fmt = emitInsFormat(ins);

    id->idIns(ins);
    id->idInsFmt(fmt);

    dispIns(id);
    appendToCurIG(id);
}

WasmValueType emitter::emitGetLclVarDeclType(const instrDesc* id)
{
    assert(id->idIsLclVarDecl());