What would be the compiler generation code?

This is a discussion on What would be the compiler generation code? within the C Programming forums, part of the General Programming Boards category; Do compiler generates a if-elseif-elseif-else.. statements for normal switch cases(I mean, case labels are NOT in a narrow range) or ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Location
    dust
    Posts
    59

    What would be the compiler generation code?

    Do compiler generates a if-elseif-elseif-else.. statements for normal switch cases(I mean, case labels are NOT in a narrow range) or do it generates the Jump statement for switch cases???

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    I suppose it depends on many things.
    You would need to study the generated code from many compilers to get some kind of "consensus" view as to how they deal with a specific code example.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    I have seen if-the-else equivalents running through the cases if there are few. I have also seen indexing into an array of destination instruction addresses when the compiler detected a contiguous set of integer cases. Thirdly I have seen a compiler generate binary search code if the number of choices is huge and the case numbers are all over the place. It generated a sorted address table.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    They do all sorts of things including some things you've thought of and some things you haven't.
    They'll use whatever seems the best at the time, for the options they have available.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    343
    Quote Originally Posted by ArunS View Post
    Do compiler generates a if-elseif-elseif-else.. statements for normal switch cases(I mean, case labels are NOT in a narrow range) or do it generates the Jump statement for switch cases???
    Yes, or something else entirely.

    For an overview of how compilers deal with this, see the article on page 103 of http://www.gccsummit.org/2008/gcc-2008-proceedings.pdf. GCC implements something like http://ivoras.net/papers/switch-complexity.pdf.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. template code generation
    By underthesun in forum C++ Programming
    Replies: 8
    Last Post: 12-09-2009, 10:26 PM
  2. Error with Fibonacci code generation
    By xeon1989 in forum C++ Programming
    Replies: 5
    Last Post: 10-04-2009, 08:58 AM
  3. Three Address Code generation using Lex, Yacc
    By Raman in forum C Programming
    Replies: 1
    Last Post: 11-04-2008, 08:04 AM
  4. Code auto generation tools?
    By xuftugulus in forum C Programming
    Replies: 3
    Last Post: 02-18-2008, 03:00 PM
  5. Bad code or bad compiler?
    By musayume in forum C Programming
    Replies: 3
    Last Post: 10-22-2001, 09:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21