Theoretical question: Duff's device and AST


I've been following clang with interest and will continue to do so
since the potential payoffs are immense, the project is just plain
cool and it's very likely to be finished with Apple backing it.

The other day, a question started nagging me: How will clang represent
weirdness like Duff's device[1] in AST form?

[1]: Duff's device - Wikipedia

As you may know, Duff's device is a famous example of a partially
unrolled loop to copy a buffer, which works by interlacing the switch
and do structures, like:

    int n = (count + 7) / 8;
    switch (count % 8) {
    case 0: do { *to = *from++;
    case 7: *to = *from++;
    case 6: *to = *from++;
    case 5: *to = *from++;
    case 4: *to = *from++;
    case 3: *to = *from++;
    case 2: *to = *from++;
    case 1: *to = *from++;
               } while (--n > 0);

Obviously, with memcpy, no one really needs to use it, but it is
nonetheless part of the C language that clang endeavors to cover, and
may be part of programs that clang will be asked to compile or
pretty-print. I'm interested purely academically in what the answer
might be.


clang is well past the point of being able to compile something as
simple as Duff's device. If you're interested in seeing what the AST
looks like for such construct, build a copy
( and run clang -ast-dump over
the code.


Why would you think that, with memcpy, you would have no need for duff's device?
As you can see from the code you posted, the 'to' pointer is never changed. Normally one would use duff's device with memory mapped IO, not with memory-to-memory tranfers :slight_smile:

   - Filipe Cabecinhas

I suppose a variant of it might be useful for std::copy_n on iterators
in C++, as well...