nset.pas 18 KB

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