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

Code before:
            dst += emitOutputByte(dst, opcode);
            cnsval_ssize_t constant = emitGetInsSC(id);
            dst += emitOutputSLEB128(dst, (int64_t)constant);
            break;
        }
        case IF_F32:
        {
            dst += emitOutputByte(dst, opcode);
            // Reinterpret the bits as a double constant and then truncate it to f32,
            //  then finally copy the raw truncated f32 bits to the output.
            cnsval_ssize_t bits = emitGetInsSC(id);
            double         value;
            float          truncated;
            memcpy(&value, &bits, sizeof(double));
            truncated = FloatingPointUtils::convertToSingle(value);
            memcpy(dst + writeableOffset, &truncated, sizeof(float));
            dst += sizeof(float);
            break;
        }
        case IF_F64:
        {
            dst += emitOutputByte(dst, opcode);
            // The int64 bits are actually a double constant we can copy directly
            //  to the output stream.
            cnsval_ssize_t bits = emitGetInsSC(id);
            memcpy(dst + writeableOffset, &bits, sizeof(cnsval_ssize_t));
            dst += sizeof(cnsval_ssize_t);
            break;
        }
        case IF_LABEL:
            NYI_WASM("emitOutputInstr IF_LABEL");
            break;
        case IF_MEMARG:
        {
            dst += emitOutputByte(dst, opcode);
            uint64_t align  = 0; // FIXME
            uint64_t offset = emitGetInsSC(id);
            assert(align <= UINT32_MAX); // spec says memarg alignment is u32
            assert(align < 64);          // spec says align > 2^6 produces a memidx for multiple memories.
            dst += emitOutputULEB128(dst, align);
            dst += emitOutputULEB128(dst, offset);
            break;
        }
        default:
            NYI_WASM("emitOutputInstr");
            break;
    }

#ifdef DEBUG
    bool dspOffs = emitComp->opts.dspGCtbls;
    if (emitComp->opts.disAsm || emitComp->verbose)

Review comment (refactor):
Other emitters largely avoid using `writeableOffset` and go through the abstractions of `emitOutputByte/Etc`. I think that's a good precedent to follow, and we can do that here by introducing e. g. `emitOutputInt64` (in common code, besides the other similar methods) that'd write the 64 bit value.

Code after:
        {
            dst += emitOutputByte(dst, opcode);
            cnsval_ssize_t constant = emitGetInsSC(id);
            dst += emitOutputULEB128(dst, (uint64_t)constant);
            break;
        }
        case IF_SLEB128:
        {
            dst += emitOutputByte(dst, opcode);
            cnsval_ssize_t constant = emitGetInsSC(id);
            dst += emitOutputSLEB128(dst, (int64_t)constant);
            break;
        }
        case IF_F32:
        {
            dst += emitOutputByte(dst, opcode);
            // Reinterpret the bits as a double constant and then truncate it to f32,
            //  then finally copy the raw truncated f32 bits to the output.
            cnsval_ssize_t bits = emitGetInsSC(id);
            double         value;
            float          truncated;
            memcpy(&value, &bits, sizeof(double));
            truncated = FloatingPointUtils::convertToSingle(value);
            dst += emitRawBytes(dst, &truncated, sizeof(float));
            break;
        }
        case IF_F64:
        {
            dst += emitOutputByte(dst, opcode);
            // The int64 bits are actually a double constant we can copy directly
            //  to the output stream.
            cnsval_ssize_t bits = emitGetInsSC(id);
            dst += emitRawBytes(dst, &bits, sizeof(cnsval_ssize_t));
            break;
        }
        case IF_LABEL:
            NYI_WASM("emitOutputInstr IF_LABEL");
            break;
        case IF_MEMARG:
        {
            dst += emitOutputByte(dst, opcode);
            uint64_t align  = emitGetAlignHintLog2(id);
            uint64_t offset = emitGetInsSC(id);
            assert(align <= UINT32_MAX); // spec says memarg alignment is u32
            assert(align < 64);          // spec says align > 2^6 produces a memidx for multiple memories.
            dst += emitOutputULEB128(dst, align);
            dst += emitOutputULEB128(dst, offset);
            break;
        }
        default:
            NYI_WASM("emitOutputInstr");