@@ -301,12 +301,27 @@ that were dropped after being copied into memory during module instantiation.
301
301
302
302
### ` memory.copy ` instruction
303
303
304
- Copy data from a source memory region to destination region; these regions may
305
- overlap: the copy is performed as if the source region was first copied to a
306
- temporary buffer, then the temporary buffer is copied to the destination
307
- region. This instruction has two immediate arguments: the source and
304
+ Copy data from a source memory region to destination region. The
305
+ regions are said to overlap if they are in the same memory and the
306
+ start address of one region is one of the addresses that's read or
307
+ written (by the copy operation) in the other region.
308
+
309
+ This instruction has two immediate arguments: the source and
308
310
destination memory indices. They currently both must be zero.
309
311
312
+ If the regions overlap, and the source region starts at a lower
313
+ address than the target region, then the copy takes place as if from
314
+ higher to lower addresses: the highest source address is read first
315
+ and the value is written to the highest target address, then the next
316
+ highest, and so on. Otherwise, the copy takes place as if from lower
317
+ to higher addresses: the lowest source address is read first and the
318
+ value is written to the lowest target address, then the next lowest,
319
+ and so on.
320
+
321
+ (The direction of the copy is defined in order to future-proof
322
+ ` memory.copy ` for shared memory and a memory read/write protection
323
+ feature.)
324
+
310
325
The instruction has the signature ` [i32 i32 i32] -> [] ` . The parameters are, in order:
311
326
312
327
- top-2: destination address
@@ -318,17 +333,14 @@ A trap occurs if:
318
333
* the source offset is outside the source memory
319
334
* the destination offset is outside the target memory
320
335
321
- All the data are read before any is written, and writing takes place
322
- bytewise from lower addresses toward higher addresses. A trap
323
- resulting from an access outside the source memory is thus signalled
324
- before any target bytes are affected. A trap resulting from an access
325
- outside the target memory only occurs once the first byte that is
326
- outside the target is reached. Bytes written before the trap stay
327
- written.
328
-
329
- TODO: While those semantics are efficient in the context of unshared
330
- memory, they are not efficient in the context of shared memory with
331
- mprotect, and need to be improved.
336
+ A trap resulting from an access outside the source or target region
337
+ only occurs once the first byte that is outside the source or target
338
+ is reached (in the defined copy order). Bytes written before the trap
339
+ stay written.
340
+
341
+ (Data are read and written as-if individual bytes were read and
342
+ written, but various optimizations are possible that avoid reading and
343
+ writing only individual bytes.)
332
344
333
345
### ` memory.fill ` instruction
334
346
0 commit comments