how does duff's device work?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • yawnmoth

    how does duff's device work?

    dsend(to, from, count)
    char *to, *from;
    int count;
    {
    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);
    }
    }

    If count % 8 is 7, the switch statement would skip the 'do' keyword
    and the open bracket used in case 0, would go down the rest of the
    cases, and, upon getting to case 1, would find a close bracket without
    any corresponding open bracket, yielding a syntax error. As such, I'm
    a little confused how the code works. Maybe there's some quirk in
    some C compiler which interprets that differently? Maybe the ISO
    standards for the C language dictate that it do that? If the latter,
    it seems like Java or Python, or whatever, are liable not to support
    duff's device?
  • Phil Carmody

    #2
    Re: how does duff's device work?

    yawnmoth <terra1024@yaho o.comwrites:
    dsend(to, from, count)
    char *to, *from;
    int count;
    {
    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);
    }
    }
    >
    If count % 8 is 7, the switch statement would skip the 'do' keyword
    Are you mistaking C for a dumb interpreted language?
    and the open bracket used in case 0, would go down the rest of the
    cases, and, upon getting to case 1, would find a close bracket without
    any corresponding open bracket, yielding a syntax error. As such, I'm
    a little confused how the code works.
    Start with something simpler. Try this:

    int signum(int i)
    {
    if(!i) { goto foo; }
    if(i>0)
    {
    return 1;
    foo:
    return 0;
    }
    return -1;
    }


    Phil
    --
    I tried the Vista speech recognition by running the tutorial. I was
    amazed, it was awesome, recognised every word I said. Then I said the
    wrong word ... and it typed the right one. It was actually just
    detecting a sound and printing the expected word! -- pbhj on /.

    Comment

    • Richard Heathfield

      #3
      Re: how does duff's device work?

      yawnmoth said:
      dsend(to, from, count)
      char *to, *from;
      int count;
      {
      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);
      }
      }
      >
      If count % 8 is 7, the switch statement would skip the 'do' keyword
      and the open bracket used in case 0, would go down the rest of the
      cases, and, upon getting to case 1, would find a close bracket without
      any corresponding open bracket, yielding a syntax error.
      No. Syntax errors are a concept that is only relevant at the time of
      translation. By the time the program has been translated and (if
      necessary) linked, there is no way to get a syntax error.

      The translation will result in some way of expressing the above in a
      different language. For example, making up some (not terribly efficient)
      assembly language syntax, and starting at the switch:

      MOV R7, TO ; save the destination pointer in a register
      MOV R1, COUNT ; save the count in a register
      MOV R2, N ; save n in a register
      MOV R3, R1 ; copy count to a temporary place
      DIV R3, 8, R4 ; divide R3 by 8, storing the remainder in R4
      C0:
      CMP R4, 0 ; is count % 8 == 0?
      JNZ C7 ; no
      MOV R5, FROM ; yes, so save the source pointer in a register
      MOV R6, *R5 ; deref source ptr
      MOV *R7, R6 ; copy fetched value to destination
      C7:
      CMP R4, 7 ; is count % 8 == 7?
      JNZ C6 ; no
      MOV R5, FROM ; yes, so save the source pointer in a register
      MOV R6, *R5 ; deref source ptr
      MOV *R7, R6 ; copy fetched value to destination
      C6:

      ; etc (cases 6, 5, 4, 3, 2...)

      C1:
      MOV R5, FROM ; save source ptr in register
      MOV R6, *R5 ; deref source ptr
      MOV *R7, R6 ; copy fetched value to destination
      DEC R2 ; --n
      CMP R2, 0 ; now decide whether to jump back to case 0
      JNZ C0

      So the mystery lies only in the C source, where the above is probably only
      legal because nobody got around to forbidding it, but it is nevertheless
      legal. As you can see, it isn't quite so mysterious, once you've worked
      through how it would be translated into something a little more - um -
      linear.

      --
      Richard Heathfield <http://www.cpax.org.uk >
      Email: -http://www. +rjh@
      Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
      "Usenet is a strange place" - dmr 29 July 1999

      Comment

      • Richard Heathfield

        #4
        Re: how does duff's device work?

        Phil Carmody said:
        yawnmoth <terra1024@yaho o.comwrites:
        <snip>
        >If count % 8 is 7, the switch statement would skip the 'do' keyword
        >
        Are you mistaking C for a dumb interpreted language?
        A number of C interpreters exist. Whether a language is interpreted or
        compiled (or a mixture) is a function of the implementation, not the
        language.

        --
        Richard Heathfield <http://www.cpax.org.uk >
        Email: -http://www. +rjh@
        Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
        "Usenet is a strange place" - dmr 29 July 1999

        Comment

        • Richard Tobin

          #5
          Re: how does duff's device work?

          In article <TZ6dnW0lmYYo-YvUnZ2dnUVZ8s7i nZ2d@bt.com>,
          Richard Heathfield <rjh@see.sig.in validwrote:
          >Are you mistaking C for a dumb interpreted language?
          >A number of C interpreters exist. Whether a language is interpreted or
          >compiled (or a mixture) is a function of the implementation, not the
          >language.
          True of course, but I think the implication of "dumb interpretated
          language" was clear: one which can be interpreted from something very
          close to the text, rather than from a representation of the structure
          of the program.

          -- Richard
          --
          Please remember to mention me / in tapes you leave behind.

          Comment

          • James Harris

            #6
            Re: how does duff's device work?

            On 9 Nov, 00:38, yawnmoth <terra1...@yaho o.comwrote:
            dsend(to, from, count)
            char *to, *from;
            int count;
            {
            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);
            }
            >
            }
            >
            If count % 8 is 7, the switch statement would skip the 'do' keyword
            and the open bracket used in case 0, would go down the rest of the
            cases, and, upon getting to case 1, would find a close bracket without
            any corresponding open bracket, yielding a syntax error. As such, I'm
            a little confused how the code works. Maybe there's some quirk in
            some C compiler which interprets that differently? Maybe the ISO
            standards for the C language dictate that it do that? If the latter,
            it seems like Java or Python, or whatever, are liable not to support
            duff's device?
            Understandably you are looking at the code structure from the point of
            view of the switch statement. Yet switch is only partially structured.
            Its case labels can appear at strange places within the following
            code.

            Instead, mentally remove the switch and case construct and the
            trailing brace. You should be left with code that exhibits more
            recognisable structure. In particular the do ... while loop will look
            like a loop. It will also function as one.

            Then adding the switch and case construct allows you (as shown in
            Richard Heathfield's assembler translation post) to jump to arbitrary
            places within that loop. Using switch this way does have its uses but,
            IMHO, is generally to be avoided.

            If you've not already seen it take a look at




            --
            James

            Comment

            • Bartc

              #7
              Re: how does duff's device work?


              "James Harris" <james.harris.1 @googlemail.com wrote in message
              news:6b58995c-5aba-4410-bbe9-3270b5d97b0c@g1 7g2000prg.googl egroups.com...
              On 9 Nov, 00:38, yawnmoth <terra1...@yaho o.comwrote:
              >dsend(to, from, count)
              >char *to, *from;
              >int count;
              >{
              > int n = (count + 7) / 8;
              > switch (count % 8) {
              > case 0: do { *to = *from++;
              >I'm a little confused how the code works. Maybe there's some quirk in
              Understandably you are looking at the code structure from the point of
              view of the switch statement. Yet switch is only partially structured.
              Its case labels can appear at strange places within the following
              code.
              This is crazy. What happens when there is a nested switch statement, how
              does it know whether a particular 'case' belongs to the inner or outer
              switch?

              --
              Bartc

              Comment

              • Richard Tobin

                #8
                Re: how does duff's device work?

                In article <9YzRk.84942$E4 1.77039@text.ne ws.virginmedia. com>,
                Bartc <bc@freeuk.comw rote:
                >This is crazy. What happens when there is a nested switch statement, how
                >does it know whether a particular 'case' belongs to the inner or outer
                >switch?
                Cases in enclosed switch statements are not visible to the outer switch.
                Or to put it another way, cases are scoped by switch statements.

                Other compound statements are "transparen t" and do not introduce a
                new scope for cases.

                -- Richard

                --
                Please remember to mention me / in tapes you leave behind.

                Comment

                • Phil Carmody

                  #9
                  Re: how does duff's device work?

                  Richard Heathfield <rjh@see.sig.in validwrites:
                  Phil Carmody said:
                  >
                  >yawnmoth <terra1024@yaho o.comwrites:
                  >
                  <snip>
                  >
                  >>If count % 8 is 7, the switch statement would skip the 'do' keyword
                  >>
                  >Are you mistaking C for a dumb interpreted language?
                  >
                  A number of C interpreters exist. Whether a language is interpreted or
                  compiled (or a mixture) is a function of the implementation, not the
                  language.
                  Yes, I have and even use a C interpreter. However, it realises that
                  between the switch and the case it's interested in, there's a do {.
                  That makes it not dumb. I guess I should have used an adverb rather
                  than an adjective.

                  Phil
                  --
                  I tried the Vista speech recognition by running the tutorial. I was
                  amazed, it was awesome, recognised every word I said. Then I said the
                  wrong word ... and it typed the right one. It was actually just
                  detecting a sound and printing the expected word! -- pbhj on /.

                  Comment

                  • Phil Carmody

                    #10
                    Re: how does duff's device work?

                    richard@cogsci. ed.ac.uk (Richard Tobin) writes:
                    In article <TZ6dnW0lmYYo-YvUnZ2dnUVZ8s7i nZ2d@bt.com>,
                    Richard Heathfield <rjh@see.sig.in validwrote:
                    >
                    >>Are you mistaking C for a dumb interpreted language?
                    >
                    >>A number of C interpreters exist. Whether a language is interpreted or
                    >>compiled (or a mixture) is a function of the implementation, not the
                    >>language.
                    >
                    True of course, but I think the implication of "dumb interpretated
                    language" was clear: one which can be interpreted from something very
                    close to the text, rather than from a representation of the structure
                    of the program.
                    Yup, exactly. I posted at nearly 3am, he responded at half 4, I
                    guess this kind of misunderstandin g was probable.

                    Phil
                    --
                    I tried the Vista speech recognition by running the tutorial. I was
                    amazed, it was awesome, recognised every word I said. Then I said the
                    wrong word ... and it typed the right one. It was actually just
                    detecting a sound and printing the expected word! -- pbhj on /.

                    Comment

                    Working...