tcset.pas 11 KB

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