home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 357_01 / cstar1.exe / ARITH.DOC < prev    next >
Text File  |  1991-06-20  |  8KB  |  154 lines

  1. ARITH.DOC:  About arithmetic in the CSTAR translator
  2.  
  3. June 25, 1991
  4.  
  5. NOTE: stuff in brackets [ ] pertains to things that need to be done but
  6. aren't implemented yet.
  7.  
  8. Arithmetic involving only integers is strictly typed without automatic
  9. conversions.  Mixed-mode expressions must be written with casts; this
  10. is good practice anyhow, and since C types are slightly less restrictive
  11. than Pascal types, it is eminently feasible in all cases.  Note also that
  12. longer results will *not* be silently truncated and blithely stuffed
  13. shorter variables.
  14.  
  15. If you are writing too many casts, it may pay to use longer forms for
  16. some of your variables.  The 68000 moves words as easily as bytes,
  17. and the penalties for using long words are fairly modest except
  18. in multiplication or division.
  19.  
  20. In order to require an intermediate result longer than its result, a
  21. mixed-mode expression must contain a division or right shift of a
  22. quantity which is wider than the result, since those are the only
  23. operations in which bits can affect other bits to their right.  (In
  24. such cases, CSTAR may generate no code for the cast if it is inherent
  25. in the operator; the multiplication of two ints to get a long has to
  26. be written:
  27.  
  28.     long_var = (long)int_1 * (long)int_2;
  29.  
  30. but it would be silly to extend the integers and call the long
  31. multiply functions.)
  32.  
  33. Characters are treated as 8-bit integers and are never automatically
  34. extended except for use as function arguments.  A mixed char/int
  35. expression should be written exactly like its int/long analog in
  36. terms of form and of placement of casts.  In the absence of casts,
  37. all char results are 8 bits wide.  Thus:
  38.  
  39.     (int)(char_1 * char_2)
  40.  
  41. means multiply char_1 by char_2 and extend the 8-BIT result to 16 bits.
  42. This treatment minimizes the generation of silly code in trivial
  43. comparisons and range checks on characters, which, in most programs,
  44. is the only use of character arithmetic.
  45.  
  46. On the 68000, I recommend against using single char variables
  47. (aggregates of char are a different matter) to "save space"
  48. if they will appear in mixed-mode expressions, since the code to
  49. extend them will in all cases occupy more space than the one byte
  50. per variable so saved.
  51.  
  52. [In order to avoid emulating the speed of a Vic-20 with your target 68000
  53. system, FLOAT variables are NOT automatically extended to DOUBLE
  54. everywhere unless the code is being compiled for the 68881 or
  55. some such chip.]
  56.  
  57. Pointer arithmetic involves the addition of a pointer to an integer
  58. (of any size) or the subtraction of two pointers to yield an integer.
  59. In CSTAR, it takes advantage of the 68000 address modes under most
  60. circumstances, in order to get good code.
  61.  
  62. Pointer addition involves two operands of different types, so it is
  63. truly mixed mode.  In CSTAR, scaling is done to whatever width is
  64. necessary to correctly hold the result of that scaling.  In practice,
  65. most scaling produces a long result; the idiosyncrasies of the 68000
  66. A registers assist with that.  The result of scaling a char (i.e. an
  67. 8-bit integer) will be integer if the scale factor is small enough
  68. for that to be correct.
  69.  
  70. In a complex expression, the C-language operator which is spelled +
  71. is not associative, and this may lead to surprises.  It is not
  72. associative because it is polymorphic, and the form it takes--pointer
  73. result or integer result--depends on its operands and therefore
  74. on its associations.
  75.  
  76. The surprise is that in the expression:
  77.  
  78.     int_1 + int_2 + pointer;
  79.  
  80. the first addition is of the integer result form, and it could overflow
  81. and provide an incorrect operand to the pointer addition, while
  82.  
  83.     pointer + int_1 + int_2;
  84.  
  85. does two additions of the pointer result form, and the analogous overflow
  86. cannot happen.  (In both cases, of course, a 24-bit overflow out of the
  87. 68000 address space could occur.)  To repeat: when pointers are involved,
  88. addition is commutative but not associative.
  89.  
  90. In the case of array subscripts, the association of the effective additions
  91. is ambiguous.  That is,
  92.  
  93.     p [i + j]
  94.  
  95. could be treated as *(p + i + j) or as *(p + (i + j)).  In order to
  96. produce the most conservative results, CSTAR uses the first association.
  97.  
  98. A command-line switch is available to choose the second form instead.
  99. If it is necessary to rigorously control overflow behavior in an expression,                                                             
  100. (as to maintain a circular buffer in a fast interrupt-service routine
  101. by allowing the pointer to overflow to zero) the expression should be
  102. written out with * and +, and eschew [], in order to avoid any dependency
  103. on how subscript calculations are optimized.
  104.  
  105. [The DRI compiler is unpredictable and often sets up to do the first association--
  106. and then truncates the result after scaling!  If someone can tell us
  107. that p[i+j] "really means" one form or the other, we can change the
  108. default flag setting.]
  109.  
  110. The 68000 indexed addressing modes use a pointer and a signed, scaled
  111. integer or constant.  The surprise that results is that an unsigned
  112. word-size pointer has to be extended to long to use it correctly in
  113. an address mode or to add it correctly to an address register.  If
  114. this is a problem in critical code, we suggest you set up a
  115. distinctive macro that does an int cast, which generates no code
  116. itself, and will suppress the extension, and use it in critical code
  117. if you are *absolutely sure* the cast will not overflow.  (If you
  118. get that wrong, you will be stung by a nasty pointer bug.)
  119.  
  120. The constant in most addressing modes is limited to 32K, so absolute
  121. (global/external/static) variables cannot be accessed with those modes; nor
  122. can frame variables beyond 32K.  On those machines whose operating
  123. systems limit memory chunks to 32K or less anyhow, the latter is not a
  124. problem.  Such long-range accesses may take longer than short-frame
  125. accesses, and they may do actual addition that would not occur with an
  126. analogous short-frame access.
  127.  
  128. Before an integer is used in an address mode, it has to be scaled, unless
  129. the address is a pointer to char.  CSTAR does scaling by 2 by doubling in
  130. an A register if one is available, or a D register if the variable is
  131. already in one.  Scaling by other powers of 2 is by shifting, and other
  132. scaling is by multiplication.  Scaling a long variable involves a long
  133. multiplication; be aware that this takes all day (worst case: function
  134. call and return plus the three mul instructions and miscellany.)
  135.  
  136. When CSTAR needs an address, or an address has overgrown the complexity
  137. of a 68000 addressing mode, code is generated to do some sort of real
  138. addition with ADDA or LEA.  For reasonable expressions, this code is
  139. very close to what you would write in assembler, and occasionally,
  140. it is better when a trick shows up that you might not have noticed.
  141. The cases that are only "very close" involve 0(An, Rn), which
  142. under some circumstances can be replaced by rearranging the surrounding
  143. code; the result would run two clock cycles faster--and possibly be one
  144. word longer.  [Maybe there should be a peephole switch.]
  145.  
  146. [Not implemented yet]
  147.  
  148. In an associated chain of pointer additions (containing one pointer
  149. operand and two or more integer operands), CSTAR may, at its option,
  150. postpone the scaling operation and perform it just once.  The result will
  151. be rigorously guaranteed to be identical to what would have been
  152. obtained had the postponement not taken place.  If necessary, extending
  153. casts and/or A register addition will be generated.
  154.