tcset.pas 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. globtype,systems,
  29. cobjects,verbose,globals,
  30. symtable,aasm,types,
  31. hcodegen,htypechk,pass_1,
  32. tccnv
  33. {$ifdef i386}
  34. {$ifdef ag386bin}
  35. ,i386base
  36. {$else}
  37. ,i386
  38. {$endif}
  39. ,tgeni386
  40. {$endif}
  41. {$ifdef m68k}
  42. ,m68k,tgen68k
  43. {$endif}
  44. ;
  45. {*****************************************************************************
  46. FirstSetElement
  47. *****************************************************************************}
  48. procedure firstsetelement(var p : ptree);
  49. begin
  50. firstpass(p^.left);
  51. if codegenerror then
  52. exit;
  53. if assigned(p^.right) then
  54. begin
  55. firstpass(p^.right);
  56. if codegenerror then
  57. exit;
  58. end;
  59. calcregisters(p,0,0,0);
  60. p^.resulttype:=p^.left^.resulttype;
  61. set_location(p^.location,p^.left^.location);
  62. end;
  63. {*****************************************************************************
  64. FirstIn
  65. *****************************************************************************}
  66. procedure firstin(var p : ptree);
  67. type
  68. byteset = set of byte;
  69. var
  70. t : ptree;
  71. begin
  72. p^.location.loc:=LOC_FLAGS;
  73. p^.resulttype:=booldef;
  74. firstpass(p^.right);
  75. if codegenerror then
  76. exit;
  77. { Convert array constructor first to set }
  78. if is_array_constructor(p^.right^.resulttype) then
  79. begin
  80. arrayconstructor_to_set(p^.right);
  81. firstpass(p^.right);
  82. if codegenerror then
  83. exit;
  84. end;
  85. if p^.right^.resulttype^.deftype<>setdef then
  86. CGMessage(sym_e_set_expected);
  87. firstpass(p^.left);
  88. if codegenerror then
  89. exit;
  90. { empty set then return false }
  91. if not assigned(psetdef(p^.right^.resulttype)^.setof) then
  92. begin
  93. t:=genordinalconstnode(0,booldef);
  94. disposetree(p);
  95. firstpass(t);
  96. p:=t;
  97. exit;
  98. end;
  99. { type conversion/check }
  100. p^.left:=gentypeconvnode(p^.left,psetdef(p^.right^.resulttype)^.setof);
  101. firstpass(p^.left);
  102. if codegenerror then
  103. exit;
  104. { constant evaulation }
  105. if (p^.left^.treetype=ordconstn) and (p^.right^.treetype=setconstn) then
  106. begin
  107. t:=genordinalconstnode(byte(p^.left^.value in byteset(p^.right^.value_set^)),booldef);
  108. disposetree(p);
  109. firstpass(t);
  110. p:=t;
  111. exit;
  112. end;
  113. left_right_max(p);
  114. { this is not allways true due to optimization }
  115. { but if we don't set this we get problems with optimizing self code }
  116. if psetdef(p^.right^.resulttype)^.settype<>smallset then
  117. procinfo.flags:=procinfo.flags or pi_do_call
  118. else
  119. begin
  120. { a smallset needs maybe an misc. register }
  121. if (p^.left^.treetype<>ordconstn) and
  122. not(p^.right^.location.loc in [LOC_CREGISTER,LOC_REGISTER]) and
  123. (p^.right^.registers32<1) then
  124. inc(p^.registers32);
  125. end;
  126. end;
  127. {*****************************************************************************
  128. FirstRange
  129. *****************************************************************************}
  130. procedure firstrange(var p : ptree);
  131. var
  132. ct : tconverttype;
  133. begin
  134. firstpass(p^.left);
  135. firstpass(p^.right);
  136. if codegenerror then
  137. exit;
  138. { both types must be compatible }
  139. if not(is_equal(p^.left^.resulttype,p^.right^.resulttype)) and
  140. (isconvertable(p^.left^.resulttype,p^.right^.resulttype,ct,ordconstn,false)=0) then
  141. CGMessage(type_e_mismatch);
  142. { Check if only when its a constant set }
  143. if (p^.left^.treetype=ordconstn) and (p^.right^.treetype=ordconstn) then
  144. begin
  145. { upper limit must be greater or equal than lower limit }
  146. { not if u32bit }
  147. if (p^.left^.value>p^.right^.value) and
  148. (( p^.left^.value<0) or (p^.right^.value>=0)) then
  149. CGMessage(cg_e_upper_lower_than_lower);
  150. end;
  151. left_right_max(p);
  152. p^.resulttype:=p^.left^.resulttype;
  153. set_location(p^.location,p^.left^.location);
  154. end;
  155. {*****************************************************************************
  156. FirstCase
  157. *****************************************************************************}
  158. procedure firstcase(var p : ptree);
  159. var
  160. old_t_times : longint;
  161. hp : ptree;
  162. begin
  163. { evalutes the case expression }
  164. cleartempgen;
  165. must_be_valid:=true;
  166. firstpass(p^.left);
  167. if codegenerror then
  168. exit;
  169. p^.registers32:=p^.left^.registers32;
  170. p^.registersfpu:=p^.left^.registersfpu;
  171. {$ifdef SUPPORT_MMX}
  172. p^.registersmmx:=p^.left^.registersmmx;
  173. {$endif SUPPORT_MMX}
  174. { walk through all instructions }
  175. { estimates the repeat of each instruction }
  176. old_t_times:=t_times;
  177. if not(cs_littlesize in aktglobalswitches) then
  178. begin
  179. t_times:=t_times div case_count_labels(p^.nodes);
  180. if t_times<1 then
  181. t_times:=1;
  182. end;
  183. { first case }
  184. hp:=p^.right;
  185. while assigned(hp) do
  186. begin
  187. cleartempgen;
  188. firstpass(hp^.right);
  189. { searchs max registers }
  190. if hp^.right^.registers32>p^.registers32 then
  191. p^.registers32:=hp^.right^.registers32;
  192. if hp^.right^.registersfpu>p^.registersfpu then
  193. p^.registersfpu:=hp^.right^.registersfpu;
  194. {$ifdef SUPPORT_MMX}
  195. if hp^.right^.registersmmx>p^.registersmmx then
  196. p^.registersmmx:=hp^.right^.registersmmx;
  197. {$endif SUPPORT_MMX}
  198. hp:=hp^.left;
  199. end;
  200. { may be handle else tree }
  201. if assigned(p^.elseblock) then
  202. begin
  203. cleartempgen;
  204. firstpass(p^.elseblock);
  205. if codegenerror then
  206. exit;
  207. if p^.registers32<p^.elseblock^.registers32 then
  208. p^.registers32:=p^.elseblock^.registers32;
  209. if p^.registersfpu<p^.elseblock^.registersfpu then
  210. p^.registersfpu:=p^.elseblock^.registersfpu;
  211. {$ifdef SUPPORT_MMX}
  212. if p^.registersmmx<p^.elseblock^.registersmmx then
  213. p^.registersmmx:=p^.elseblock^.registersmmx;
  214. {$endif SUPPORT_MMX}
  215. end;
  216. t_times:=old_t_times;
  217. { there is one register required for the case expression }
  218. if p^.registers32<1 then p^.registers32:=1;
  219. end;
  220. end.
  221. {
  222. $Log$
  223. Revision 1.8 1999-04-14 15:00:13 peter
  224. * forgot firstpass after array->set conversion
  225. Revision 1.7 1999/03/02 18:22:36 peter
  226. * arrayconstructor convert for in
  227. Revision 1.6 1999/02/22 02:15:55 peter
  228. * updates for ag386bin
  229. Revision 1.5 1998/12/18 17:15:40 peter
  230. * added 'in []' support
  231. Revision 1.4 1998/12/11 00:03:58 peter
  232. + globtype,tokens,version unit splitted from globals
  233. Revision 1.3 1998/11/13 10:17:06 peter
  234. + constant eval for in
  235. Revision 1.2 1998/10/06 20:49:13 peter
  236. * m68k compiler compiles again
  237. Revision 1.1 1998/09/23 20:42:24 peter
  238. * splitted pass_1
  239. }