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

Code before:
    static_assert(sizeof(cnsval_ssize_t) >= sizeof(double));

    GenTreeIntConCommon* icon = nullptr;
    if ((type == TYP_INT) || (type == TYP_LONG))
    {
        icon = treeNode->AsIntConCommon();
        if (icon->ImmedValNeedsReloc(compiler))
        {
            // WASM-TODO: Generate reloc for this handle; 64-bit support
            ins  = INS_i32_const;
            bits = 0;
        }
        else
        {
            bits = icon->IntegralValue();
        }
    }

    if (ins == INS_unreachable)
    {
        switch (type)
        {
            case TYP_INT:
            {
                ins = INS_i32_const;
                assert(((INT64)(INT32)bits) == bits);
                break;
            }
            case TYP_LONG:
            {
                ins = INS_i64_const;
                break;
            }
            case TYP_FLOAT:
            {
                ins                  = INS_f32_const;
                GenTreeDblCon* con   = treeNode->AsDblCon();
                double         value = con->DconValue();
                memcpy(&bits, &value, sizeof(double));
                break;
            }
            case TYP_DOUBLE:
            {
                ins                  = INS_f64_const;
                GenTreeDblCon* con   = treeNode->AsDblCon();
                double         value = con->DconValue();
                memcpy(&bits, &value, sizeof(double));
                break;
            }
            default:
                unreached();

Review comment (suggestion):
This assertion checks that an INT64 value can fit in INT32 range when the type is TYP_INT. However, this assertion is only valid if the 'bits' value has already been set from icon->IntegralValue() for TYP_INT. If we entered this switch for TYP_INT after handling relocation (lines 1032-1036 where bits is set to 0), this assertion becomes meaningless. Consider restructuring to ensure the assertion is only performed when bits contains the actual integral value.

Code after:
    static_assert(sizeof(cnsval_ssize_t) >= sizeof(double));

    GenTreeIntConCommon* icon = nullptr;
    if ((type == TYP_INT) || (type == TYP_LONG))
    {
        icon = treeNode->AsIntConCommon();
        if (icon->ImmedValNeedsReloc(compiler))
        {
            // WASM-TODO: Generate reloc for this handle
            ins  = INS_I_const;
            bits = 0;
        }
        else
        {
            bits = icon->IntegralValue();
        }
    }

    if (ins == INS_none)
    {
        switch (type)
        {
            case TYP_INT:
            {
                ins = INS_i32_const;
                assert(FitsIn<INT32>(bits));
                break;
            }
            case TYP_LONG:
            {
                ins = INS_i64_const;
                break;
            }
            case TYP_FLOAT:
            {
                ins                  = INS_f32_const;
                GenTreeDblCon* con   = treeNode->AsDblCon();
                double         value = con->DconValue();
                memcpy(&bits, &value, sizeof(double));
                break;
            }
            case TYP_DOUBLE:
            {
                ins                  = INS_f64_const;
                GenTreeDblCon* con   = treeNode->AsDblCon();
                double         value = con->DconValue();
                memcpy(&bits, &value, sizeof(double));
                break;
            }
            default:
                unreached();