Thread: What would be the compiler generation code?

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

    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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    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,318
    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
    344
    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, 11: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, 09:04 AM
  4. Code auto generation tools?
    By xuftugulus in forum C Programming
    Replies: 3
    Last Post: 02-18-2008, 04:00 PM
  5. Bad code or bad compiler?
    By musayume in forum C Programming
    Replies: 3
    Last Post: 10-22-2001, 09:08 PM