nset.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. {
  2. $Id$
  3. Copyright (c) 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 nset;
  19. {$i defines.inc}
  20. interface
  21. uses
  22. node,cpuinfo,aasm;
  23. type
  24. pcaserecord = ^tcaserecord;
  25. tcaserecord = record
  26. { range }
  27. _low,_high : TConstExprInt;
  28. { only used by gentreejmp }
  29. _at : pasmlabel;
  30. { label of instruction }
  31. statement : pasmlabel;
  32. { is this the first of an case entry, needed to release statement
  33. label (PFV) }
  34. firstlabel : boolean;
  35. { left and right tree node }
  36. less,greater : pcaserecord;
  37. end;
  38. tsetelementnode = class(tbinarynode)
  39. constructor create(l,r : tnode);virtual;
  40. function pass_1 : tnode;override;
  41. end;
  42. tinnode = class(tbinopnode)
  43. constructor create(l,r : tnode);virtual;
  44. function pass_1 : tnode;override;
  45. end;
  46. trangenode = class(tbinarynode)
  47. constructor create(l,r : tnode);virtual;
  48. function pass_1 : tnode;override;
  49. end;
  50. tcasenode = class(tbinarynode)
  51. nodes : pcaserecord;
  52. elseblock : tnode;
  53. constructor create(l,r : tnode;n : pcaserecord);virtual;
  54. destructor destroy;override;
  55. function getcopy : tnode;override;
  56. function pass_1 : tnode;override;
  57. end;
  58. var
  59. csetelementnode : class of tsetelementnode;
  60. cinnode : class of tinnode;
  61. crangenode : class of trangenode;
  62. ccasenode : class of tcasenode;
  63. { counts the labels }
  64. function case_count_labels(root : pcaserecord) : longint;
  65. { searches the highest label }
  66. function case_get_max(root : pcaserecord) : longint;
  67. { searches the lowest label }
  68. function case_get_min(root : pcaserecord) : longint;
  69. function gencasenode(l,r : tnode;nodes : pcaserecord) : tnode;
  70. implementation
  71. uses
  72. globtype,systems,
  73. cobjects,verbose,globals,
  74. symconst,symtable,types,
  75. htypechk,pass_1,
  76. ncnv,ncon,cpubase,nld
  77. {$ifdef newcg}
  78. ,cgbase
  79. ,tgcpu
  80. {$else newcg}
  81. ,hcodegen
  82. {$ifdef i386}
  83. ,tgeni386
  84. {$endif}
  85. {$ifdef m68k}
  86. ,tgen68k
  87. {$endif}
  88. {$endif newcg}
  89. ;
  90. function gencasenode(l,r : tnode;nodes : pcaserecord) : tnode;
  91. var
  92. t : tnode;
  93. begin
  94. t:=ccasenode.create(l,r,nodes);
  95. gencasenode:=t;
  96. end;
  97. {*****************************************************************************
  98. TSETELEMENTNODE
  99. *****************************************************************************}
  100. constructor tsetelementnode.create(l,r : tnode);
  101. begin
  102. inherited create(setelementn,l,r);
  103. end;
  104. function tsetelementnode.pass_1 : tnode;
  105. begin
  106. pass_1:=nil;
  107. firstpass(left);
  108. set_varstate(left,true);
  109. if codegenerror then
  110. exit;
  111. if assigned(right) then
  112. begin
  113. firstpass(right);
  114. if codegenerror then
  115. exit;
  116. end;
  117. calcregisters(self,0,0,0);
  118. resulttype:=left.resulttype;
  119. set_location(location,left.location);
  120. end;
  121. {*****************************************************************************
  122. TINNODE
  123. *****************************************************************************}
  124. constructor tinnode.create(l,r : tnode);
  125. begin
  126. inherited create(inn,l,r);
  127. end;
  128. function tinnode.pass_1 : tnode;
  129. type
  130. byteset = set of byte;
  131. var
  132. t : tnode;
  133. pst : pconstset;
  134. function createsetconst(psd : psetdef) : pconstset;
  135. var
  136. pcs : pconstset;
  137. pes : penumsym;
  138. i : longint;
  139. begin
  140. new(pcs);
  141. case psd^.elementtype.def^.deftype of
  142. enumdef :
  143. begin
  144. pes:=penumdef(psd^.elementtype.def)^.firstenum;
  145. while assigned(pes) do
  146. begin
  147. pcs^[pes^.value div 8]:=pcs^[pes^.value div 8] or (1 shl (pes^.value mod 8));
  148. pes:=pes^.nextenum;
  149. end;
  150. end;
  151. orddef :
  152. begin
  153. for i:=porddef(psd^.elementtype.def)^.low to porddef(psd^.elementtype.def)^.high do
  154. begin
  155. pcs^[i div 8]:=pcs^[i div 8] or (1 shl (i mod 8));
  156. end;
  157. end;
  158. end;
  159. createsetconst:=pcs;
  160. end;
  161. begin
  162. pass_1:=nil;
  163. location.loc:=LOC_FLAGS;
  164. resulttype:=booldef;
  165. firstpass(right);
  166. set_varstate(right,true);
  167. if codegenerror then
  168. exit;
  169. { Convert array constructor first to set }
  170. if is_array_constructor(right.resulttype) then
  171. begin
  172. arrayconstructor_to_set(tarrayconstructnode(right));
  173. firstpass(right);
  174. if codegenerror then
  175. exit;
  176. end;
  177. { if right is a typen then the def
  178. is in typenodetype PM }
  179. if right.nodetype=typen then
  180. right.resulttype:=ttypenode(right).typenodetype;
  181. if right.resulttype^.deftype<>setdef then
  182. CGMessage(sym_e_set_expected);
  183. if codegenerror then
  184. exit;
  185. if (right.nodetype=typen) then
  186. begin
  187. { we need to create a setconstn }
  188. pst:=createsetconst(psetdef(ttypenode(right).typenodetype));
  189. t:=gensetconstnode(pst,psetdef(ttypenode(right).typenodetype));
  190. dispose(pst);
  191. right.free;
  192. right:=t;
  193. end;
  194. firstpass(left);
  195. set_varstate(left,true);
  196. if codegenerror then
  197. exit;
  198. { empty set then return false }
  199. if not assigned(psetdef(right.resulttype)^.elementtype.def) then
  200. begin
  201. t:=genordinalconstnode(0,booldef);
  202. firstpass(t);
  203. pass_1:=t;
  204. exit;
  205. end;
  206. { type conversion/check }
  207. left:=gentypeconvnode(left,psetdef(right.resulttype)^.elementtype.def);
  208. firstpass(left);
  209. if codegenerror then
  210. exit;
  211. { constant evaulation }
  212. if (left.nodetype=ordconstn) and (right.nodetype=setconstn) then
  213. begin
  214. t:=genordinalconstnode(byte(tordconstnode(left).value in byteset(tsetconstnode(right).value_set^)),booldef);
  215. firstpass(t);
  216. pass_1:=t;
  217. exit;
  218. end;
  219. left_right_max;
  220. { this is not allways true due to optimization }
  221. { but if we don't set this we get problems with optimizing self code }
  222. if psetdef(right.resulttype)^.settype<>smallset then
  223. procinfo^.flags:=procinfo^.flags or pi_do_call
  224. else
  225. begin
  226. { a smallset needs maybe an misc. register }
  227. if (left.nodetype<>ordconstn) and
  228. not(right.location.loc in [LOC_CREGISTER,LOC_REGISTER]) and
  229. (right.registers32<1) then
  230. inc(registers32);
  231. end;
  232. end;
  233. {*****************************************************************************
  234. TRANGENODE
  235. *****************************************************************************}
  236. constructor trangenode.create(l,r : tnode);
  237. begin
  238. inherited create(rangen,l,r);
  239. end;
  240. function trangenode.pass_1 : tnode;
  241. var
  242. ct : tconverttype;
  243. begin
  244. pass_1:=nil;
  245. firstpass(left);
  246. set_varstate(left,true);
  247. firstpass(right);
  248. set_varstate(right,true);
  249. if codegenerror then
  250. exit;
  251. { both types must be compatible }
  252. if not(is_equal(left.resulttype,right.resulttype)) and
  253. (isconvertable(left.resulttype,right.resulttype,ct,ordconstn,false)=0) then
  254. CGMessage(type_e_mismatch);
  255. { Check if only when its a constant set }
  256. if (left.nodetype=ordconstn) and (right.nodetype=ordconstn) then
  257. begin
  258. { upper limit must be greater or equal than lower limit }
  259. { not if u32bit }
  260. if (tordconstnode(left).value>tordconstnode(right).value) and
  261. ((tordconstnode(left).value<0) or (tordconstnode(right).value>=0)) then
  262. CGMessage(cg_e_upper_lower_than_lower);
  263. end;
  264. left_right_max;
  265. resulttype:=left.resulttype;
  266. set_location(location,left.location);
  267. end;
  268. {*****************************************************************************
  269. Case Helpers
  270. *****************************************************************************}
  271. function case_count_labels(root : pcaserecord) : longint;
  272. var
  273. _l : longint;
  274. procedure count(p : pcaserecord);
  275. begin
  276. inc(_l);
  277. if assigned(p^.less) then
  278. count(p^.less);
  279. if assigned(p^.greater) then
  280. count(p^.greater);
  281. end;
  282. begin
  283. _l:=0;
  284. count(root);
  285. case_count_labels:=_l;
  286. end;
  287. function case_get_max(root : pcaserecord) : longint;
  288. var
  289. hp : pcaserecord;
  290. begin
  291. hp:=root;
  292. while assigned(hp^.greater) do
  293. hp:=hp^.greater;
  294. case_get_max:=hp^._high;
  295. end;
  296. function case_get_min(root : pcaserecord) : longint;
  297. var
  298. hp : pcaserecord;
  299. begin
  300. hp:=root;
  301. while assigned(hp^.less) do
  302. hp:=hp^.less;
  303. case_get_min:=hp^._low;
  304. end;
  305. procedure deletecaselabels(p : pcaserecord);
  306. begin
  307. if assigned(p^.greater) then
  308. deletecaselabels(p^.greater);
  309. if assigned(p^.less) then
  310. deletecaselabels(p^.less);
  311. dispose(p);
  312. end;
  313. function copycaserecord(p : pcaserecord) : pcaserecord;
  314. var
  315. n : pcaserecord;
  316. begin
  317. new(n);
  318. n^:=p^;
  319. if assigned(p^.greater) then
  320. n^.greater:=copycaserecord(p^.greater);
  321. if assigned(p^.less) then
  322. n^.less:=copycaserecord(p^.less);
  323. copycaserecord:=n;
  324. end;
  325. {*****************************************************************************
  326. TCASENODE
  327. *****************************************************************************}
  328. constructor tcasenode.create(l,r : tnode;n : pcaserecord);
  329. begin
  330. inherited create(casen,l,r);
  331. nodes:=n;
  332. elseblock:=nil;
  333. set_file_line(l);
  334. end;
  335. destructor tcasenode.destroy;
  336. begin
  337. elseblock.free;
  338. deletecaselabels(nodes);
  339. inherited destroy;
  340. end;
  341. function tcasenode.pass_1 : tnode;
  342. var
  343. old_t_times : longint;
  344. hp : tbinarynode;
  345. begin
  346. pass_1:=nil;
  347. { evalutes the case expression }
  348. {$ifdef newcg}
  349. tg.cleartempgen;
  350. {$else newcg}
  351. cleartempgen;
  352. {$endif newcg}
  353. firstpass(left);
  354. set_varstate(left,true);
  355. if codegenerror then
  356. exit;
  357. registers32:=left.registers32;
  358. registersfpu:=left.registersfpu;
  359. {$ifdef SUPPORT_MMX}
  360. registersmmx:=left.registersmmx;
  361. {$endif SUPPORT_MMX}
  362. { walk through all instructions }
  363. { estimates the repeat of each instruction }
  364. old_t_times:=t_times;
  365. if not(cs_littlesize in aktglobalswitches) then
  366. begin
  367. t_times:=t_times div case_count_labels(nodes);
  368. if t_times<1 then
  369. t_times:=1;
  370. end;
  371. { first case }
  372. hp:=tbinarynode(right);
  373. while assigned(hp) do
  374. begin
  375. {$ifdef newcg}
  376. tg.cleartempgen;
  377. {$else newcg}
  378. cleartempgen;
  379. {$endif newcg}
  380. firstpass(hp.right);
  381. { searchs max registers }
  382. if hp.right.registers32>registers32 then
  383. registers32:=hp.right.registers32;
  384. if hp.right.registersfpu>registersfpu then
  385. registersfpu:=hp.right.registersfpu;
  386. {$ifdef SUPPORT_MMX}
  387. if hp.right.registersmmx>registersmmx then
  388. registersmmx:=hp.right.registersmmx;
  389. {$endif SUPPORT_MMX}
  390. hp:=tbinarynode(hp.left);
  391. end;
  392. { may be handle else tree }
  393. if assigned(elseblock) then
  394. begin
  395. {$ifdef newcg}
  396. tg.cleartempgen;
  397. {$else newcg}
  398. cleartempgen;
  399. {$endif newcg}
  400. firstpass(elseblock);
  401. if codegenerror then
  402. exit;
  403. if registers32<elseblock.registers32 then
  404. registers32:=elseblock.registers32;
  405. if registersfpu<elseblock.registersfpu then
  406. registersfpu:=elseblock.registersfpu;
  407. {$ifdef SUPPORT_MMX}
  408. if registersmmx<elseblock.registersmmx then
  409. registersmmx:=elseblock.registersmmx;
  410. {$endif SUPPORT_MMX}
  411. end;
  412. t_times:=old_t_times;
  413. { there is one register required for the case expression }
  414. { for 64 bit ints we cheat: the high dword is stored in EDI }
  415. { so we don't need an extra register }
  416. if registers32<1 then registers32:=1;
  417. end;
  418. function tcasenode.getcopy : tnode;
  419. var
  420. p : tcasenode;
  421. begin
  422. p:=tcasenode(inherited getcopy);
  423. p.elseblock:=elseblock.getcopy;
  424. p.nodes:=copycaserecord(nodes);
  425. getcopy:=p;
  426. end;
  427. begin
  428. csetelementnode:=tsetelementnode;
  429. cinnode:=tinnode;
  430. crangenode:=trangenode;
  431. ccasenode:=tcasenode;
  432. end.
  433. {
  434. $Log$
  435. Revision 1.4 2000-10-01 19:48:25 peter
  436. * lot of compile updates for cg11
  437. Revision 1.3 2000/09/27 18:14:31 florian
  438. * fixed a lot of syntax errors in the n*.pas stuff
  439. Revision 1.2 2000/09/24 20:17:44 florian
  440. * more conversion work done
  441. Revision 1.1 2000/09/24 19:38:39 florian
  442. * initial implementation
  443. }