nset.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. {
  2. $Id$
  3. Copyright (c) 2000-2002 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 fpcdefs.inc}
  20. interface
  21. uses
  22. node,globals,aasmbase,aasmtai;
  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. tsetelementnodeclass = class of tsetelementnode;
  44. tinnode = class(tbinopnode)
  45. constructor create(l,r : tnode);virtual;
  46. function det_resulttype:tnode;override;
  47. function pass_1 : tnode;override;
  48. end;
  49. tinnodeclass = class of tinnode;
  50. trangenode = class(tbinarynode)
  51. constructor create(l,r : tnode);virtual;
  52. function det_resulttype:tnode;override;
  53. function pass_1 : tnode;override;
  54. end;
  55. trangenodeclass = class of trangenode;
  56. tcasenode = class(tbinarynode)
  57. nodes : pcaserecord;
  58. elseblock : tnode;
  59. constructor create(l,r : tnode;n : pcaserecord);virtual;
  60. destructor destroy;override;
  61. function getcopy : tnode;override;
  62. procedure insertintolist(l : tnodelist);override;
  63. function det_resulttype:tnode;override;
  64. function pass_1 : tnode;override;
  65. function docompare(p: tnode): boolean; override;
  66. end;
  67. tcasenodeclass = class of tcasenode;
  68. var
  69. csetelementnode : tsetelementnodeclass;
  70. cinnode : tinnodeclass;
  71. crangenode : trangenodeclass;
  72. ccasenode : tcasenodeclass;
  73. { counts the labels }
  74. function case_count_labels(root : pcaserecord) : longint;
  75. { searches the highest label }
  76. {$ifdef int64funcresok}
  77. function case_get_max(root : pcaserecord) : tconstexprint;
  78. {$else int64funcresok}
  79. function case_get_max(root : pcaserecord) : longint;
  80. {$endif int64funcresok}
  81. { searches the lowest label }
  82. {$ifdef int64funcresok}
  83. function case_get_min(root : pcaserecord) : tconstexprint;
  84. {$else int64funcresok}
  85. function case_get_min(root : pcaserecord) : longint;
  86. {$endif int64funcresok}
  87. function gencasenode(l,r : tnode;nodes : pcaserecord) : tnode;
  88. implementation
  89. uses
  90. globtype,systems,
  91. verbose,
  92. symconst,symdef,symsym,defbase,
  93. htypechk,pass_1,
  94. ncnv,ncon,cpubase,nld,rgobj,cgbase;
  95. function gencasenode(l,r : tnode;nodes : pcaserecord) : tnode;
  96. var
  97. t : tnode;
  98. begin
  99. t:=ccasenode.create(l,r,nodes);
  100. gencasenode:=t;
  101. end;
  102. {*****************************************************************************
  103. TSETELEMENTNODE
  104. *****************************************************************************}
  105. constructor tsetelementnode.create(l,r : tnode);
  106. begin
  107. inherited create(setelementn,l,r);
  108. end;
  109. function tsetelementnode.det_resulttype:tnode;
  110. begin
  111. result:=nil;
  112. resulttypepass(left);
  113. if assigned(right) then
  114. resulttypepass(right);
  115. set_varstate(left,true);
  116. if codegenerror then
  117. exit;
  118. resulttype:=left.resulttype;
  119. end;
  120. function tsetelementnode.pass_1 : tnode;
  121. begin
  122. result:=nil;
  123. firstpass(left);
  124. if assigned(right) then
  125. firstpass(right);
  126. if codegenerror then
  127. exit;
  128. location_copy(location,left.location);
  129. calcregisters(self,0,0,0);
  130. end;
  131. {*****************************************************************************
  132. TINNODE
  133. *****************************************************************************}
  134. constructor tinnode.create(l,r : tnode);
  135. begin
  136. inherited create(inn,l,r);
  137. end;
  138. function tinnode.det_resulttype:tnode;
  139. {$ifdef oldset}
  140. type
  141. byteset = set of byte;
  142. {$endif}
  143. var
  144. t : tnode;
  145. pst : pconstset;
  146. function createsetconst(psd : tsetdef) : pconstset;
  147. var
  148. pcs : pconstset;
  149. pes : tenumsym;
  150. i : longint;
  151. begin
  152. new(pcs);
  153. case psd.elementtype.def.deftype of
  154. enumdef :
  155. begin
  156. pes:=tenumsym(tenumdef(psd.elementtype.def).firstenum);
  157. while assigned(pes) do
  158. begin
  159. {$ifdef oldset}
  160. pcs^[pes.value div 8]:=pcs^[pes.value div 8] or (1 shl (pes.value mod 8));
  161. {$else}
  162. include(pcs^,pes.value);
  163. {$endif}
  164. pes:=pes.nextenum;
  165. end;
  166. end;
  167. orddef :
  168. begin
  169. for i:=torddef(psd.elementtype.def).low to torddef(psd.elementtype.def).high do
  170. {$ifdef oldset}
  171. pcs^[i div 8]:=pcs^[i div 8] or (1 shl (i mod 8));
  172. {$else}
  173. include(pcs^,i);
  174. {$endif}
  175. end;
  176. end;
  177. createsetconst:=pcs;
  178. end;
  179. begin
  180. result:=nil;
  181. resulttype:=booltype;
  182. resulttypepass(right);
  183. set_varstate(right,true);
  184. if codegenerror then
  185. exit;
  186. { Convert array constructor first to set }
  187. if is_array_constructor(right.resulttype.def) then
  188. begin
  189. arrayconstructor_to_set(right);
  190. firstpass(right);
  191. if codegenerror then
  192. exit;
  193. end;
  194. if right.resulttype.def.deftype<>setdef then
  195. CGMessage(sym_e_set_expected);
  196. if (right.nodetype=typen) then
  197. begin
  198. { we need to create a setconstn }
  199. pst:=createsetconst(tsetdef(ttypenode(right).resulttype.def));
  200. t:=csetconstnode.create(pst,ttypenode(right).resulttype);
  201. dispose(pst);
  202. right.free;
  203. right:=t;
  204. end;
  205. resulttypepass(left);
  206. set_varstate(left,true);
  207. if codegenerror then
  208. exit;
  209. { type conversion/check }
  210. if assigned(tsetdef(right.resulttype.def).elementtype.def) then
  211. inserttypeconv(left,tsetdef(right.resulttype.def).elementtype);
  212. { empty set then return false }
  213. if not assigned(tsetdef(right.resulttype.def).elementtype.def) or
  214. ((right.nodetype = setconstn) and
  215. {$ifdef oldset}
  216. (byteset(tsetconstnode(right).value_set^) = [])) then
  217. {$else oldset}
  218. (tsetconstnode(right).value_set^ = [])) then
  219. {$endif oldset}
  220. begin
  221. t:=cordconstnode.create(0,booltype);
  222. resulttypepass(t);
  223. result:=t;
  224. exit;
  225. end;
  226. { constant evaluation }
  227. if (left.nodetype=ordconstn) and (right.nodetype=setconstn) then
  228. begin
  229. {$ifdef oldset}
  230. t:=cordconstnode.create(byte(tordconstnode(left).value in byteset(tsetconstnode(right).value_set^)),booltype);
  231. {$else}
  232. t:=cordconstnode.create(byte(tordconstnode(left).value in Tsetconstnode(right).value_set^),booltype);
  233. {$endif}
  234. resulttypepass(t);
  235. result:=t;
  236. exit;
  237. end;
  238. end;
  239. { Warning : This is the first pass for the generic version }
  240. { the only difference is mainly the result location which }
  241. { is changed, compared to the i386 version. }
  242. { ALSO REGISTER ALLOC IS WRONG? }
  243. function tinnode.pass_1 : tnode;
  244. begin
  245. result:=nil;
  246. location.loc:=LOC_REGISTER;
  247. firstpass(right);
  248. firstpass(left);
  249. if codegenerror then
  250. exit;
  251. left_right_max;
  252. { this is not allways true due to optimization }
  253. { but if we don't set this we get problems with optimizing self code }
  254. if tsetdef(right.resulttype.def).settype<>smallset then
  255. procinfo^.flags:=procinfo^.flags or pi_do_call
  256. else
  257. begin
  258. { a smallset needs maybe an misc. register }
  259. if (left.nodetype<>ordconstn) and
  260. not(right.location.loc in [LOC_CREGISTER,LOC_REGISTER]) and
  261. (right.registers32<1) then
  262. inc(registers32);
  263. end;
  264. end;
  265. {*****************************************************************************
  266. TRANGENODE
  267. *****************************************************************************}
  268. constructor trangenode.create(l,r : tnode);
  269. begin
  270. inherited create(rangen,l,r);
  271. end;
  272. function trangenode.det_resulttype : tnode;
  273. var
  274. ct : tconverttype;
  275. begin
  276. result:=nil;
  277. resulttypepass(left);
  278. resulttypepass(right);
  279. set_varstate(left,true);
  280. set_varstate(right,true);
  281. if codegenerror then
  282. exit;
  283. { both types must be compatible }
  284. if not(is_equal(left.resulttype.def,right.resulttype.def)) and
  285. (isconvertable(left.resulttype.def,right.resulttype.def,ct,ordconstn,false)=0) then
  286. CGMessage(type_e_mismatch);
  287. { Check if only when its a constant set }
  288. if (left.nodetype=ordconstn) and (right.nodetype=ordconstn) then
  289. begin
  290. { upper limit must be greater or equal than lower limit }
  291. if (tordconstnode(left).value>tordconstnode(right).value) and
  292. ((tordconstnode(left).value<0) or (tordconstnode(right).value>=0)) then
  293. CGMessage(cg_e_upper_lower_than_lower);
  294. end;
  295. resulttype:=left.resulttype;
  296. end;
  297. function trangenode.pass_1 : tnode;
  298. begin
  299. result:=nil;
  300. firstpass(left);
  301. firstpass(right);
  302. if codegenerror then
  303. exit;
  304. left_right_max;
  305. location_copy(location,left.location);
  306. end;
  307. {*****************************************************************************
  308. Case Helpers
  309. *****************************************************************************}
  310. function case_count_labels(root : pcaserecord) : longint;
  311. var
  312. _l : longint;
  313. procedure count(p : pcaserecord);
  314. begin
  315. inc(_l);
  316. if assigned(p^.less) then
  317. count(p^.less);
  318. if assigned(p^.greater) then
  319. count(p^.greater);
  320. end;
  321. begin
  322. _l:=0;
  323. count(root);
  324. case_count_labels:=_l;
  325. end;
  326. {$ifdef int64funcresok}
  327. function case_get_max(root : pcaserecord) : tconstexprint;
  328. {$else int64funcresok}
  329. function case_get_max(root : pcaserecord) : longint;
  330. {$endif int64funcresok}
  331. var
  332. hp : pcaserecord;
  333. begin
  334. hp:=root;
  335. while assigned(hp^.greater) do
  336. hp:=hp^.greater;
  337. case_get_max:=hp^._high;
  338. end;
  339. {$ifdef int64funcresok}
  340. function case_get_min(root : pcaserecord) : tconstexprint;
  341. {$else int64funcresok}
  342. function case_get_min(root : pcaserecord) : longint;
  343. {$endif int64funcresok}
  344. var
  345. hp : pcaserecord;
  346. begin
  347. hp:=root;
  348. while assigned(hp^.less) do
  349. hp:=hp^.less;
  350. case_get_min:=hp^._low;
  351. end;
  352. procedure deletecaselabels(p : pcaserecord);
  353. begin
  354. if assigned(p^.greater) then
  355. deletecaselabels(p^.greater);
  356. if assigned(p^.less) then
  357. deletecaselabels(p^.less);
  358. dispose(p);
  359. end;
  360. function copycaserecord(p : pcaserecord) : pcaserecord;
  361. var
  362. n : pcaserecord;
  363. begin
  364. new(n);
  365. n^:=p^;
  366. if assigned(p^.greater) then
  367. n^.greater:=copycaserecord(p^.greater);
  368. if assigned(p^.less) then
  369. n^.less:=copycaserecord(p^.less);
  370. copycaserecord:=n;
  371. end;
  372. {*****************************************************************************
  373. TCASENODE
  374. *****************************************************************************}
  375. constructor tcasenode.create(l,r : tnode;n : pcaserecord);
  376. begin
  377. inherited create(casen,l,r);
  378. nodes:=n;
  379. elseblock:=nil;
  380. set_file_line(l);
  381. end;
  382. destructor tcasenode.destroy;
  383. begin
  384. elseblock.free;
  385. deletecaselabels(nodes);
  386. inherited destroy;
  387. end;
  388. function tcasenode.det_resulttype : tnode;
  389. begin
  390. result:=nil;
  391. resulttype:=voidtype;
  392. end;
  393. function tcasenode.pass_1 : tnode;
  394. var
  395. old_t_times : longint;
  396. hp : tbinarynode;
  397. begin
  398. result:=nil;
  399. { evalutes the case expression }
  400. rg.cleartempgen;
  401. firstpass(left);
  402. set_varstate(left,true);
  403. if codegenerror then
  404. exit;
  405. registers32:=left.registers32;
  406. registersfpu:=left.registersfpu;
  407. {$ifdef SUPPORT_MMX}
  408. registersmmx:=left.registersmmx;
  409. {$endif SUPPORT_MMX}
  410. { walk through all instructions }
  411. { estimates the repeat of each instruction }
  412. old_t_times:=rg.t_times;
  413. if not(cs_littlesize in aktglobalswitches) then
  414. begin
  415. rg.t_times:=rg.t_times div case_count_labels(nodes);
  416. if rg.t_times<1 then
  417. rg.t_times:=1;
  418. end;
  419. { first case }
  420. hp:=tbinarynode(right);
  421. while assigned(hp) do
  422. begin
  423. rg.cleartempgen;
  424. firstpass(hp.right);
  425. { searchs max registers }
  426. if hp.right.registers32>registers32 then
  427. registers32:=hp.right.registers32;
  428. if hp.right.registersfpu>registersfpu then
  429. registersfpu:=hp.right.registersfpu;
  430. {$ifdef SUPPORT_MMX}
  431. if hp.right.registersmmx>registersmmx then
  432. registersmmx:=hp.right.registersmmx;
  433. {$endif SUPPORT_MMX}
  434. hp:=tbinarynode(hp.left);
  435. end;
  436. { may be handle else tree }
  437. if assigned(elseblock) then
  438. begin
  439. rg.cleartempgen;
  440. firstpass(elseblock);
  441. if codegenerror then
  442. exit;
  443. if registers32<elseblock.registers32 then
  444. registers32:=elseblock.registers32;
  445. if registersfpu<elseblock.registersfpu then
  446. registersfpu:=elseblock.registersfpu;
  447. {$ifdef SUPPORT_MMX}
  448. if registersmmx<elseblock.registersmmx then
  449. registersmmx:=elseblock.registersmmx;
  450. {$endif SUPPORT_MMX}
  451. end;
  452. rg.t_times:=old_t_times;
  453. { there is one register required for the case expression }
  454. { for 64 bit ints we cheat: the high dword is stored in EDI }
  455. { so we don't need an extra register }
  456. if registers32<1 then registers32:=1;
  457. end;
  458. function tcasenode.getcopy : tnode;
  459. var
  460. p : tcasenode;
  461. begin
  462. p:=tcasenode(inherited getcopy);
  463. if assigned(elseblock) then
  464. p.elseblock:=elseblock.getcopy
  465. else
  466. p.elseblock:=nil;
  467. p.nodes:=copycaserecord(nodes);
  468. getcopy:=p;
  469. end;
  470. procedure tcasenode.insertintolist(l : tnodelist);
  471. begin
  472. end;
  473. function casenodesequal(n1,n2: pcaserecord): boolean;
  474. begin
  475. casenodesequal :=
  476. (not assigned(n1) and not assigned(n2)) or
  477. (assigned(n1) and assigned(n2) and
  478. (n1^._low = n2^._low) and
  479. (n1^._high = n2^._high) and
  480. { the rest of the fields don't matter for equality (JM) }
  481. casenodesequal(n1^.less,n2^.less) and
  482. casenodesequal(n1^.greater,n2^.greater))
  483. end;
  484. function tcasenode.docompare(p: tnode): boolean;
  485. begin
  486. docompare :=
  487. inherited docompare(p) and
  488. casenodesequal(nodes,tcasenode(p).nodes) and
  489. elseblock.isequal(tcasenode(p).elseblock);
  490. end;
  491. begin
  492. csetelementnode:=tsetelementnode;
  493. cinnode:=tinnode;
  494. crangenode:=trangenode;
  495. ccasenode:=tcasenode;
  496. end.
  497. {
  498. $Log$
  499. Revision 1.30 2002-07-23 13:19:40 jonas
  500. * fixed evaluation of expressions with empty sets that are calculated
  501. at compile time
  502. Revision 1.29 2002/07/23 12:34:30 daniel
  503. * Readded old set code. To use it define 'oldset'. Activated by default
  504. for ppc.
  505. Revision 1.28 2002/07/22 11:48:04 daniel
  506. * Sets are now internally sets.
  507. Revision 1.27 2002/07/20 11:57:55 florian
  508. * types.pas renamed to defbase.pas because D6 contains a types
  509. unit so this would conflicts if D6 programms are compiled
  510. + Willamette/SSE2 instructions to assembler added
  511. Revision 1.26 2002/07/06 20:19:25 carl
  512. + generic set handling
  513. Revision 1.25 2002/07/01 18:46:24 peter
  514. * internal linker
  515. * reorganized aasm layer
  516. Revision 1.24 2002/05/18 13:34:10 peter
  517. * readded missing revisions
  518. Revision 1.23 2002/05/16 19:46:39 carl
  519. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  520. + try to fix temp allocation (still in ifdef)
  521. + generic constructor calls
  522. + start of tassembler / tmodulebase class cleanup
  523. Revision 1.21 2002/05/12 16:53:08 peter
  524. * moved entry and exitcode to ncgutil and cgobj
  525. * foreach gets extra argument for passing local data to the
  526. iterator function
  527. * -CR checks also class typecasts at runtime by changing them
  528. into as
  529. * fixed compiler to cycle with the -CR option
  530. * fixed stabs with elf writer, finally the global variables can
  531. be watched
  532. * removed a lot of routines from cga unit and replaced them by
  533. calls to cgobj
  534. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  535. u32bit then the other is typecasted also to u32bit without giving
  536. a rangecheck warning/error.
  537. * fixed pascal calling method with reversing also the high tree in
  538. the parast, detected by tcalcst3 test
  539. Revision 1.20 2002/04/07 13:27:50 carl
  540. + change unit use
  541. Revision 1.19 2002/04/02 17:11:29 peter
  542. * tlocation,treference update
  543. * LOC_CONSTANT added for better constant handling
  544. * secondadd splitted in multiple routines
  545. * location_force_reg added for loading a location to a register
  546. of a specified size
  547. * secondassignment parses now first the right and then the left node
  548. (this is compatible with Kylix). This saves a lot of push/pop especially
  549. with string operations
  550. * adapted some routines to use the new cg methods
  551. Revision 1.18 2002/03/31 20:26:35 jonas
  552. + a_loadfpu_* and a_loadmm_* methods in tcg
  553. * register allocation is now handled by a class and is mostly processor
  554. independent (+rgobj.pas and i386/rgcpu.pas)
  555. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  556. * some small improvements and fixes to the optimizer
  557. * some register allocation fixes
  558. * some fpuvaroffset fixes in the unary minus node
  559. * push/popusedregisters is now called rg.save/restoreusedregisters and
  560. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  561. also better optimizable)
  562. * fixed and optimized register saving/restoring for new/dispose nodes
  563. * LOC_FPU locations now also require their "register" field to be set to
  564. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  565. - list field removed of the tnode class because it's not used currently
  566. and can cause hard-to-find bugs
  567. }