nset.pas 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  1. {
  2. Copyright (c) 2000-2002 by Florian Klaempfl
  3. Type checking and register allocation for set/case nodes
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit nset;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,constexp,
  22. node,globtype,globals,
  23. aasmbase,ncon,nflw,symtype;
  24. type
  25. TLabelType = (ltOrdinal, ltConstString);
  26. pcaselabel = ^tcaselabel;
  27. tcaselabel = record
  28. { unique blockid }
  29. blockid : longint;
  30. { left and right tree node }
  31. less,
  32. greater : pcaselabel;
  33. labellabel : TAsmLabel;
  34. { range type }
  35. case label_type : TLabelType of
  36. ltOrdinal:
  37. (
  38. _low,
  39. _high : TConstExprInt;
  40. );
  41. ltConstString:
  42. (
  43. _low_str,
  44. _high_str : tstringconstnode;
  45. );
  46. end;
  47. pcaseblock = ^tcaseblock;
  48. tcaseblock = record
  49. { label (only used in pass_generate_code) }
  50. blocklabel : tasmlabel;
  51. { shortcut - set to true if blocklabel isn't actually unique to the
  52. case block due to one of the following conditions:
  53. - if the node contains a jump, then the label is set to that jump's destination,
  54. - if the node is empty, the label is set to the end label. }
  55. shortcut: Boolean;
  56. statementlabel : tlabelnode;
  57. { instructions }
  58. statement : tnode;
  59. end;
  60. tsetelementnode = class(tbinarynode)
  61. constructor create(l,r : tnode);virtual;
  62. function pass_typecheck:tnode;override;
  63. function pass_1 : tnode;override;
  64. end;
  65. tsetelementnodeclass = class of tsetelementnode;
  66. tinnode = class(tbinopnode)
  67. constructor create(l,r : tnode);virtual;reintroduce;
  68. function pass_typecheck:tnode;override;
  69. function simplify(forinline : boolean):tnode;override;
  70. function pass_1 : tnode;override;
  71. end;
  72. tinnodeclass = class of tinnode;
  73. trangenode = class(tbinarynode)
  74. constructor create(l,r : tnode);virtual;
  75. function pass_typecheck:tnode;override;
  76. function pass_1 : tnode;override;
  77. end;
  78. trangenodeclass = class of trangenode;
  79. tcasenode = class(tunarynode)
  80. labels : pcaselabel;
  81. blocks : TFPList;
  82. elseblock : tnode;
  83. constructor create(l:tnode);virtual;
  84. destructor destroy;override;
  85. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  86. procedure ppuwrite(ppufile:tcompilerppufile);override;
  87. procedure buildderefimpl;override;
  88. procedure derefimpl;override;
  89. function dogetcopy : tnode;override;
  90. procedure printnodetree(var t:text);override;
  91. procedure insertintolist(l : tnodelist);override;
  92. function pass_typecheck:tnode;override;
  93. function pass_1 : tnode;override;
  94. function simplify(forinline:boolean):tnode;override;
  95. function docompare(p: tnode): boolean; override;
  96. procedure addlabel(blockid:longint;const l,h : TConstExprInt); overload;
  97. procedure addlabel(blockid:longint;l,h : tstringconstnode); overload;
  98. procedure addblock(blockid:longint;instr:tnode);
  99. procedure addelseblock(instr:tnode);
  100. end;
  101. tcasenodeclass = class of tcasenode;
  102. var
  103. csetelementnode : tsetelementnodeclass = tsetelementnode;
  104. cinnode : tinnodeclass = tinnode;
  105. crangenode : trangenodeclass = trangenode;
  106. ccasenode : tcasenodeclass = tcasenode;
  107. { counts the labels }
  108. function case_count_labels(root : pcaselabel) : longint;
  109. { Returns the true count in a case block, which includes each individual
  110. value in a range (e.g. "0..2" counts as 3) }
  111. function case_true_count(root : pcaselabel) : longint;
  112. { searches the highest label }
  113. function case_get_max(root : pcaselabel) : tconstexprint;
  114. { searches the lowest label }
  115. function case_get_min(root : pcaselabel) : tconstexprint;
  116. implementation
  117. uses
  118. verbose,
  119. symconst,symdef,symsym,symtable,defutil,defcmp,
  120. htypechk,pass_1,
  121. nadd,nbas,ncnv,nld,cgbase;
  122. {*****************************************************************************
  123. TSETELEMENTNODE
  124. *****************************************************************************}
  125. constructor tsetelementnode.create(l,r : tnode);
  126. begin
  127. inherited create(setelementn,l,r);
  128. end;
  129. function tsetelementnode.pass_typecheck:tnode;
  130. begin
  131. result:=nil;
  132. typecheckpass(left);
  133. if assigned(right) then
  134. typecheckpass(right);
  135. set_varstate(left,vs_read,[vsf_must_be_valid]);
  136. if codegenerror then
  137. exit;
  138. resultdef:=left.resultdef;
  139. end;
  140. function tsetelementnode.pass_1 : tnode;
  141. begin
  142. result:=nil;
  143. firstpass(left);
  144. if assigned(right) then
  145. firstpass(right);
  146. if codegenerror then
  147. exit;
  148. expectloc:=left.expectloc;
  149. end;
  150. {*****************************************************************************
  151. TINNODE
  152. *****************************************************************************}
  153. constructor tinnode.create(l,r : tnode);
  154. begin
  155. inherited create(inn,l,r);
  156. end;
  157. function tinnode.pass_typecheck:tnode;
  158. var
  159. t : tnode;
  160. function createsetconst(psd : tsetdef) : pconstset;
  161. var
  162. pcs : pconstset;
  163. i : longint;
  164. begin
  165. new(pcs);
  166. case psd.elementdef.typ of
  167. enumdef :
  168. begin
  169. for i := 0 to tenumdef(psd.elementdef).symtable.SymList.Count - 1 do
  170. begin
  171. include(pcs^,tenumsym(tenumdef(psd.elementdef).symtable.SymList[i]).value);
  172. end;
  173. end;
  174. orddef :
  175. begin
  176. for i:=int64(torddef(psd.elementdef).low) to int64(torddef(psd.elementdef).high) do
  177. include(pcs^,i);
  178. end;
  179. end;
  180. createsetconst:=pcs;
  181. end;
  182. begin
  183. result:=nil;
  184. resultdef:=pasbool1type;
  185. typecheckpass(right);
  186. set_varstate(right,vs_read,[vsf_must_be_valid]);
  187. if codegenerror then
  188. exit;
  189. { Convert array constructor first to set }
  190. if is_array_constructor(right.resultdef) then
  191. begin
  192. arrayconstructor_to_set(right);
  193. firstpass(right);
  194. if codegenerror then
  195. exit;
  196. end;
  197. typecheckpass(left);
  198. set_varstate(left,vs_read,[vsf_must_be_valid]);
  199. if codegenerror then
  200. exit;
  201. if not assigned(left.resultdef) then
  202. internalerror(20021126);
  203. t:=self;
  204. if isbinaryoverloaded(t,[]) then
  205. begin
  206. result:=t;
  207. exit;
  208. end;
  209. if right.resultdef.typ<>setdef then
  210. CGMessage(sym_e_set_expected);
  211. if codegenerror then
  212. exit;
  213. if (m_tp7 in current_settings.modeswitches) then
  214. begin
  215. { insert a hint that a range check error might occur on non-byte
  216. elements with the in operator.
  217. }
  218. if (
  219. (left.resultdef.typ = orddef) and not
  220. (torddef(left.resultdef).ordtype in [s8bit,u8bit,uchar,pasbool1,pasbool8,bool8bit])
  221. )
  222. or
  223. (
  224. (left.resultdef.typ = enumdef) and
  225. (tenumdef(left.resultdef).maxval > 255)
  226. )
  227. then
  228. CGMessage(type_h_in_range_check);
  229. { type conversion/check }
  230. if assigned(tsetdef(right.resultdef).elementdef) then
  231. inserttypeconv(left,tsetdef(right.resultdef).elementdef);
  232. end
  233. else if not is_ordinal(left.resultdef) or (left.resultdef.size > u32inttype.size) then
  234. begin
  235. CGMessage(type_h_in_range_check);
  236. if is_signed(left.resultdef) then
  237. inserttypeconv(left,s32inttype)
  238. else
  239. inserttypeconv(left,u32inttype);
  240. end
  241. else if assigned(tsetdef(right.resultdef).elementdef) and
  242. not(is_integer(tsetdef(right.resultdef).elementdef) and
  243. is_integer(left.resultdef)) then
  244. { Type conversion to check things like 'char in set_of_byte'. }
  245. { Can't use is_subequal because that will fail for }
  246. { 'widechar in set_of_char' }
  247. { Can't use the type conversion for integers because then }
  248. { "longint in set_of_byte" will give a range check error }
  249. { instead of false }
  250. inserttypeconv(left,tsetdef(right.resultdef).elementdef);
  251. { empty set then return false }
  252. if not assigned(tsetdef(right.resultdef).elementdef) or
  253. ((right.nodetype = setconstn) and
  254. (tnormalset(tsetconstnode(right).value_set^) = [])) then
  255. begin
  256. t:=cordconstnode.create(0,pasbool1type,false);
  257. typecheckpass(t);
  258. result:=t;
  259. exit;
  260. end;
  261. result:=simplify(false);
  262. end;
  263. function tinnode.simplify(forinline : boolean):tnode;
  264. var
  265. t : tnode;
  266. begin
  267. result:=nil;
  268. { constant evaluation }
  269. if (left.nodetype=ordconstn) then
  270. begin
  271. if (right.nodetype=setconstn) then
  272. begin
  273. { tordconstnode.value is int64 -> signed -> the expression }
  274. { below will be converted to longint on 32 bit systems due }
  275. { to the rule above -> will give range check error if }
  276. { value > high(longint) if we don't take the signedness }
  277. { into account }
  278. if Tordconstnode(left).value.signed then
  279. t:=cordconstnode.create(byte(tordconstnode(left).value.svalue in Tsetconstnode(right).value_set^),
  280. pasbool1type,true)
  281. else
  282. t:=cordconstnode.create(byte(tordconstnode(left).value.uvalue in Tsetconstnode(right).value_set^),
  283. pasbool1type,true);
  284. typecheckpass(t);
  285. result:=t;
  286. exit;
  287. end
  288. else
  289. begin
  290. if (Tordconstnode(left).value<int64(tsetdef(right.resultdef).setbase)) or
  291. (Tordconstnode(left).value>int64(Tsetdef(right.resultdef).setmax)) then
  292. begin
  293. t:=cordconstnode.create(0, pasbool1type, true);
  294. typecheckpass(t);
  295. result:=t;
  296. exit;
  297. end;
  298. end;
  299. end;
  300. end;
  301. function tinnode.pass_1 : tnode;
  302. begin
  303. result:=nil;
  304. expectloc:=LOC_REGISTER;
  305. firstpass(right);
  306. firstpass(left);
  307. if codegenerror then
  308. exit;
  309. end;
  310. {*****************************************************************************
  311. TRANGENODE
  312. *****************************************************************************}
  313. constructor trangenode.create(l,r : tnode);
  314. var
  315. value: string;
  316. begin
  317. { if right is char and left is string then }
  318. { right should be treated as one-symbol string }
  319. if is_conststringnode(l) and is_constcharnode(r) then
  320. begin
  321. value := char(tordconstnode(r).value.uvalue) + ''#0;
  322. r.free;
  323. r := cstringconstnode.createstr(value);
  324. do_typecheckpass(r);
  325. end;
  326. inherited create(rangen,l,r);
  327. end;
  328. function trangenode.pass_typecheck : tnode;
  329. begin
  330. result:=nil;
  331. typecheckpass(left);
  332. typecheckpass(right);
  333. set_varstate(left,vs_read,[vsf_must_be_valid]);
  334. set_varstate(right,vs_read,[vsf_must_be_valid]);
  335. if codegenerror then
  336. exit;
  337. { both types must be compatible }
  338. if compare_defs(left.resultdef,right.resultdef,left.nodetype)=te_incompatible then
  339. IncompatibleTypes(left.resultdef,right.resultdef);
  340. { Check if only when its a constant set }
  341. if (left.nodetype=ordconstn) and (right.nodetype=ordconstn) then
  342. begin
  343. { upper limit must be greater or equal than lower limit }
  344. if (tordconstnode(left).value>tordconstnode(right).value) and
  345. ((tordconstnode(left).value<0) or (tordconstnode(right).value>=0)) then
  346. CGMessage(parser_e_upper_lower_than_lower);
  347. end;
  348. resultdef:=left.resultdef;
  349. end;
  350. function trangenode.pass_1 : tnode;
  351. begin
  352. result:=nil;
  353. firstpass(left);
  354. firstpass(right);
  355. if codegenerror then
  356. exit;
  357. expectloc:=left.expectloc;
  358. end;
  359. {*****************************************************************************
  360. Case Helpers
  361. *****************************************************************************}
  362. function case_count_labels(root : pcaselabel) : longint;
  363. var
  364. _l : longint;
  365. procedure count(p : pcaselabel);
  366. begin
  367. inc(_l);
  368. if assigned(p^.less) then
  369. count(p^.less);
  370. if assigned(p^.greater) then
  371. count(p^.greater);
  372. end;
  373. begin
  374. _l:=0;
  375. count(root);
  376. case_count_labels:=_l;
  377. end;
  378. { Returns the true count in a case block, which includes each individual
  379. value in a range (e.g. "0..2" counts as 3) }
  380. function case_true_count(root : pcaselabel) : longint;
  381. var
  382. _l : longint;
  383. procedure count(p : pcaselabel);
  384. begin
  385. inc(_l, (p^._high.svalue - p^._low.svalue) + 1);
  386. if assigned(p^.less) then
  387. count(p^.less);
  388. if assigned(p^.greater) then
  389. count(p^.greater);
  390. end;
  391. begin
  392. _l:=0;
  393. count(root);
  394. case_true_count:=_l;
  395. end;
  396. function case_get_max(root : pcaselabel) : tconstexprint;
  397. var
  398. hp : pcaselabel;
  399. begin
  400. hp:=root;
  401. while assigned(hp^.greater) do
  402. hp:=hp^.greater;
  403. case_get_max:=hp^._high;
  404. end;
  405. function case_get_min(root : pcaselabel) : tconstexprint;
  406. var
  407. hp : pcaselabel;
  408. begin
  409. hp:=root;
  410. while assigned(hp^.less) do
  411. hp:=hp^.less;
  412. case_get_min:=hp^._low;
  413. end;
  414. procedure deletecaselabels(p : pcaselabel);
  415. begin
  416. if assigned(p^.greater) then
  417. deletecaselabels(p^.greater);
  418. if assigned(p^.less) then
  419. deletecaselabels(p^.less);
  420. if (p^.label_type = ltConstString) then
  421. begin
  422. p^._low_str.Free;
  423. p^._high_str.Free;
  424. end;
  425. dispose(p);
  426. end;
  427. function copycaselabel(p : pcaselabel) : pcaselabel;
  428. var
  429. n : pcaselabel;
  430. begin
  431. new(n);
  432. n^:=p^;
  433. if (p^.label_type = ltConstString) then
  434. begin
  435. n^._low_str := tstringconstnode(p^._low_str.getcopy);
  436. n^._high_str := tstringconstnode(p^._high_str.getcopy);
  437. end;
  438. if assigned(p^.greater) then
  439. n^.greater:=copycaselabel(p^.greater);
  440. if assigned(p^.less) then
  441. n^.less:=copycaselabel(p^.less);
  442. copycaselabel:=n;
  443. end;
  444. procedure ppuwritecaselabel(ppufile:tcompilerppufile;p : pcaselabel);
  445. var
  446. b : byte;
  447. begin
  448. ppufile.putboolean(p^.label_type = ltConstString);
  449. if (p^.label_type = ltConstString) then
  450. begin
  451. p^._low_str.ppuwrite(ppufile);
  452. p^._high_str.ppuwrite(ppufile);
  453. end
  454. else
  455. begin
  456. ppufile.putexprint(p^._low);
  457. ppufile.putexprint(p^._high);
  458. end;
  459. ppufile.putlongint(p^.blockid);
  460. b:=ord(assigned(p^.greater)) or (ord(assigned(p^.less)) shl 1);
  461. ppufile.putbyte(b);
  462. if assigned(p^.greater) then
  463. ppuwritecaselabel(ppufile,p^.greater);
  464. if assigned(p^.less) then
  465. ppuwritecaselabel(ppufile,p^.less);
  466. end;
  467. function ppuloadcaselabel(ppufile:tcompilerppufile):pcaselabel;
  468. var
  469. b : byte;
  470. p : pcaselabel;
  471. begin
  472. new(p);
  473. if ppufile.getboolean then
  474. begin
  475. p^.label_type := ltConstString;
  476. p^._low_str := cstringconstnode.ppuload(stringconstn,ppufile);
  477. p^._high_str := cstringconstnode.ppuload(stringconstn,ppufile);
  478. end
  479. else
  480. begin
  481. p^.label_type := ltOrdinal;
  482. p^._low:=ppufile.getexprint;
  483. p^._high:=ppufile.getexprint;
  484. end;
  485. p^.blockid:=ppufile.getlongint;
  486. b:=ppufile.getbyte;
  487. if (b and 1)=1 then
  488. p^.greater:=ppuloadcaselabel(ppufile)
  489. else
  490. p^.greater:=nil;
  491. if (b and 2)=2 then
  492. p^.less:=ppuloadcaselabel(ppufile)
  493. else
  494. p^.less:=nil;
  495. ppuloadcaselabel:=p;
  496. end;
  497. {*****************************************************************************
  498. TCASENODE
  499. *****************************************************************************}
  500. constructor tcasenode.create(l:tnode);
  501. begin
  502. inherited create(casen,l);
  503. labels:=nil;
  504. blocks:=TFPList.create;
  505. elseblock:=nil;
  506. end;
  507. destructor tcasenode.destroy;
  508. var
  509. i : longint;
  510. hp : pcaseblock;
  511. begin
  512. elseblock.free;
  513. deletecaselabels(labels);
  514. for i:=0 to blocks.count-1 do
  515. begin
  516. pcaseblock(blocks[i])^.statement.free;
  517. hp:=pcaseblock(blocks[i]);
  518. dispose(hp);
  519. end;
  520. blocks.free;
  521. inherited destroy;
  522. end;
  523. constructor tcasenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  524. var
  525. cnt,i : longint;
  526. begin
  527. inherited ppuload(t,ppufile);
  528. elseblock:=ppuloadnode(ppufile);
  529. cnt:=ppufile.getlongint();
  530. blocks:=TFPList.create;
  531. for i:=0 to cnt-1 do
  532. addblock(i,ppuloadnode(ppufile));
  533. labels:=ppuloadcaselabel(ppufile);
  534. end;
  535. procedure tcasenode.ppuwrite(ppufile:tcompilerppufile);
  536. var
  537. i : longint;
  538. begin
  539. inherited ppuwrite(ppufile);
  540. ppuwritenode(ppufile,elseblock);
  541. ppufile.putlongint(blocks.count);
  542. for i:=0 to blocks.count-1 do
  543. ppuwritenode(ppufile,pcaseblock(blocks[i])^.statement);
  544. ppuwritecaselabel(ppufile,labels);
  545. end;
  546. procedure tcasenode.buildderefimpl;
  547. var
  548. i : integer;
  549. begin
  550. inherited buildderefimpl;
  551. if assigned(elseblock) then
  552. elseblock.buildderefimpl;
  553. for i:=0 to blocks.count-1 do
  554. pcaseblock(blocks[i])^.statement.buildderefimpl;
  555. end;
  556. procedure tcasenode.derefimpl;
  557. var
  558. i : integer;
  559. begin
  560. inherited derefimpl;
  561. if assigned(elseblock) then
  562. elseblock.derefimpl;
  563. for i:=0 to blocks.count-1 do
  564. pcaseblock(blocks[i])^.statement.derefimpl;
  565. end;
  566. function tcasenode.pass_typecheck : tnode;
  567. var
  568. i : integer;
  569. begin
  570. result:=nil;
  571. do_typecheckpass(left);
  572. for i:=0 to blocks.count-1 do
  573. typecheckpass(pcaseblock(blocks[i])^.statement);
  574. if assigned(elseblock) then
  575. typecheckpass(elseblock);
  576. resultdef:=voidtype;
  577. end;
  578. type
  579. TLinkedListCaseLabelItem = class(TLinkedListItem)
  580. casenode: pcaselabel;
  581. constructor create(c: pcaselabel);
  582. end;
  583. constructor TLinkedListCaseLabelItem.create(c: pcaselabel);
  584. begin
  585. inherited create;
  586. casenode:=c;
  587. end;
  588. function tcasenode.pass_1 : tnode;
  589. var
  590. i: integer;
  591. node_thenblock, node_elseblock, if_node,temp_cleanup : tnode;
  592. tempcaseexpr : ttempcreatenode;
  593. if_block, init_block: tblocknode;
  594. stmt: tstatementnode;
  595. procedure add_label_to_blockid_list(list: tfpobjectlist; lab: pcaselabel);
  596. begin
  597. if not assigned(lab) then
  598. exit;
  599. if not assigned(list[lab^.blockid]) then
  600. list[lab^.blockid]:=tfpobjectlist.create(true);
  601. tfpobjectlist(list[lab^.blockid]).add(TLinkedListCaseLabelItem.create(lab));
  602. add_label_to_blockid_list(list,lab^.less);
  603. add_label_to_blockid_list(list,lab^.greater);
  604. end;
  605. function order_labels_by_blockid: tfpobjectlist;
  606. begin
  607. result:=tfpobjectlist.create(true);
  608. result.count:=blocks.count;
  609. add_label_to_blockid_list(result,labels);
  610. end;
  611. function makeifblock(elseblock : tnode): tnode;
  612. var
  613. i, j: longint;
  614. check: taddnode;
  615. newcheck: ^taddnode;
  616. blocklist, lablist: tfpobjectlist;
  617. labitem: pcaselabel;
  618. begin
  619. result:=elseblock;
  620. blocklist:=order_labels_by_blockid;
  621. { in reverse order so that the case options at the start of the case
  622. statement are evaluated first, as they presumably are the most
  623. common }
  624. for i:=blocklist.count-1 downto 0 do
  625. begin
  626. lablist:=tfpobjectlist(blocklist[i]);
  627. check:=nil;
  628. for j:=0 to lablist.count-1 do
  629. begin
  630. if assigned(check) then
  631. begin
  632. check:=caddnode.create(orn,check,nil);
  633. newcheck:[email protected]
  634. end
  635. else
  636. newcheck:=@check;
  637. labitem:=TLinkedListCaseLabelItem(lablist[j]).casenode;
  638. newcheck^:=caddnode.create(equaln,left.getcopy,labitem^._low_str.getcopy);
  639. if (labitem^._low_str.fullcompare(labitem^._high_str)<>0) then
  640. begin
  641. newcheck^.nodetype:=gten;
  642. newcheck^:=caddnode.create(
  643. andn,newcheck^,caddnode.create(
  644. lten,left.getcopy,labitem^._high_str.getcopy));
  645. end;
  646. end;
  647. result:=cifnode.create(check,
  648. pcaseblock(blocks[i])^.statement,result);
  649. pcaseblock(blocks[i])^.statement:=nil;
  650. end;
  651. { will free its elements too because of create(true) }
  652. blocklist.free;
  653. typecheckpass(result);
  654. end;
  655. begin
  656. result:=nil;
  657. init_block:=nil;
  658. temp_cleanup:=nil;
  659. expectloc:=LOC_VOID;
  660. { evalutes the case expression }
  661. firstpass(left);
  662. set_varstate(left,vs_read,[vsf_must_be_valid]);
  663. if codegenerror then
  664. exit;
  665. { Load caseexpr into temp var if complex. }
  666. { No need to do this for ordinal, because }
  667. { in that case caseexpr is generated once }
  668. if (labels^.label_type = ltConstString) and (not valid_for_addr(left, false)) and
  669. (blocks.count > 0) then
  670. begin
  671. init_block := internalstatements(stmt);
  672. tempcaseexpr :=
  673. ctempcreatenode.create(
  674. left.resultdef, left.resultdef.size, tt_persistent, true);
  675. temp_cleanup := ctempdeletenode.create(tempcaseexpr);
  676. typecheckpass(tnode(tempcaseexpr));
  677. addstatement(stmt, tempcaseexpr);
  678. addstatement(
  679. stmt, cassignmentnode.create(
  680. ctemprefnode.create(tempcaseexpr), left));
  681. left := ctemprefnode.create(tempcaseexpr);
  682. typecheckpass(left);
  683. end;
  684. { first case }
  685. for i:=0 to blocks.count-1 do
  686. firstpass(pcaseblock(blocks[i])^.statement);
  687. { may be handle else tree }
  688. if assigned(elseblock) then
  689. begin
  690. firstpass(elseblock);
  691. { kill case? }
  692. if blocks.count=0 then
  693. begin
  694. result:=elseblock;
  695. elseblock:=nil;
  696. exit;
  697. end;
  698. end
  699. else
  700. if blocks.count=0 then
  701. begin
  702. result:=cnothingnode.create;
  703. exit;
  704. end;
  705. if (labels^.label_type = ltConstString) then
  706. begin
  707. if_node:=makeifblock(elseblock);
  708. if assigned(init_block) then
  709. firstpass(tnode(init_block));
  710. if_block:=internalstatements(stmt);
  711. if assigned(init_block) then
  712. addstatement(stmt, init_block);
  713. addstatement(stmt,if_node);
  714. if assigned(temp_cleanup) then
  715. addstatement(stmt,temp_cleanup);
  716. result:=if_block;
  717. elseblock:= nil;
  718. exit;
  719. end;
  720. if is_boolean(left.resultdef) then
  721. begin
  722. case blocks.count of
  723. 2:
  724. begin
  725. if boolean(qword(labels^._low))=false then
  726. begin
  727. node_thenblock:=pcaseblock(blocks[labels^.greater^.blockid])^.statement;
  728. node_elseblock:=pcaseblock(blocks[labels^.blockid])^.statement;
  729. pcaseblock(blocks[labels^.greater^.blockid])^.statement:=nil;
  730. end
  731. else
  732. begin
  733. node_thenblock:=pcaseblock(blocks[labels^.blockid])^.statement;
  734. node_elseblock:=pcaseblock(blocks[labels^.less^.blockid])^.statement;
  735. pcaseblock(blocks[labels^.less^.blockid])^.statement:=nil;
  736. end;
  737. pcaseblock(blocks[labels^.blockid])^.statement:=nil;
  738. end;
  739. 1:
  740. begin
  741. if labels^._low=labels^._high then
  742. begin
  743. if boolean(qword(labels^._low))=false then
  744. begin
  745. node_thenblock:=elseblock;
  746. node_elseblock:=pcaseblock(blocks[labels^.blockid])^.statement;
  747. end
  748. else
  749. begin
  750. node_thenblock:=pcaseblock(blocks[labels^.blockid])^.statement;
  751. node_elseblock:=elseblock;
  752. end;
  753. pcaseblock(blocks[labels^.blockid])^.statement:=nil;
  754. elseblock:=nil;
  755. end
  756. else
  757. begin
  758. result:=pcaseblock(blocks[labels^.blockid])^.statement;
  759. pcaseblock(blocks[labels^.blockid])^.statement:=nil;
  760. elseblock:=nil;
  761. exit;
  762. end;
  763. end;
  764. else
  765. internalerror(200805031);
  766. end;
  767. result:=cifnode.create(left,node_thenblock,node_elseblock);
  768. left:=nil;
  769. end;
  770. end;
  771. function tcasenode.simplify(forinline:boolean):tnode;
  772. var
  773. tmp: pcaselabel;
  774. begin
  775. result:=nil;
  776. if left.nodetype=ordconstn then
  777. begin
  778. tmp:=labels;
  779. { check all case labels until we find one that fits }
  780. while assigned(tmp) do
  781. begin
  782. if (tmp^._low<=tordconstnode(left).value) and
  783. (tmp^._high>=tordconstnode(left).value) then
  784. begin
  785. if tmp^.blockid>=blocks.count then
  786. internalerror(2014022101);
  787. result:=pcaseblock(blocks[tmp^.blockid])^.statement;
  788. if not assigned(result) then
  789. internalerror(2014022102);
  790. result:=result.getcopy;
  791. exit;
  792. end;
  793. if tmp^._high<tordconstnode(left).value then
  794. tmp:=tmp^.greater
  795. else
  796. tmp:=tmp^.less;
  797. end;
  798. { no label did match; use the else block if available }
  799. if assigned(elseblock) then
  800. result:=elseblock.getcopy
  801. else
  802. { no else block, so there is no code to execute at all }
  803. result:=cnothingnode.create;
  804. end;
  805. end;
  806. function tcasenode.dogetcopy : tnode;
  807. var
  808. n : tcasenode;
  809. i : longint;
  810. begin
  811. n:=tcasenode(inherited dogetcopy);
  812. if assigned(elseblock) then
  813. n.elseblock:=elseblock.dogetcopy
  814. else
  815. n.elseblock:=nil;
  816. if assigned(labels) then
  817. n.labels:=copycaselabel(labels)
  818. else
  819. n.labels:=nil;
  820. if assigned(blocks) then
  821. begin
  822. n.blocks:=TFPList.create;
  823. for i:=0 to blocks.count-1 do
  824. begin
  825. if not assigned(blocks[i]) then
  826. internalerror(200411302);
  827. n.addblock(i,pcaseblock(blocks[i])^.statement.dogetcopy);
  828. end;
  829. end
  830. else
  831. n.blocks:=nil;
  832. dogetcopy:=n;
  833. end;
  834. procedure tcasenode.printnodetree(var t: text);
  835. var
  836. i : longint;
  837. begin
  838. write(t,printnodeindention,'(');
  839. printnodeindent;
  840. printnodeinfo(t);
  841. writeln(t);
  842. printnode(t,left);
  843. i:=0;
  844. for i:=0 to blocks.count-1 do
  845. begin
  846. writeln(t,printnodeindention,'(caseblock blockid: ',i);
  847. printnodeindent;
  848. printnode(t,pcaseblock(blocks[i])^.statement);
  849. printnodeunindent;
  850. writeln(t,printnodeindention,')');
  851. end;
  852. if assigned(elseblock) then
  853. begin
  854. writeln(t,printnodeindention,'(else: ',i);
  855. printnodeindent;
  856. printnode(t,elseblock);
  857. printnodeunindent;
  858. writeln(t,printnodeindention,')');
  859. end;
  860. printnodeunindent;
  861. writeln(t,printnodeindention,')');
  862. end;
  863. procedure tcasenode.insertintolist(l : tnodelist);
  864. begin
  865. end;
  866. function caselabelsequal(n1,n2: pcaselabel): boolean;
  867. begin
  868. result :=
  869. (not assigned(n1) and not assigned(n2)) or
  870. (assigned(n1) and assigned(n2) and
  871. (n1^._low = n2^._low) and
  872. (n1^._high = n2^._high) and
  873. { the rest of the fields don't matter for equality (JM) }
  874. caselabelsequal(n1^.less,n2^.less) and
  875. caselabelsequal(n1^.greater,n2^.greater))
  876. end;
  877. function caseblocksequal(b1,b2:TFPList): boolean;
  878. var
  879. i : longint;
  880. begin
  881. result:=false;
  882. if b1.count<>b2.count then
  883. exit;
  884. for i:=0 to b1.count-1 do
  885. begin
  886. if not pcaseblock(b1[i])^.statement.isequal(pcaseblock(b2[i])^.statement) then
  887. exit;
  888. end;
  889. result:=true;
  890. end;
  891. function tcasenode.docompare(p: tnode): boolean;
  892. begin
  893. result :=
  894. inherited docompare(p) and
  895. caselabelsequal(labels,tcasenode(p).labels) and
  896. caseblocksequal(blocks,tcasenode(p).blocks) and
  897. elseblock.isequal(tcasenode(p).elseblock);
  898. end;
  899. procedure tcasenode.addblock(blockid:longint;instr:tnode);
  900. var
  901. hcaseblock : pcaseblock;
  902. begin
  903. new(hcaseblock);
  904. fillchar(hcaseblock^,sizeof(hcaseblock^),0);
  905. hcaseblock^.statement:=instr;
  906. if blockid>=blocks.count then
  907. blocks.count:=blockid+1;
  908. blocks[blockid]:=hcaseblock;
  909. end;
  910. procedure tcasenode.addelseblock(instr:tnode);
  911. begin
  912. elseblock:=instr;
  913. end;
  914. procedure tcasenode.addlabel(blockid:longint;const l,h : TConstExprInt);
  915. var
  916. hcaselabel : pcaselabel;
  917. function insertlabel(var p : pcaselabel):pcaselabel;
  918. begin
  919. if p=nil then
  920. begin
  921. p:=hcaselabel;
  922. result:=p;
  923. end
  924. else
  925. if (p^._low>hcaselabel^._low) and
  926. (p^._low>hcaselabel^._high) then
  927. begin
  928. if (hcaselabel^.blockid = p^.blockid) and
  929. (p^._low = hcaselabel^._high + 1) then
  930. begin
  931. p^._low := hcaselabel^._low;
  932. dispose(hcaselabel);
  933. result:=p;
  934. end
  935. else
  936. result:=insertlabel(p^.less)
  937. end
  938. else
  939. if (p^._high<hcaselabel^._low) and
  940. (p^._high<hcaselabel^._high) then
  941. begin
  942. if (hcaselabel^.blockid = p^.blockid) and
  943. (p^._high+1 = hcaselabel^._low) then
  944. begin
  945. p^._high := hcaselabel^._high;
  946. dispose(hcaselabel);
  947. result:=p;
  948. end
  949. else
  950. result:=insertlabel(p^.greater);
  951. end
  952. else
  953. begin
  954. dispose(hcaselabel);
  955. Message(parser_e_double_caselabel);
  956. result:=nil;
  957. end
  958. end;
  959. begin
  960. new(hcaselabel);
  961. fillchar(hcaselabel^,sizeof(tcaselabel),0);
  962. hcaselabel^.blockid:=blockid;
  963. hcaselabel^.label_type:=ltOrdinal;
  964. hcaselabel^._low:=l;
  965. hcaselabel^._high:=h;
  966. insertlabel(labels);
  967. end;
  968. procedure tcasenode.addlabel(blockid: longint; l, h: tstringconstnode);
  969. var
  970. hcaselabel : pcaselabel;
  971. function insertlabel(var p : pcaselabel) : pcaselabel;
  972. begin
  973. if not assigned(p) then
  974. begin
  975. p := hcaselabel;
  976. result := p;
  977. end
  978. else
  979. if (p^._low_str.fullcompare(hcaselabel^._high_str) > 0) then
  980. result := insertlabel(p^.less)
  981. else
  982. if (p^._high_str.fullcompare(hcaselabel^._low_str) < 0) then
  983. result := insertlabel(p^.greater)
  984. else
  985. begin
  986. hcaselabel^._low_str.free;
  987. hcaselabel^._high_str.free;
  988. dispose(hcaselabel);
  989. Message(parser_e_double_caselabel);
  990. result:=nil;
  991. end;
  992. end;
  993. begin
  994. new(hcaselabel);
  995. fillchar(hcaselabel^, sizeof(tcaselabel), 0);
  996. hcaselabel^.blockid := blockid;
  997. hcaselabel^.label_type := ltConstString;
  998. hcaselabel^._low_str := tstringconstnode(l.getcopy);
  999. hcaselabel^._high_str := tstringconstnode(h.getcopy);
  1000. insertlabel(labels);
  1001. end;
  1002. end.