home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / arch / 8878 < prev    next >
Encoding:
Text File  |  1992-08-13  |  2.9 KB  |  79 lines

  1. Newsgroups: comp.arch
  2. Path: sparky!uunet!mcsun!Germany.EU.net!Urmel.Informatik.RWTH-Aachen.DE!martin
  3. From: martin@math.rwth-aachen.de (  Martin Schoenert)
  4. Subject: Re: Threaded Code
  5. Message-ID: <martin.713702167@bert>
  6. Sender: news@Urmel.Informatik.RWTH-Aachen.DE (Newsfiles Owner)
  7. Nntp-Posting-Host: bert.math.rwth-aachen.de
  8. Organization: Rechnerbetrieb Informatik  /  RWTH Aachen
  9. References: <GLEW.92Aug7185506@pdx007.intel.com> <1992Aug8.171834.8610@news.eng.convex.com> <1992Aug08.182610.6904@news.th-darmstadt.de> <martin.713456948@bert> <1992Aug10.185713.50527@news.th-darmstadt.de>
  10. Date: 13 Aug 92 10:36:07 GMT
  11. Lines: 66
  12.  
  13. Ulrich Graef writes:
  14.  
  15.     [about whether or not a compiler must generate a range check for
  16.      'switch ( unsigned-char-var ) { }']
  17.  
  18.     The problem is, a char value and an enumeration type value 
  19.     is internally used like an integer (see the function call).
  20.     That is the reason why I mentioned the extension to 2^16 or 2^32
  21.     addresses (the domain of an integer).
  22.  
  23.     I want to make clear the conseqeuences: At every position in the
  24.     statements a char value is converted to an integer. To generate code
  25.     without range check with sources like your example you must change this
  26.     for the switch statement.
  27.  
  28. Well the compiler must generate code that behaves as if the following
  29. happens for a switch:
  30.  
  31.     The switch expression is evaluated.
  32.     The value is converted to an integer.
  33.     The integer value is compared with the values of the case labels
  34.         (of which only one may compare equal)
  35.     If one value compares equal,
  36.         control is transferred to this label.
  37.     If no value compares equal and there is a default label,
  38.         control is transferred to the default label.
  39.     If no value compares equal and there is no default label,
  40.         control is transferred to the statement following the switch.
  41.  
  42. However the important thing is that the code only has to behave as if
  43. this happens.  That means that a compiler may use any information he can
  44. gather statically to implement a switch statement efficiently.  For
  45. example the following statement sequence
  46.  
  47.     i = 1;
  48.     switch ( i ) {
  49.     case 2: <do-something>;
  50.     case 3: <do-something-else>;
  51.     default i++;
  52.     } 
  53.  
  54. could well be implemented as
  55.  
  56.     load-immediate      $i,2
  57.  
  58. by a compiler (provided 'i' is not declared 'volatile').  No need for a
  59. conversion, a range check, or a jump table.
  60.  
  61. Likewise, for the following switch statement
  62.  
  63.     unsigned char       * pc;
  64.  
  65.     switch ( *++pc ) {
  66.     ...
  67.     }
  68.  
  69. the compiler can simply use a jump table with 256 entries.  There is no
  70. need for a range check because the compiler can detect that, even after
  71. the conversion, the value of the switch expression can only be one of 256
  72. possible values.
  73.  
  74. Martin.
  75.  
  76. --
  77. Martin Sch"onert,   Martin.Schoenert@Math.RWTH-Aachen.DE,  +49 241 804551
  78. Lehrstuhl D f"ur Mathematik, Templergraben 64, RWTH, D 51 Aachen, Germany
  79.