nset.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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. 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. { Warning : This is the first pass for the generic version }
  222. { the only difference is mainly the result location which }
  223. { is changed, compared to the i386 version. }
  224. { ALSO REGISTER ALLOC IS WRONG? }
  225. function tinnode.pass_1 : tnode;
  226. begin
  227. result:=nil;
  228. location.loc:=LOC_REGISTER;
  229. firstpass(right);
  230. firstpass(left);
  231. if codegenerror then
  232. exit;
  233. left_right_max;
  234. { this is not allways true due to optimization }
  235. { but if we don't set this we get problems with optimizing self code }
  236. if tsetdef(right.resulttype.def).settype<>smallset then
  237. procinfo^.flags:=procinfo^.flags or pi_do_call
  238. else
  239. begin
  240. { a smallset needs maybe an misc. register }
  241. if (left.nodetype<>ordconstn) and
  242. not(right.location.loc in [LOC_CREGISTER,LOC_REGISTER]) and
  243. (right.registers32<1) then
  244. inc(registers32);
  245. end;
  246. end;
  247. {*****************************************************************************
  248. TRANGENODE
  249. *****************************************************************************}
  250. constructor trangenode.create(l,r : tnode);
  251. begin
  252. inherited create(rangen,l,r);
  253. end;
  254. function trangenode.det_resulttype : tnode;
  255. var
  256. ct : tconverttype;
  257. begin
  258. result:=nil;
  259. resulttypepass(left);
  260. resulttypepass(right);
  261. set_varstate(left,true);
  262. set_varstate(right,true);
  263. if codegenerror then
  264. exit;
  265. { both types must be compatible }
  266. if not(is_equal(left.resulttype.def,right.resulttype.def)) and
  267. (isconvertable(left.resulttype.def,right.resulttype.def,ct,ordconstn,false)=0) then
  268. CGMessage(type_e_mismatch);
  269. { Check if only when its a constant set }
  270. if (left.nodetype=ordconstn) and (right.nodetype=ordconstn) then
  271. begin
  272. { upper limit must be greater or equal than lower limit }
  273. if (tordconstnode(left).value>tordconstnode(right).value) and
  274. ((tordconstnode(left).value<0) or (tordconstnode(right).value>=0)) then
  275. CGMessage(cg_e_upper_lower_than_lower);
  276. end;
  277. resulttype:=left.resulttype;
  278. end;
  279. function trangenode.pass_1 : tnode;
  280. begin
  281. result:=nil;
  282. firstpass(left);
  283. firstpass(right);
  284. if codegenerror then
  285. exit;
  286. left_right_max;
  287. location_copy(location,left.location);
  288. end;
  289. {*****************************************************************************
  290. Case Helpers
  291. *****************************************************************************}
  292. function case_count_labels(root : pcaserecord) : longint;
  293. var
  294. _l : longint;
  295. procedure count(p : pcaserecord);
  296. begin
  297. inc(_l);
  298. if assigned(p^.less) then
  299. count(p^.less);
  300. if assigned(p^.greater) then
  301. count(p^.greater);
  302. end;
  303. begin
  304. _l:=0;
  305. count(root);
  306. case_count_labels:=_l;
  307. end;
  308. {$ifdef int64funcresok}
  309. function case_get_max(root : pcaserecord) : tconstexprint;
  310. {$else int64funcresok}
  311. function case_get_max(root : pcaserecord) : longint;
  312. {$endif int64funcresok}
  313. var
  314. hp : pcaserecord;
  315. begin
  316. hp:=root;
  317. while assigned(hp^.greater) do
  318. hp:=hp^.greater;
  319. case_get_max:=hp^._high;
  320. end;
  321. {$ifdef int64funcresok}
  322. function case_get_min(root : pcaserecord) : tconstexprint;
  323. {$else int64funcresok}
  324. function case_get_min(root : pcaserecord) : longint;
  325. {$endif int64funcresok}
  326. var
  327. hp : pcaserecord;
  328. begin
  329. hp:=root;
  330. while assigned(hp^.less) do
  331. hp:=hp^.less;
  332. case_get_min:=hp^._low;
  333. end;
  334. procedure deletecaselabels(p : pcaserecord);
  335. begin
  336. if assigned(p^.greater) then
  337. deletecaselabels(p^.greater);
  338. if assigned(p^.less) then
  339. deletecaselabels(p^.less);
  340. dispose(p);
  341. end;
  342. function copycaserecord(p : pcaserecord) : pcaserecord;
  343. var
  344. n : pcaserecord;
  345. begin
  346. new(n);
  347. n^:=p^;
  348. if assigned(p^.greater) then
  349. n^.greater:=copycaserecord(p^.greater);
  350. if assigned(p^.less) then
  351. n^.less:=copycaserecord(p^.less);
  352. copycaserecord:=n;
  353. end;
  354. {*****************************************************************************
  355. TCASENODE
  356. *****************************************************************************}
  357. constructor tcasenode.create(l,r : tnode;n : pcaserecord);
  358. begin
  359. inherited create(casen,l,r);
  360. nodes:=n;
  361. elseblock:=nil;
  362. set_file_line(l);
  363. end;
  364. destructor tcasenode.destroy;
  365. begin
  366. elseblock.free;
  367. deletecaselabels(nodes);
  368. inherited destroy;
  369. end;
  370. function tcasenode.det_resulttype : tnode;
  371. begin
  372. result:=nil;
  373. resulttype:=voidtype;
  374. end;
  375. function tcasenode.pass_1 : tnode;
  376. var
  377. old_t_times : longint;
  378. hp : tbinarynode;
  379. begin
  380. result:=nil;
  381. { evalutes the case expression }
  382. rg.cleartempgen;
  383. firstpass(left);
  384. set_varstate(left,true);
  385. if codegenerror then
  386. exit;
  387. registers32:=left.registers32;
  388. registersfpu:=left.registersfpu;
  389. {$ifdef SUPPORT_MMX}
  390. registersmmx:=left.registersmmx;
  391. {$endif SUPPORT_MMX}
  392. { walk through all instructions }
  393. { estimates the repeat of each instruction }
  394. old_t_times:=rg.t_times;
  395. if not(cs_littlesize in aktglobalswitches) then
  396. begin
  397. rg.t_times:=rg.t_times div case_count_labels(nodes);
  398. if rg.t_times<1 then
  399. rg.t_times:=1;
  400. end;
  401. { first case }
  402. hp:=tbinarynode(right);
  403. while assigned(hp) do
  404. begin
  405. rg.cleartempgen;
  406. firstpass(hp.right);
  407. { searchs max registers }
  408. if hp.right.registers32>registers32 then
  409. registers32:=hp.right.registers32;
  410. if hp.right.registersfpu>registersfpu then
  411. registersfpu:=hp.right.registersfpu;
  412. {$ifdef SUPPORT_MMX}
  413. if hp.right.registersmmx>registersmmx then
  414. registersmmx:=hp.right.registersmmx;
  415. {$endif SUPPORT_MMX}
  416. hp:=tbinarynode(hp.left);
  417. end;
  418. { may be handle else tree }
  419. if assigned(elseblock) then
  420. begin
  421. rg.cleartempgen;
  422. firstpass(elseblock);
  423. if codegenerror then
  424. exit;
  425. if registers32<elseblock.registers32 then
  426. registers32:=elseblock.registers32;
  427. if registersfpu<elseblock.registersfpu then
  428. registersfpu:=elseblock.registersfpu;
  429. {$ifdef SUPPORT_MMX}
  430. if registersmmx<elseblock.registersmmx then
  431. registersmmx:=elseblock.registersmmx;
  432. {$endif SUPPORT_MMX}
  433. end;
  434. rg.t_times:=old_t_times;
  435. { there is one register required for the case expression }
  436. { for 64 bit ints we cheat: the high dword is stored in EDI }
  437. { so we don't need an extra register }
  438. if registers32<1 then registers32:=1;
  439. end;
  440. function tcasenode.getcopy : tnode;
  441. var
  442. p : tcasenode;
  443. begin
  444. p:=tcasenode(inherited getcopy);
  445. if assigned(elseblock) then
  446. p.elseblock:=elseblock.getcopy
  447. else
  448. p.elseblock:=nil;
  449. p.nodes:=copycaserecord(nodes);
  450. getcopy:=p;
  451. end;
  452. procedure tcasenode.insertintolist(l : tnodelist);
  453. begin
  454. end;
  455. function casenodesequal(n1,n2: pcaserecord): boolean;
  456. begin
  457. casenodesequal :=
  458. (not assigned(n1) and not assigned(n2)) or
  459. (assigned(n1) and assigned(n2) and
  460. (n1^._low = n2^._low) and
  461. (n1^._high = n2^._high) and
  462. { the rest of the fields don't matter for equality (JM) }
  463. casenodesequal(n1^.less,n2^.less) and
  464. casenodesequal(n1^.greater,n2^.greater))
  465. end;
  466. function tcasenode.docompare(p: tnode): boolean;
  467. begin
  468. docompare :=
  469. inherited docompare(p) and
  470. casenodesequal(nodes,tcasenode(p).nodes) and
  471. elseblock.isequal(tcasenode(p).elseblock);
  472. end;
  473. begin
  474. csetelementnode:=tsetelementnode;
  475. cinnode:=tinnode;
  476. crangenode:=trangenode;
  477. ccasenode:=tcasenode;
  478. end.
  479. {
  480. $Log$
  481. Revision 1.27 2002-07-20 11:57:55 florian
  482. * types.pas renamed to defbase.pas because D6 contains a types
  483. unit so this would conflicts if D6 programms are compiled
  484. + Willamette/SSE2 instructions to assembler added
  485. Revision 1.26 2002/07/06 20:19:25 carl
  486. + generic set handling
  487. Revision 1.25 2002/07/01 18:46:24 peter
  488. * internal linker
  489. * reorganized aasm layer
  490. Revision 1.24 2002/05/18 13:34:10 peter
  491. * readded missing revisions
  492. Revision 1.23 2002/05/16 19:46:39 carl
  493. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  494. + try to fix temp allocation (still in ifdef)
  495. + generic constructor calls
  496. + start of tassembler / tmodulebase class cleanup
  497. Revision 1.21 2002/05/12 16:53:08 peter
  498. * moved entry and exitcode to ncgutil and cgobj
  499. * foreach gets extra argument for passing local data to the
  500. iterator function
  501. * -CR checks also class typecasts at runtime by changing them
  502. into as
  503. * fixed compiler to cycle with the -CR option
  504. * fixed stabs with elf writer, finally the global variables can
  505. be watched
  506. * removed a lot of routines from cga unit and replaced them by
  507. calls to cgobj
  508. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  509. u32bit then the other is typecasted also to u32bit without giving
  510. a rangecheck warning/error.
  511. * fixed pascal calling method with reversing also the high tree in
  512. the parast, detected by tcalcst3 test
  513. Revision 1.20 2002/04/07 13:27:50 carl
  514. + change unit use
  515. Revision 1.19 2002/04/02 17:11:29 peter
  516. * tlocation,treference update
  517. * LOC_CONSTANT added for better constant handling
  518. * secondadd splitted in multiple routines
  519. * location_force_reg added for loading a location to a register
  520. of a specified size
  521. * secondassignment parses now first the right and then the left node
  522. (this is compatible with Kylix). This saves a lot of push/pop especially
  523. with string operations
  524. * adapted some routines to use the new cg methods
  525. Revision 1.18 2002/03/31 20:26:35 jonas
  526. + a_loadfpu_* and a_loadmm_* methods in tcg
  527. * register allocation is now handled by a class and is mostly processor
  528. independent (+rgobj.pas and i386/rgcpu.pas)
  529. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  530. * some small improvements and fixes to the optimizer
  531. * some register allocation fixes
  532. * some fpuvaroffset fixes in the unary minus node
  533. * push/popusedregisters is now called rg.save/restoreusedregisters and
  534. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  535. also better optimizable)
  536. * fixed and optimized register saving/restoring for new/dispose nodes
  537. * LOC_FPU locations now also require their "register" field to be set to
  538. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  539. - list field removed of the tnode class because it's not used currently
  540. and can cause hard-to-find bugs
  541. }