tcset.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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. {$i defines.inc}
  20. interface
  21. uses
  22. tree;
  23. procedure firstsetelement(var p : ptree);
  24. procedure firstin(var p : ptree);
  25. procedure firstrange(var p : ptree);
  26. procedure firstcase(var p : ptree);
  27. implementation
  28. uses
  29. globtype,systems,
  30. cobjects,verbose,globals,
  31. symconst,symtable,aasm,types,
  32. htypechk,pass_1,
  33. tccnv,cpubase
  34. {$ifdef newcg}
  35. ,cgbase
  36. ,tgcpu
  37. {$else newcg}
  38. ,hcodegen
  39. {$ifdef i386}
  40. ,tgeni386
  41. {$endif}
  42. {$ifdef m68k}
  43. ,tgen68k
  44. {$endif}
  45. {$endif newcg}
  46. ;
  47. {*****************************************************************************
  48. FirstSetElement
  49. *****************************************************************************}
  50. procedure firstsetelement(var p : ptree);
  51. begin
  52. firstpass(p^.left);
  53. set_varstate(p^.left,true);
  54. if codegenerror then
  55. exit;
  56. if assigned(p^.right) then
  57. begin
  58. firstpass(p^.right);
  59. if codegenerror then
  60. exit;
  61. end;
  62. calcregisters(p,0,0,0);
  63. p^.resulttype:=p^.left^.resulttype;
  64. set_location(p^.location,p^.left^.location);
  65. end;
  66. {*****************************************************************************
  67. FirstIn
  68. *****************************************************************************}
  69. procedure firstin(var p : ptree);
  70. type
  71. byteset = set of byte;
  72. var
  73. t : ptree;
  74. pst : pconstset;
  75. function createsetconst(psd : psetdef) : pconstset;
  76. var
  77. pcs : pconstset;
  78. pes : penumsym;
  79. i : longint;
  80. begin
  81. new(pcs);
  82. case psd^.elementtype.def^.deftype of
  83. enumdef :
  84. begin
  85. pes:=penumdef(psd^.elementtype.def)^.firstenum;
  86. while assigned(pes) do
  87. begin
  88. pcs^[pes^.value div 8]:=pcs^[pes^.value div 8] or (1 shl (pes^.value mod 8));
  89. pes:=pes^.nextenum;
  90. end;
  91. end;
  92. orddef :
  93. begin
  94. for i:=porddef(psd^.elementtype.def)^.low to porddef(psd^.elementtype.def)^.high do
  95. begin
  96. pcs^[i div 8]:=pcs^[i div 8] or (1 shl (i mod 8));
  97. end;
  98. end;
  99. end;
  100. createsetconst:=pcs;
  101. end;
  102. begin
  103. p^.location.loc:=LOC_FLAGS;
  104. p^.resulttype:=booldef;
  105. firstpass(p^.right);
  106. set_varstate(p^.right,true);
  107. if codegenerror then
  108. exit;
  109. { Convert array constructor first to set }
  110. if is_array_constructor(p^.right^.resulttype) then
  111. begin
  112. arrayconstructor_to_set(p^.right);
  113. firstpass(p^.right);
  114. if codegenerror then
  115. exit;
  116. end;
  117. { if p^.right is a typen then the def
  118. is in typenodetype PM }
  119. if p^.right^.treetype=typen then
  120. p^.right^.resulttype:=p^.right^.typenodetype;
  121. if p^.right^.resulttype^.deftype<>setdef then
  122. CGMessage(sym_e_set_expected);
  123. if codegenerror then
  124. exit;
  125. if (p^.right^.treetype=typen) then
  126. begin
  127. { we need to create a setconstn }
  128. pst:=createsetconst(psetdef(p^.right^.typenodetype));
  129. t:=gensetconstnode(pst,psetdef(p^.right^.typenodetype));
  130. dispose(pst);
  131. putnode(p^.right);
  132. p^.right:=t;
  133. end;
  134. firstpass(p^.left);
  135. set_varstate(p^.left,true);
  136. if codegenerror then
  137. exit;
  138. { empty set then return false }
  139. if not assigned(psetdef(p^.right^.resulttype)^.elementtype.def) then
  140. begin
  141. t:=genordinalconstnode(0,booldef);
  142. disposetree(p);
  143. firstpass(t);
  144. p:=t;
  145. exit;
  146. end;
  147. { type conversion/check }
  148. p^.left:=gentypeconvnode(p^.left,psetdef(p^.right^.resulttype)^.elementtype.def);
  149. firstpass(p^.left);
  150. if codegenerror then
  151. exit;
  152. { constant evaulation }
  153. if (p^.left^.treetype=ordconstn) and (p^.right^.treetype=setconstn) then
  154. begin
  155. t:=genordinalconstnode(byte(p^.left^.value in byteset(p^.right^.value_set^)),booldef);
  156. disposetree(p);
  157. firstpass(t);
  158. p:=t;
  159. exit;
  160. end;
  161. left_right_max(p);
  162. { this is not allways true due to optimization }
  163. { but if we don't set this we get problems with optimizing self code }
  164. if psetdef(p^.right^.resulttype)^.settype<>smallset then
  165. procinfo^.flags:=procinfo^.flags or pi_do_call
  166. else
  167. begin
  168. { a smallset needs maybe an misc. register }
  169. if (p^.left^.treetype<>ordconstn) and
  170. not(p^.right^.location.loc in [LOC_CREGISTER,LOC_REGISTER]) and
  171. (p^.right^.registers32<1) then
  172. inc(p^.registers32);
  173. end;
  174. end;
  175. {*****************************************************************************
  176. FirstRange
  177. *****************************************************************************}
  178. procedure firstrange(var p : ptree);
  179. var
  180. ct : tconverttype;
  181. begin
  182. firstpass(p^.left);
  183. set_varstate(p^.left,true);
  184. firstpass(p^.right);
  185. set_varstate(p^.right,true);
  186. if codegenerror then
  187. exit;
  188. { both types must be compatible }
  189. if not(is_equal(p^.left^.resulttype,p^.right^.resulttype)) and
  190. (isconvertable(p^.left^.resulttype,p^.right^.resulttype,ct,ordconstn,false)=0) then
  191. CGMessage(type_e_mismatch);
  192. { Check if only when its a constant set }
  193. if (p^.left^.treetype=ordconstn) and (p^.right^.treetype=ordconstn) then
  194. begin
  195. { upper limit must be greater or equal than lower limit }
  196. { not if u32bit }
  197. if (p^.left^.value>p^.right^.value) and
  198. (( p^.left^.value<0) or (p^.right^.value>=0)) then
  199. CGMessage(cg_e_upper_lower_than_lower);
  200. end;
  201. left_right_max(p);
  202. p^.resulttype:=p^.left^.resulttype;
  203. set_location(p^.location,p^.left^.location);
  204. end;
  205. {*****************************************************************************
  206. FirstCase
  207. *****************************************************************************}
  208. procedure firstcase(var p : ptree);
  209. var
  210. old_t_times : longint;
  211. hp : ptree;
  212. begin
  213. { evalutes the case expression }
  214. {$ifdef newcg}
  215. tg.cleartempgen;
  216. {$else newcg}
  217. cleartempgen;
  218. {$endif newcg}
  219. firstpass(p^.left);
  220. set_varstate(p^.left,true);
  221. if codegenerror then
  222. exit;
  223. p^.registers32:=p^.left^.registers32;
  224. p^.registersfpu:=p^.left^.registersfpu;
  225. {$ifdef SUPPORT_MMX}
  226. p^.registersmmx:=p^.left^.registersmmx;
  227. {$endif SUPPORT_MMX}
  228. { walk through all instructions }
  229. { estimates the repeat of each instruction }
  230. old_t_times:=t_times;
  231. if not(cs_littlesize in aktglobalswitches) then
  232. begin
  233. t_times:=t_times div case_count_labels(p^.nodes);
  234. if t_times<1 then
  235. t_times:=1;
  236. end;
  237. { first case }
  238. hp:=p^.right;
  239. while assigned(hp) do
  240. begin
  241. {$ifdef newcg}
  242. tg.cleartempgen;
  243. {$else newcg}
  244. cleartempgen;
  245. {$endif newcg}
  246. firstpass(hp^.right);
  247. { searchs max registers }
  248. if hp^.right^.registers32>p^.registers32 then
  249. p^.registers32:=hp^.right^.registers32;
  250. if hp^.right^.registersfpu>p^.registersfpu then
  251. p^.registersfpu:=hp^.right^.registersfpu;
  252. {$ifdef SUPPORT_MMX}
  253. if hp^.right^.registersmmx>p^.registersmmx then
  254. p^.registersmmx:=hp^.right^.registersmmx;
  255. {$endif SUPPORT_MMX}
  256. hp:=hp^.left;
  257. end;
  258. { may be handle else tree }
  259. if assigned(p^.elseblock) then
  260. begin
  261. {$ifdef newcg}
  262. tg.cleartempgen;
  263. {$else newcg}
  264. cleartempgen;
  265. {$endif newcg}
  266. firstpass(p^.elseblock);
  267. if codegenerror then
  268. exit;
  269. if p^.registers32<p^.elseblock^.registers32 then
  270. p^.registers32:=p^.elseblock^.registers32;
  271. if p^.registersfpu<p^.elseblock^.registersfpu then
  272. p^.registersfpu:=p^.elseblock^.registersfpu;
  273. {$ifdef SUPPORT_MMX}
  274. if p^.registersmmx<p^.elseblock^.registersmmx then
  275. p^.registersmmx:=p^.elseblock^.registersmmx;
  276. {$endif SUPPORT_MMX}
  277. end;
  278. t_times:=old_t_times;
  279. { there is one register required for the case expression }
  280. { for 64 bit ints we cheat: the high dword is stored in EDI }
  281. { so we don't need an extra register }
  282. if p^.registers32<1 then p^.registers32:=1;
  283. end;
  284. end.
  285. {
  286. $Log$
  287. Revision 1.4 2000-09-24 21:19:53 peter
  288. * delphi compile fixes
  289. Revision 1.3 2000/08/12 06:46:26 florian
  290. + case statement for int64/qword implemented
  291. Revision 1.2 2000/07/13 11:32:52 michael
  292. + removed logs
  293. }