nset.pas 15 KB

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