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

Code before:
            GetEmitter()->emitIns_I(ins_Store(TYP_I_IMPL), EA_PTRSIZE, 0);

            // Leave the base address of the allocated region on the stack.
            //
            GetEmitter()->emitIns_I(INS_local_get, EA_PTRSIZE, WasmRegToIndex(GetStackPointerReg()));
            GetEmitter()->emitIns_I(INS_I_const, EA_PTRSIZE, reservedSpace);
            GetEmitter()->emitIns(INS_I_add);
        }
    }
    else
    {
        bool const is64Bit = (TARGET_POINTER_SIZE == 8);
        genConsumeReg(size);

        // Extend size to pointer size, if necessary
        if (genTypeSize(size->TypeGet()) < TARGET_POINTER_SIZE)
        {
            assert(TARGET_POINTER_SIZE == 8);
            GetEmitter()->emitIns(INS_i64_extend_u_i32);
        }

        // Fetch the internal register we reserved during RA
        InternalRegs* regs = internalRegisters.GetAll(tree);
        assert(regs->Count() == 1);
        regNumber sizeReg = regs->Extract();
        assert(WasmRegToType(sizeReg) == TypeToWasmValueType(TYP_I_IMPL));

        // Check for zero-sized requests
        GetEmitter()->emitIns(INS_I_eqz);
        GetEmitter()->emitIns_Block_Ty(INS_if, is64Bit ? WasmValueType::I64 : WasmValueType::I32);
        {
            // If size is zero, leave a zero on the stack
            GetEmitter()->emitIns_I(INS_I_const, EA_PTRSIZE, 0);
        }
        // Else ... nonzero size case.
        GetEmitter()->emitIns(INS_else);
        {
            // Prepare to subtract from SP
            GetEmitter()->emitIns_I(INS_local_get, EA_PTRSIZE, WasmRegToIndex(GetStackPointerReg()));

            // Add reserved space and round up request size to a multiple of STACK_ALIGN
            GetEmitter()->emitIns_I(INS_local_get, EA_PTRSIZE, WasmRegToIndex(sizeReg));
            GetEmitter()->emitIns_I(INS_I_const, EA_PTRSIZE, reservedSpace + STACK_ALIGN - 1);
            GetEmitter()->emitIns(INS_I_add);
            GetEmitter()->emitIns_I(INS_I_const, EA_PTRSIZE, ~cnsval_ssize_t(STACK_ALIGN - 1));
            GetEmitter()->emitIns(INS_I_and);

            // Save the aligned size for the zeroing call below if needed.
            if (needsZeroing)
            {
                GetEmitter()->emitIns_I(INS_local_tee, EA_PTRSIZE, WasmRegToIndex(sizeReg));

Review comment (suggestion):
I am confused. Shouldn't we be initializing `sizeReg` on some path here? `eqz` consumes the size value from the stack.

Code after:
            GetEmitter()->emitIns_I(ins_Store(TYP_I_IMPL), EA_PTRSIZE, 0);

            // Leave the base address of the allocated region on the stack.
            //
            GetEmitter()->emitIns_I(INS_local_get, EA_PTRSIZE, WasmRegToIndex(GetStackPointerReg()));
            GetEmitter()->emitIns_I(INS_I_const, EA_PTRSIZE, reservedSpace);
            GetEmitter()->emitIns(INS_I_add);
        }
    }
    else
    {
        bool const is64Bit = (TARGET_POINTER_SIZE == 8);
        genConsumeReg(size);

        // Extend size to pointer size, if necessary
        if (genTypeSize(genActualType(size->TypeGet())) < TARGET_POINTER_SIZE)
        {
            assert(TARGET_POINTER_SIZE == 8);
            GetEmitter()->emitIns(INS_i64_extend_u_i32);
        }

        // Fetch the internal register we reserved during RA
        InternalRegs* regs = internalRegisters.GetAll(tree);
        assert(regs->Count() == 1);
        regNumber sizeReg = regs->Extract();
        assert(WasmRegToType(sizeReg) == TypeToWasmValueType(TYP_I_IMPL));

        // Save the size
        GetEmitter()->emitIns_I(INS_local_tee, EA_PTRSIZE, WasmRegToIndex(sizeReg));

        // Check for zero-sized requests
        GetEmitter()->emitIns(INS_I_eqz);
        GetEmitter()->emitIns_BlockTy(INS_if, is64Bit ? WasmValueType::I64 : WasmValueType::I32);
        {
            // If size is zero, leave a zero on the stack
            GetEmitter()->emitIns_I(INS_I_const, EA_PTRSIZE, 0);
        }
        // Else ... nonzero size case.
        GetEmitter()->emitIns(INS_else);
        {
            // Prepare to subtract from SP
            GetEmitter()->emitIns_I(INS_local_get, EA_PTRSIZE, WasmRegToIndex(GetStackPointerReg()));

            // Add reserved space and round up request size to a multiple of STACK_ALIGN
            GetEmitter()->emitIns_I(INS_local_get, EA_PTRSIZE, WasmRegToIndex(sizeReg));
            GetEmitter()->emitIns_I(INS_I_const, EA_PTRSIZE, reservedSpace + STACK_ALIGN - 1);
            GetEmitter()->emitIns(INS_I_add);
            GetEmitter()->emitIns_I(INS_I_const, EA_PTRSIZE, ~cnsval_ssize_t(STACK_ALIGN - 1));
            GetEmitter()->emitIns(INS_I_and);

            // Save the aligned size for the zeroing call below if needed.