[cfe-dev] Theoretical question: Duff's device and AST

Jesper wootest at gmail.com
Sat Jul 19 14:23:39 PDT 2008


Hello,

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]: http://en.wikipedia.org/wiki/Duff%27s_device

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.

/Jesper



More information about the cfe-dev mailing list