nset.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. {
  2. $Id$
  3. Copyright (c) 2000-2001 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,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. 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,types,
  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. type
  140. byteset = set of byte;
  141. var
  142. t : tnode;
  143. pst : pconstset;
  144. function createsetconst(psd : tsetdef) : pconstset;
  145. var
  146. pcs : pconstset;
  147. pes : tenumsym;
  148. i : longint;
  149. begin
  150. new(pcs);
  151. case psd.elementtype.def.deftype of
  152. enumdef :
  153. begin
  154. pes:=tenumsym(tenumdef(psd.elementtype.def).firstenum);
  155. while assigned(pes) do
  156. begin
  157. pcs^[pes.value div 8]:=pcs^[pes.value div 8] or (1 shl (pes.value mod 8));
  158. pes:=pes.nextenum;
  159. end;
  160. end;
  161. orddef :
  162. begin
  163. for i:=torddef(psd.elementtype.def).low to torddef(psd.elementtype.def).high do
  164. begin
  165. pcs^[i div 8]:=pcs^[i div 8] or (1 shl (i mod 8));
  166. end;
  167. end;
  168. end;
  169. createsetconst:=pcs;
  170. end;
  171. begin
  172. result:=nil;
  173. resulttype:=booltype;
  174. resulttypepass(right);
  175. set_varstate(right,true);
  176. if codegenerror then
  177. exit;
  178. { Convert array constructor first to set }
  179. if is_array_constructor(right.resulttype.def) then
  180. begin
  181. arrayconstructor_to_set(right);
  182. firstpass(right);
  183. if codegenerror then
  184. exit;
  185. end;
  186. if right.resulttype.def.deftype<>setdef then
  187. CGMessage(sym_e_set_expected);
  188. if (right.nodetype=typen) then
  189. begin
  190. { we need to create a setconstn }
  191. pst:=createsetconst(tsetdef(ttypenode(right).resulttype.def));
  192. t:=csetconstnode.create(pst,ttypenode(right).resulttype);
  193. dispose(pst);
  194. right.free;
  195. right:=t;
  196. end;
  197. resulttypepass(left);
  198. set_varstate(left,true);
  199. if codegenerror then
  200. exit;
  201. { type conversion/check }
  202. if assigned(tsetdef(right.resulttype.def).elementtype.def) then
  203. inserttypeconv(left,tsetdef(right.resulttype.def).elementtype);
  204. { empty set then return false }
  205. if not assigned(tsetdef(right.resulttype.def).elementtype.def) then
  206. begin
  207. t:=cordconstnode.create(0,booltype);
  208. resulttypepass(t);
  209. result:=t;
  210. exit;
  211. end;
  212. { constant evaulation }
  213. if (left.nodetype=ordconstn) and (right.nodetype=setconstn) then
  214. begin
  215. t:=cordconstnode.create(byte(tordconstnode(left).value in byteset(tsetconstnode(right).value_set^)),booltype);
  216. resulttypepass(t);
  217. result:=t;
  218. exit;
  219. end;
  220. end;
  221. function tinnode.pass_1 : tnode;
  222. begin
  223. result:=nil;
  224. location.loc:=LOC_FLAGS;
  225. firstpass(right);
  226. firstpass(left);
  227. if codegenerror then
  228. exit;
  229. left_right_max;
  230. { this is not allways true due to optimization }
  231. { but if we don't set this we get problems with optimizing self code }
  232. if tsetdef(right.resulttype.def).settype<>smallset then
  233. procinfo^.flags:=procinfo^.flags or pi_do_call
  234. else
  235. begin
  236. { a smallset needs maybe an misc. register }
  237. if (left.nodetype<>ordconstn) and
  238. not(right.location.loc in [LOC_CREGISTER,LOC_REGISTER]) and
  239. (right.registers32<1) then
  240. inc(registers32);
  241. end;
  242. end;
  243. {*****************************************************************************
  244. TRANGENODE
  245. *****************************************************************************}
  246. constructor trangenode.create(l,r : tnode);
  247. begin
  248. inherited create(rangen,l,r);
  249. end;
  250. function trangenode.det_resulttype : tnode;
  251. var
  252. ct : tconverttype;
  253. begin
  254. result:=nil;
  255. resulttypepass(left);
  256. resulttypepass(right);
  257. set_varstate(left,true);
  258. set_varstate(right,true);
  259. if codegenerror then
  260. exit;
  261. { both types must be compatible }
  262. if not(is_equal(left.resulttype.def,right.resulttype.def)) and
  263. (isconvertable(left.resulttype.def,right.resulttype.def,ct,ordconstn,false)=0) then
  264. CGMessage(type_e_mismatch);
  265. { Check if only when its a constant set }
  266. if (left.nodetype=ordconstn) and (right.nodetype=ordconstn) then
  267. begin
  268. { upper limit must be greater or equal than lower limit }
  269. if (tordconstnode(left).value>tordconstnode(right).value) and
  270. ((tordconstnode(left).value<0) or (tordconstnode(right).value>=0)) then
  271. CGMessage(cg_e_upper_lower_than_lower);
  272. end;
  273. resulttype:=left.resulttype;
  274. end;
  275. function trangenode.pass_1 : tnode;
  276. begin
  277. result:=nil;
  278. firstpass(left);
  279. firstpass(right);
  280. if codegenerror then
  281. exit;
  282. left_right_max;
  283. location_copy(location,left.location);
  284. end;
  285. {*****************************************************************************
  286. Case Helpers
  287. *****************************************************************************}
  288. function case_count_labels(root : pcaserecord) : longint;
  289. var
  290. _l : longint;
  291. procedure count(p : pcaserecord);
  292. begin
  293. inc(_l);
  294. if assigned(p^.less) then
  295. count(p^.less);
  296. if assigned(p^.greater) then
  297. count(p^.greater);
  298. end;
  299. begin
  300. _l:=0;
  301. count(root);
  302. case_count_labels:=_l;
  303. end;
  304. {$ifdef int64funcresok}
  305. function case_get_max(root : pcaserecord) : tconstexprint;
  306. {$else int64funcresok}
  307. function case_get_max(root : pcaserecord) : longint;
  308. {$endif int64funcresok}
  309. var
  310. hp : pcaserecord;
  311. begin
  312. hp:=root;
  313. while assigned(hp^.greater) do
  314. hp:=hp^.greater;
  315. case_get_max:=hp^._high;
  316. end;
  317. {$ifdef int64funcresok}
  318. function case_get_min(root : pcaserecord) : tconstexprint;
  319. {$else int64funcresok}
  320. function case_get_min(root : pcaserecord) : longint;
  321. {$endif int64funcresok}
  322. var
  323. hp : pcaserecord;
  324. begin
  325. hp:=root;
  326. while assigned(hp^.less) do
  327. hp:=hp^.less;
  328. case_get_min:=hp^._low;
  329. end;
  330. procedure deletecaselabels(p : pcaserecord);
  331. begin
  332. if assigned(p^.greater) then
  333. deletecaselabels(p^.greater);
  334. if assigned(p^.less) then
  335. deletecaselabels(p^.less);
  336. dispose(p);
  337. end;
  338. function copycaserecord(p : pcaserecord) : pcaserecord;
  339. var
  340. n : pcaserecord;
  341. begin
  342. new(n);
  343. n^:=p^;
  344. if assigned(p^.greater) then
  345. n^.greater:=copycaserecord(p^.greater);
  346. if assigned(p^.less) then
  347. n^.less:=copycaserecord(p^.less);
  348. copycaserecord:=n;
  349. end;
  350. {*****************************************************************************
  351. TCASENODE
  352. *****************************************************************************}
  353. constructor tcasenode.create(l,r : tnode;n : pcaserecord);
  354. begin
  355. inherited create(casen,l,r);
  356. nodes:=n;
  357. elseblock:=nil;
  358. set_file_line(l);
  359. end;
  360. destructor tcasenode.destroy;
  361. begin
  362. elseblock.free;
  363. deletecaselabels(nodes);
  364. inherited destroy;
  365. end;
  366. function tcasenode.det_resulttype : tnode;
  367. begin
  368. result:=nil;
  369. resulttype:=voidtype;
  370. end;
  371. function tcasenode.pass_1 : tnode;
  372. var
  373. old_t_times : longint;
  374. hp : tbinarynode;
  375. begin
  376. result:=nil;
  377. { evalutes the case expression }
  378. rg.cleartempgen;
  379. firstpass(left);
  380. set_varstate(left,true);
  381. if codegenerror then
  382. exit;
  383. registers32:=left.registers32;
  384. registersfpu:=left.registersfpu;
  385. {$ifdef SUPPORT_MMX}
  386. registersmmx:=left.registersmmx;
  387. {$endif SUPPORT_MMX}
  388. { walk through all instructions }
  389. { estimates the repeat of each instruction }
  390. old_t_times:=rg.t_times;
  391. if not(cs_littlesize in aktglobalswitches) then
  392. begin
  393. rg.t_times:=rg.t_times div case_count_labels(nodes);
  394. if rg.t_times<1 then
  395. rg.t_times:=1;
  396. end;
  397. { first case }
  398. hp:=tbinarynode(right);
  399. while assigned(hp) do
  400. begin
  401. rg.cleartempgen;
  402. firstpass(hp.right);
  403. { searchs max registers }
  404. if hp.right.registers32>registers32 then
  405. registers32:=hp.right.registers32;
  406. if hp.right.registersfpu>registersfpu then
  407. registersfpu:=hp.right.registersfpu;
  408. {$ifdef SUPPORT_MMX}
  409. if hp.right.registersmmx>registersmmx then
  410. registersmmx:=hp.right.registersmmx;
  411. {$endif SUPPORT_MMX}
  412. hp:=tbinarynode(hp.left);
  413. end;
  414. { may be handle else tree }
  415. if assigned(elseblock) then
  416. begin
  417. rg.cleartempgen;
  418. firstpass(elseblock);
  419. if codegenerror then
  420. exit;
  421. if registers32<elseblock.registers32 then
  422. registers32:=elseblock.registers32;
  423. if registersfpu<elseblock.registersfpu then
  424. registersfpu:=elseblock.registersfpu;
  425. {$ifdef SUPPORT_MMX}
  426. if registersmmx<elseblock.registersmmx then
  427. registersmmx:=elseblock.registersmmx;
  428. {$endif SUPPORT_MMX}
  429. end;
  430. rg.t_times:=old_t_times;
  431. { there is one register required for the case expression }
  432. { for 64 bit ints we cheat: the high dword is stored in EDI }
  433. { so we don't need an extra register }
  434. if registers32<1 then registers32:=1;
  435. end;
  436. function tcasenode.getcopy : tnode;
  437. var
  438. p : tcasenode;
  439. begin
  440. p:=tcasenode(inherited getcopy);
  441. if assigned(elseblock) then
  442. p.elseblock:=elseblock.getcopy
  443. else
  444. p.elseblock:=nil;
  445. p.nodes:=copycaserecord(nodes);
  446. getcopy:=p;
  447. end;
  448. procedure tcasenode.insertintolist(l : tnodelist);
  449. begin
  450. end;
  451. function casenodesequal(n1,n2: pcaserecord): boolean;
  452. begin
  453. casenodesequal :=
  454. (not assigned(n1) and not assigned(n2)) or
  455. (assigned(n1) and assigned(n2) and
  456. (n1^._low = n2^._low) and
  457. (n1^._high = n2^._high) and
  458. { the rest of the fields don't matter for equality (JM) }
  459. casenodesequal(n1^.less,n2^.less) and
  460. casenodesequal(n1^.greater,n2^.greater))
  461. end;
  462. function tcasenode.docompare(p: tnode): boolean;
  463. begin
  464. docompare :=
  465. inherited docompare(p) and
  466. casenodesequal(nodes,tcasenode(p).nodes) and
  467. elseblock.isequal(tcasenode(p).elseblock);
  468. end;
  469. begin
  470. csetelementnode:=tsetelementnode;
  471. cinnode:=tinnode;
  472. crangenode:=trangenode;
  473. ccasenode:=tcasenode;
  474. end.
  475. {
  476. $Log$
  477. Revision 1.23 2002-05-16 19:46:39 carl
  478. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  479. + try to fix temp allocation (still in ifdef)
  480. + generic constructor calls
  481. + start of tassembler / tmodulebase class cleanup
  482. Revision 1.21 2002/05/12 16:53:08 peter
  483. * moved entry and exitcode to ncgutil and cgobj
  484. * foreach gets extra argument for passing local data to the
  485. iterator function
  486. * -CR checks also class typecasts at runtime by changing them
  487. into as
  488. * fixed compiler to cycle with the -CR option
  489. * fixed stabs with elf writer, finally the global variables can
  490. be watched
  491. * removed a lot of routines from cga unit and replaced them by
  492. calls to cgobj
  493. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  494. u32bit then the other is typecasted also to u32bit without giving
  495. a rangecheck warning/error.
  496. * fixed pascal calling method with reversing also the high tree in
  497. the parast, detected by tcalcst3 test
  498. Revision 1.20 2002/04/07 13:27:50 carl
  499. + change unit use
  500. Revision 1.19 2002/04/02 17:11:29 peter
  501. * tlocation,treference update
  502. * LOC_CONSTANT added for better constant handling
  503. * secondadd splitted in multiple routines
  504. * location_force_reg added for loading a location to a register
  505. of a specified size
  506. * secondassignment parses now first the right and then the left node
  507. (this is compatible with Kylix). This saves a lot of push/pop especially
  508. with string operations
  509. * adapted some routines to use the new cg methods
  510. Revision 1.18 2002/03/31 20:26:35 jonas
  511. + a_loadfpu_* and a_loadmm_* methods in tcg
  512. * register allocation is now handled by a class and is mostly processor
  513. independent (+rgobj.pas and i386/rgcpu.pas)
  514. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  515. * some small improvements and fixes to the optimizer
  516. * some register allocation fixes
  517. * some fpuvaroffset fixes in the unary minus node
  518. * push/popusedregisters is now called rg.save/restoreusedregisters and
  519. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  520. also better optimizable)
  521. * fixed and optimized register saving/restoring for new/dispose nodes
  522. * LOC_FPU locations now also require their "register" field to be set to
  523. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  524. - list field removed of the tnode class because it's not used currently
  525. and can cause hard-to-find bugs
  526. Revision 1.17 2001/12/06 17:57:35 florian
  527. + parasym to tparaitem added
  528. Revision 1.16 2001/10/12 13:51:51 jonas
  529. * fixed internalerror(10) due to previous fpu overflow fixes ("merged")
  530. * fixed bug in n386add (introduced after compilerproc changes for string
  531. operations) where calcregisters wasn't called for shortstring addnodes
  532. * NOTE: from now on, the location of a binary node must now always be set
  533. before you call calcregisters() for it
  534. Revision 1.15 2001/09/02 21:12:07 peter
  535. * move class of definitions into type section for delphi
  536. Revision 1.14 2001/08/26 13:36:43 florian
  537. * some cg reorganisation
  538. * some PPC updates
  539. Revision 1.13 2001/04/13 01:22:10 peter
  540. * symtable change to classes
  541. * range check generation and errors fixed, make cycle DEBUG=1 works
  542. * memory leaks fixed
  543. Revision 1.12 2001/04/02 21:20:31 peter
  544. * resulttype rewrite
  545. Revision 1.11 2000/12/31 11:14:11 jonas
  546. + implemented/fixed docompare() mathods for all nodes (not tested)
  547. + nopt.pas, nadd.pas, i386/n386opt.pas: optimized nodes for adding strings
  548. and constant strings/chars together
  549. * n386add.pas: don't copy temp strings (of size 256) to another temp string
  550. when adding
  551. Revision 1.10 2000/12/18 17:44:26 jonas
  552. * more int64 case fixes
  553. Revision 1.9 2000/11/29 00:30:34 florian
  554. * unused units removed from uses clause
  555. * some changes for widestrings
  556. Revision 1.8 2000/11/04 14:25:20 florian
  557. + merged Attila's changes for interfaces, not tested yet
  558. Revision 1.7 2000/10/31 22:02:49 peter
  559. * symtable splitted, no real code changes
  560. Revision 1.6 2000/10/21 18:16:11 florian
  561. * a lot of changes:
  562. - basic dyn. array support
  563. - basic C++ support
  564. - some work for interfaces done
  565. ....
  566. Revision 1.5 2000/10/14 10:14:51 peter
  567. * moehrendorf oct 2000 rewrite
  568. Revision 1.4 2000/10/01 19:48:25 peter
  569. * lot of compile updates for cg11
  570. Revision 1.3 2000/09/27 18:14:31 florian
  571. * fixed a lot of syntax errors in the n*.pas stuff
  572. Revision 1.2 2000/09/24 20:17:44 florian
  573. * more conversion work done
  574. Revision 1.1 2000/09/24 19:38:39 florian
  575. * initial implementation
  576. }