tcset.pas 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 by Florian Klaempfl
  4. Type checking and register allocation for set/case nodes
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit tcset;
  19. interface
  20. uses
  21. tree;
  22. procedure firstsetelement(var p : ptree);
  23. procedure firstin(var p : ptree);
  24. procedure firstrange(var p : ptree);
  25. procedure firstcase(var p : ptree);
  26. implementation
  27. uses
  28. cobjects,verbose,globals,systems,
  29. symtable,aasm,types,
  30. hcodegen,htypechk,pass_1
  31. {$ifdef i386}
  32. ,i386,tgeni386
  33. {$endif}
  34. {$ifdef m68k}
  35. ,m68k,tgen68k
  36. {$endif}
  37. ;
  38. {*****************************************************************************
  39. FirstSetElement
  40. *****************************************************************************}
  41. procedure firstsetelement(var p : ptree);
  42. begin
  43. firstpass(p^.left);
  44. if codegenerror then
  45. exit;
  46. if assigned(p^.right) then
  47. begin
  48. firstpass(p^.right);
  49. if codegenerror then
  50. exit;
  51. end;
  52. calcregisters(p,0,0,0);
  53. p^.resulttype:=p^.left^.resulttype;
  54. set_location(p^.location,p^.left^.location);
  55. end;
  56. {*****************************************************************************
  57. FirstIn
  58. *****************************************************************************}
  59. procedure firstin(var p : ptree);
  60. type
  61. byteset = set of byte;
  62. var
  63. t : ptree;
  64. begin
  65. p^.location.loc:=LOC_FLAGS;
  66. p^.resulttype:=booldef;
  67. firstpass(p^.right);
  68. if codegenerror then
  69. exit;
  70. if p^.right^.resulttype^.deftype<>setdef then
  71. CGMessage(sym_e_set_expected);
  72. firstpass(p^.left);
  73. if codegenerror then
  74. exit;
  75. p^.left:=gentypeconvnode(p^.left,psetdef(p^.right^.resulttype)^.setof);
  76. firstpass(p^.left);
  77. if codegenerror then
  78. exit;
  79. { constant evaulation }
  80. if (p^.left^.treetype=ordconstn) and (p^.right^.treetype=setconstn) then
  81. begin
  82. t:=genordinalconstnode(byte(p^.left^.value in byteset(p^.right^.value_set^)),booldef);
  83. disposetree(p);
  84. firstpass(t);
  85. p:=t;
  86. exit;
  87. end;
  88. left_right_max(p);
  89. { this is not allways true due to optimization }
  90. { but if we don't set this we get problems with optimizing self code }
  91. if psetdef(p^.right^.resulttype)^.settype<>smallset then
  92. procinfo.flags:=procinfo.flags or pi_do_call
  93. else
  94. begin
  95. { a smallset needs maybe an misc. register }
  96. if (p^.left^.treetype<>ordconstn) and
  97. not(p^.right^.location.loc in [LOC_CREGISTER,LOC_REGISTER]) and
  98. (p^.right^.registers32<1) then
  99. inc(p^.registers32);
  100. end;
  101. end;
  102. {*****************************************************************************
  103. FirstRange
  104. *****************************************************************************}
  105. procedure firstrange(var p : ptree);
  106. var
  107. ct : tconverttype;
  108. begin
  109. firstpass(p^.left);
  110. firstpass(p^.right);
  111. if codegenerror then
  112. exit;
  113. { both types must be compatible }
  114. if not(is_equal(p^.left^.resulttype,p^.right^.resulttype)) and
  115. not(isconvertable(p^.left^.resulttype,p^.right^.resulttype,ct,ordconstn,false)) then
  116. CGMessage(type_e_mismatch);
  117. { Check if only when its a constant set }
  118. if (p^.left^.treetype=ordconstn) and (p^.right^.treetype=ordconstn) then
  119. begin
  120. { upper limit must be greater or equal than lower limit }
  121. { not if u32bit }
  122. if (p^.left^.value>p^.right^.value) and
  123. (( p^.left^.value<0) or (p^.right^.value>=0)) then
  124. CGMessage(cg_e_upper_lower_than_lower);
  125. end;
  126. left_right_max(p);
  127. p^.resulttype:=p^.left^.resulttype;
  128. set_location(p^.location,p^.left^.location);
  129. end;
  130. {*****************************************************************************
  131. FirstCase
  132. *****************************************************************************}
  133. procedure firstcase(var p : ptree);
  134. var
  135. old_t_times : longint;
  136. hp : ptree;
  137. begin
  138. { evalutes the case expression }
  139. cleartempgen;
  140. must_be_valid:=true;
  141. firstpass(p^.left);
  142. if codegenerror then
  143. exit;
  144. p^.registers32:=p^.left^.registers32;
  145. p^.registersfpu:=p^.left^.registersfpu;
  146. {$ifdef SUPPORT_MMX}
  147. p^.registersmmx:=p^.left^.registersmmx;
  148. {$endif SUPPORT_MMX}
  149. { walk through all instructions }
  150. { estimates the repeat of each instruction }
  151. old_t_times:=t_times;
  152. if not(cs_littlesize in aktglobalswitches) then
  153. begin
  154. t_times:=t_times div case_count_labels(p^.nodes);
  155. if t_times<1 then
  156. t_times:=1;
  157. end;
  158. { first case }
  159. hp:=p^.right;
  160. while assigned(hp) do
  161. begin
  162. cleartempgen;
  163. firstpass(hp^.right);
  164. { searchs max registers }
  165. if hp^.right^.registers32>p^.registers32 then
  166. p^.registers32:=hp^.right^.registers32;
  167. if hp^.right^.registersfpu>p^.registersfpu then
  168. p^.registersfpu:=hp^.right^.registersfpu;
  169. {$ifdef SUPPORT_MMX}
  170. if hp^.right^.registersmmx>p^.registersmmx then
  171. p^.registersmmx:=hp^.right^.registersmmx;
  172. {$endif SUPPORT_MMX}
  173. hp:=hp^.left;
  174. end;
  175. { may be handle else tree }
  176. if assigned(p^.elseblock) then
  177. begin
  178. cleartempgen;
  179. firstpass(p^.elseblock);
  180. if codegenerror then
  181. exit;
  182. if p^.registers32<p^.elseblock^.registers32 then
  183. p^.registers32:=p^.elseblock^.registers32;
  184. if p^.registersfpu<p^.elseblock^.registersfpu then
  185. p^.registersfpu:=p^.elseblock^.registersfpu;
  186. {$ifdef SUPPORT_MMX}
  187. if p^.registersmmx<p^.elseblock^.registersmmx then
  188. p^.registersmmx:=p^.elseblock^.registersmmx;
  189. {$endif SUPPORT_MMX}
  190. end;
  191. t_times:=old_t_times;
  192. { there is one register required for the case expression }
  193. if p^.registers32<1 then p^.registers32:=1;
  194. end;
  195. end.
  196. {
  197. $Log$
  198. Revision 1.3 1998-11-13 10:17:06 peter
  199. + constant eval for in
  200. Revision 1.2 1998/10/06 20:49:13 peter
  201. * m68k compiler compiles again
  202. Revision 1.1 1998/09/23 20:42:24 peter
  203. * splitted pass_1
  204. }