| | 712 | -- Copying byte arrays |
| | 713 | |
| | 714 | -- | Takes a source 'ByteArray#', an offset in the source array, a |
| | 715 | -- destination 'MutableByteArray#', an offset into the destination |
| | 716 | -- array, and the number of bytes to copy. Copies the given number of |
| | 717 | -- bytes from the source array to the destination array. |
| | 718 | doCopyByteArrayOp :: CmmExpr -> CmmExpr -> CmmExpr -> CmmExpr -> CmmExpr |
| | 719 | -> FCode () |
| | 720 | doCopyByteArrayOp = emitCopyByteArray copy |
| | 721 | where |
| | 722 | -- Copy data (we assume the arrays aren't overlapping since |
| | 723 | -- they're of different types) |
| | 724 | copy _src _dst dst_p src_p bytes = |
| | 725 | emitMemcpyCall dst_p src_p bytes (CmmLit (mkIntCLit 1)) |
| | 726 | |
| | 727 | -- | Takes a source 'MutableByteArray#', an offset in the source |
| | 728 | -- array, a destination 'MutableByteArray#', an offset into the |
| | 729 | -- destination array, and the number of bytes to copy. Copies the |
| | 730 | -- given number of bytes from the source array to the destination |
| | 731 | -- array. |
| | 732 | doCopyMutableByteArrayOp :: CmmExpr -> CmmExpr -> CmmExpr -> CmmExpr -> CmmExpr |
| | 733 | -> FCode () |
| | 734 | doCopyMutableByteArrayOp = emitCopyByteArray copy |
| | 735 | where |
| | 736 | -- The only time the memory might overlap is when the two arrays |
| | 737 | -- we were provided are the same array! |
| | 738 | -- TODO: Optimize branch for common case of no aliasing. |
| | 739 | copy src dst dst_p src_p bytes = do |
| | 740 | [moveCall, cpyCall] <- forkAlts [ |
| | 741 | getCode $ emitMemmoveCall dst_p src_p bytes (CmmLit (mkIntCLit 1)), |
| | 742 | getCode $ emitMemcpyCall dst_p src_p bytes (CmmLit (mkIntCLit 1)) |
| | 743 | ] |
| | 744 | emit $ mkCmmIfThenElse (cmmEqWord src dst) moveCall cpyCall |
| | 745 | |
| | 746 | emitCopyByteArray :: (CmmExpr -> CmmExpr -> CmmExpr -> CmmExpr -> CmmExpr |
| | 747 | -> FCode ()) |
| | 748 | -> CmmExpr -> CmmExpr -> CmmExpr -> CmmExpr -> CmmExpr |
| | 749 | -> FCode () |
| | 750 | emitCopyByteArray copy src0 src_off0 dst0 dst_off0 n0 = do |
| | 751 | -- Passed as arguments (be careful) |
| | 752 | src <- assignTempE src0 |
| | 753 | src_off <- assignTempE src_off0 |
| | 754 | dst <- assignTempE dst0 |
| | 755 | dst_off <- assignTempE dst_off0 |
| | 756 | n <- assignTempE n0 |
| | 757 | |
| | 758 | dst_p <- assignTempE $ cmmOffsetExpr (cmmOffsetB dst arrWordsHdrSize) dst_off |
| | 759 | src_p <- assignTempE $ cmmOffsetExpr (cmmOffsetB src arrWordsHdrSize) src_off |
| | 760 | |
| | 761 | copy src dst dst_p src_p n |
| | 762 | |
| | 763 | -- ---------------------------------------------------------------------------- |