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

Code before:
unsigned emitter::instrDesc::idCodeSize() const
{
#ifdef TARGET_WASM32
    static const unsigned PADDED_RELOC_SIZE = 5;
#else
#error WASM64
#endif

    // Currently, all our instructions have 1 byte opcode.
    unsigned size = 1;
    assert(FitsIn<uint8_t>(GetInsOpcode(idIns())));
    switch (idInsFmt())
    {
        case IF_OPCODE:
            break;
        case IF_BLOCK:
            size += 1;
            break;
        case IF_RAW_ULEB128:
            assert(!idIsCnsReloc());
            size = SizeOfULEB128(emitGetInsSC(this));
            break;
        case IF_LOCAL_DECL:
        {
            assert(idIsLclVarDecl());
            instrDescLclVarDecl* idl      = static_cast<instrDescLclVarDecl*>(const_cast<instrDesc*>(this));
            uint8_t              typeCode = GetWasmValueTypeCode(idl->lclType);
            size                          = SizeOfULEB128(idl->lclCnt) + sizeof(typeCode);
            break;
        }
        case IF_ULEB128:
            size += idIsCnsReloc() ? PADDED_RELOC_SIZE : SizeOfULEB128(emitGetInsSC(this));
            break;
        case IF_SLEB128:
            size += idIsCnsReloc() ? PADDED_RELOC_SIZE : SizeOfSLEB128(emitGetInsSC(this));
            break;
        case IF_F32:
            size += 4;
            break;
        case IF_F64:
            size += 8;
            break;
        case IF_MEMARG:
        {
            uint64_t align = emitGetAlignHintLog2(this);
            assert(align < 64); // spec says align > 2^6 produces a memidx for multiple memories.
            size += SizeOfULEB128(align);
            size += idIsCnsReloc() ? PADDED_RELOC_SIZE : SizeOfULEB128(emitGetInsSC(this));
            break;
        }
        default:

Review comment (refactor):
```suggestion
            const instrDescLclVarDecl* idl = static_cast<const instrDescLclVarDecl*>(this);
```
Avoid the `const_cast`?

Or you could use the `emitGetLclVarDeclType/...` helpers you added here by making them take `const instrDesc*`.

Code after:
unsigned emitter::instrDesc::idCodeSize() const
{
#ifdef TARGET_WASM32
    static const unsigned PADDED_RELOC_SIZE = 5;
#else
#error WASM64
#endif

    // Currently, all our instructions have 1 byte opcode.
    unsigned size = 1;
    assert(FitsIn<uint8_t>(GetInsOpcode(idIns())));
    switch (idInsFmt())
    {
        case IF_OPCODE:
            break;
        case IF_BLOCK:
            size += 1;
            break;
        case IF_RAW_ULEB128:
            assert(!idIsCnsReloc());
            size = SizeOfULEB128(emitGetInsSC(this));
            break;
        case IF_LOCAL_DECL:
        {
            assert(idIsLclVarDecl());
            uint8_t typeCode = GetWasmValueTypeCode(emitGetLclVarDeclType(this));
            size             = SizeOfULEB128(emitGetLclVarDeclCount(this)) + sizeof(typeCode);
            break;
        }
        case IF_ULEB128:
            size += idIsCnsReloc() ? PADDED_RELOC_SIZE : SizeOfULEB128(emitGetInsSC(this));
            break;
        case IF_SLEB128:
            size += idIsCnsReloc() ? PADDED_RELOC_SIZE : SizeOfSLEB128(emitGetInsSC(this));
            break;
        case IF_F32:
            size += 4;
            break;
        case IF_F64:
            size += 8;
            break;
        case IF_MEMARG:
        {
            uint64_t align = emitGetAlignHintLog2(this);
            assert(align < 64); // spec says align > 2^6 produces a memidx for multiple memories.
            size += SizeOfULEB128(align);
            size += idIsCnsReloc() ? PADDED_RELOC_SIZE : SizeOfULEB128(emitGetInsSC(this));
            break;
        }
        default:
            unreached();