nset.pas 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  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,aasmtai,aasmdata,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. { range type }
  34. case label_type : TLabelType of
  35. ltOrdinal:
  36. (
  37. _low,
  38. _high : TConstExprInt;
  39. );
  40. ltConstString:
  41. (
  42. _low_str,
  43. _high_str : tstringconstnode;
  44. );
  45. end;
  46. pcaseblock = ^tcaseblock;
  47. tcaseblock = record
  48. { label (only used in pass_generate_code) }
  49. blocklabel : tasmlabel;
  50. statementlabel : tlabelnode;
  51. { instructions }
  52. statement : tnode;
  53. end;
  54. tsetelementnode = class(tbinarynode)
  55. constructor create(l,r : tnode);virtual;
  56. function pass_typecheck:tnode;override;
  57. function pass_1 : tnode;override;
  58. end;
  59. tsetelementnodeclass = class of tsetelementnode;
  60. tinnode = class(tbinopnode)
  61. constructor create(l,r : tnode);virtual;reintroduce;
  62. function pass_typecheck:tnode;override;
  63. function simplify(forinline : boolean):tnode;override;
  64. function pass_1 : tnode;override;
  65. end;
  66. tinnodeclass = class of tinnode;
  67. trangenode = class(tbinarynode)
  68. constructor create(l,r : tnode);virtual;
  69. function pass_typecheck:tnode;override;
  70. function pass_1 : tnode;override;
  71. end;
  72. trangenodeclass = class of trangenode;
  73. tcasenode = class(tunarynode)
  74. labels : pcaselabel;
  75. blocks : TFPList;
  76. elseblock : tnode;
  77. constructor create(l:tnode);virtual;
  78. destructor destroy;override;
  79. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  80. procedure ppuwrite(ppufile:tcompilerppufile);override;
  81. procedure buildderefimpl;override;
  82. procedure derefimpl;override;
  83. function dogetcopy : tnode;override;
  84. procedure insertintolist(l : tnodelist);override;
  85. function pass_typecheck:tnode;override;
  86. function pass_1 : tnode;override;
  87. function docompare(p: tnode): boolean; override;
  88. procedure addlabel(blockid:longint;l,h : TConstExprInt); overload;
  89. procedure addlabel(blockid:longint;l,h : tstringconstnode); overload;
  90. procedure addblock(blockid:longint;instr:tnode);
  91. procedure addelseblock(instr:tnode);
  92. end;
  93. tcasenodeclass = class of tcasenode;
  94. var
  95. csetelementnode : tsetelementnodeclass = tsetelementnode;
  96. cinnode : tinnodeclass = tinnode;
  97. crangenode : trangenodeclass = trangenode;
  98. ccasenode : tcasenodeclass = tcasenode;
  99. { counts the labels }
  100. function case_count_labels(root : pcaselabel) : longint;
  101. { searches the highest label }
  102. function case_get_max(root : pcaselabel) : tconstexprint;
  103. { searches the lowest label }
  104. function case_get_min(root : pcaselabel) : tconstexprint;
  105. implementation
  106. uses
  107. systems,
  108. verbose,
  109. symconst,symdef,symsym,symtable,defutil,defcmp,
  110. htypechk,pass_1,
  111. nadd,nbas,ncnv,nld,cgobj,cgbase,
  112. widestr;
  113. {*****************************************************************************
  114. TSETELEMENTNODE
  115. *****************************************************************************}
  116. constructor tsetelementnode.create(l,r : tnode);
  117. begin
  118. inherited create(setelementn,l,r);
  119. end;
  120. function tsetelementnode.pass_typecheck:tnode;
  121. begin
  122. result:=nil;
  123. typecheckpass(left);
  124. if assigned(right) then
  125. typecheckpass(right);
  126. set_varstate(left,vs_read,[vsf_must_be_valid]);
  127. if codegenerror then
  128. exit;
  129. resultdef:=left.resultdef;
  130. end;
  131. function tsetelementnode.pass_1 : tnode;
  132. begin
  133. result:=nil;
  134. firstpass(left);
  135. if assigned(right) then
  136. firstpass(right);
  137. if codegenerror then
  138. exit;
  139. expectloc:=left.expectloc;
  140. end;
  141. {*****************************************************************************
  142. TINNODE
  143. *****************************************************************************}
  144. constructor tinnode.create(l,r : tnode);
  145. begin
  146. inherited create(inn,l,r);
  147. end;
  148. function tinnode.pass_typecheck:tnode;
  149. var
  150. t : tnode;
  151. function createsetconst(psd : tsetdef) : pconstset;
  152. var
  153. pcs : pconstset;
  154. i : longint;
  155. begin
  156. new(pcs);
  157. case psd.elementdef.typ of
  158. enumdef :
  159. begin
  160. for i := 0 to tenumdef(psd.elementdef).symtable.SymList.Count - 1 do
  161. begin
  162. include(pcs^,tenumsym(tenumdef(psd.elementdef).symtable.SymList[i]).value);
  163. end;
  164. end;
  165. orddef :
  166. begin
  167. for i:=int64(torddef(psd.elementdef).low) to int64(torddef(psd.elementdef).high) do
  168. include(pcs^,i);
  169. end;
  170. end;
  171. createsetconst:=pcs;
  172. end;
  173. begin
  174. result:=nil;
  175. resultdef:=booltype;
  176. typecheckpass(right);
  177. set_varstate(right,vs_read,[vsf_must_be_valid]);
  178. if codegenerror then
  179. exit;
  180. { Convert array constructor first to set }
  181. if is_array_constructor(right.resultdef) then
  182. begin
  183. arrayconstructor_to_set(right);
  184. firstpass(right);
  185. if codegenerror then
  186. exit;
  187. end;
  188. typecheckpass(left);
  189. set_varstate(left,vs_read,[vsf_must_be_valid]);
  190. if codegenerror then
  191. exit;
  192. if not assigned(left.resultdef) then
  193. internalerror(20021126);
  194. t:=self;
  195. if isbinaryoverloaded(t) then
  196. begin
  197. result:=t;
  198. exit;
  199. end;
  200. if right.resultdef.typ<>setdef then
  201. CGMessage(sym_e_set_expected);
  202. if codegenerror then
  203. exit;
  204. if (m_tp7 in current_settings.modeswitches) then
  205. begin
  206. { insert a hint that a range check error might occur on non-byte
  207. elements with the in operator.
  208. }
  209. if (
  210. (left.resultdef.typ = orddef) and not
  211. (torddef(left.resultdef).ordtype in [s8bit,u8bit,uchar,pasbool,bool8bit])
  212. )
  213. or
  214. (
  215. (left.resultdef.typ = enumdef) and
  216. (tenumdef(left.resultdef).maxval > 255)
  217. )
  218. then
  219. CGMessage(type_h_in_range_check);
  220. { type conversion/check }
  221. if assigned(tsetdef(right.resultdef).elementdef) then
  222. inserttypeconv(left,tsetdef(right.resultdef).elementdef);
  223. end
  224. else if not is_ordinal(left.resultdef) or (left.resultdef.size > u32inttype.size) then
  225. begin
  226. CGMessage(type_h_in_range_check);
  227. if is_signed(left.resultdef) then
  228. inserttypeconv(left,s32inttype)
  229. else
  230. inserttypeconv(left,u32inttype);
  231. end
  232. else if assigned(tsetdef(right.resultdef).elementdef) and
  233. not(is_integer(tsetdef(right.resultdef).elementdef) and
  234. is_integer(left.resultdef)) then
  235. { Type conversion to check things like 'char in set_of_byte'. }
  236. { Can't use is_subequal because that will fail for }
  237. { 'widechar in set_of_char' }
  238. { Can't use the type conversion for integers because then }
  239. { "longint in set_of_byte" will give a range check error }
  240. { instead of false }
  241. inserttypeconv(left,tsetdef(right.resultdef).elementdef);
  242. { empty set then return false }
  243. if not assigned(tsetdef(right.resultdef).elementdef) or
  244. ((right.nodetype = setconstn) and
  245. (tnormalset(tsetconstnode(right).value_set^) = [])) then
  246. begin
  247. t:=cordconstnode.create(0,booltype,false);
  248. typecheckpass(t);
  249. result:=t;
  250. exit;
  251. end;
  252. result:=simplify(false);
  253. end;
  254. function tinnode.simplify(forinline : boolean):tnode;
  255. var
  256. t : tnode;
  257. begin
  258. result:=nil;
  259. { constant evaluation }
  260. if (left.nodetype=ordconstn) then
  261. begin
  262. if (right.nodetype=setconstn) then
  263. begin
  264. { tordconstnode.value is int64 -> signed -> the expression }
  265. { below will be converted to longint on 32 bit systems due }
  266. { to the rule above -> will give range check error if }
  267. { value > high(longint) if we don't take the signedness }
  268. { into account }
  269. if Tordconstnode(left).value.signed then
  270. t:=cordconstnode.create(byte(tordconstnode(left).value.svalue in Tsetconstnode(right).value_set^),
  271. booltype,true)
  272. else
  273. t:=cordconstnode.create(byte(tordconstnode(left).value.uvalue in Tsetconstnode(right).value_set^),
  274. booltype,true);
  275. typecheckpass(t);
  276. result:=t;
  277. exit;
  278. end
  279. else
  280. begin
  281. if (Tordconstnode(left).value<int64(tsetdef(right.resultdef).setbase)) or
  282. (Tordconstnode(left).value>int64(Tsetdef(right.resultdef).setmax)) then
  283. begin
  284. t:=cordconstnode.create(0, booltype, true);
  285. typecheckpass(t);
  286. result:=t;
  287. exit;
  288. end;
  289. end;
  290. end;
  291. end;
  292. function tinnode.pass_1 : tnode;
  293. begin
  294. result:=nil;
  295. expectloc:=LOC_REGISTER;
  296. firstpass(right);
  297. firstpass(left);
  298. if codegenerror then
  299. exit;
  300. end;
  301. {*****************************************************************************
  302. TRANGENODE
  303. *****************************************************************************}
  304. constructor trangenode.create(l,r : tnode);
  305. var
  306. value: string;
  307. begin
  308. { if right is char and left is string then }
  309. { right should be treated as one-symbol string }
  310. if is_conststringnode(l) and is_constcharnode(r) then
  311. begin
  312. value := char(tordconstnode(r).value.uvalue) + ''#0;
  313. r.free;
  314. r := cstringconstnode.createstr(value);
  315. do_typecheckpass(r);
  316. end;
  317. inherited create(rangen,l,r);
  318. end;
  319. function trangenode.pass_typecheck : tnode;
  320. begin
  321. result:=nil;
  322. typecheckpass(left);
  323. typecheckpass(right);
  324. set_varstate(left,vs_read,[vsf_must_be_valid]);
  325. set_varstate(right,vs_read,[vsf_must_be_valid]);
  326. if codegenerror then
  327. exit;
  328. { both types must be compatible }
  329. if compare_defs(left.resultdef,right.resultdef,left.nodetype)=te_incompatible then
  330. IncompatibleTypes(left.resultdef,right.resultdef);
  331. { Check if only when its a constant set }
  332. if (left.nodetype=ordconstn) and (right.nodetype=ordconstn) then
  333. begin
  334. { upper limit must be greater or equal than lower limit }
  335. if (tordconstnode(left).value>tordconstnode(right).value) and
  336. ((tordconstnode(left).value<0) or (tordconstnode(right).value>=0)) then
  337. CGMessage(parser_e_upper_lower_than_lower);
  338. end;
  339. resultdef:=left.resultdef;
  340. end;
  341. function trangenode.pass_1 : tnode;
  342. begin
  343. result:=nil;
  344. firstpass(left);
  345. firstpass(right);
  346. if codegenerror then
  347. exit;
  348. expectloc:=left.expectloc;
  349. end;
  350. {*****************************************************************************
  351. Case Helpers
  352. *****************************************************************************}
  353. function case_count_labels(root : pcaselabel) : longint;
  354. var
  355. _l : longint;
  356. procedure count(p : pcaselabel);
  357. begin
  358. inc(_l);
  359. if assigned(p^.less) then
  360. count(p^.less);
  361. if assigned(p^.greater) then
  362. count(p^.greater);
  363. end;
  364. begin
  365. _l:=0;
  366. count(root);
  367. case_count_labels:=_l;
  368. end;
  369. function case_get_max(root : pcaselabel) : tconstexprint;
  370. var
  371. hp : pcaselabel;
  372. begin
  373. hp:=root;
  374. while assigned(hp^.greater) do
  375. hp:=hp^.greater;
  376. case_get_max:=hp^._high;
  377. end;
  378. function case_get_min(root : pcaselabel) : tconstexprint;
  379. var
  380. hp : pcaselabel;
  381. begin
  382. hp:=root;
  383. while assigned(hp^.less) do
  384. hp:=hp^.less;
  385. case_get_min:=hp^._low;
  386. end;
  387. procedure deletecaselabels(p : pcaselabel);
  388. begin
  389. if assigned(p^.greater) then
  390. deletecaselabels(p^.greater);
  391. if assigned(p^.less) then
  392. deletecaselabels(p^.less);
  393. if (p^.label_type = ltConstString) then
  394. begin
  395. p^._low_str.Free;
  396. p^._high_str.Free;
  397. end;
  398. dispose(p);
  399. end;
  400. function copycaselabel(p : pcaselabel) : pcaselabel;
  401. var
  402. n : pcaselabel;
  403. begin
  404. new(n);
  405. n^:=p^;
  406. if (p^.label_type = ltConstString) then
  407. begin
  408. n^._low_str := tstringconstnode(p^._low_str.getcopy);
  409. n^._high_str := tstringconstnode(p^._high_str.getcopy);
  410. end;
  411. if assigned(p^.greater) then
  412. n^.greater:=copycaselabel(p^.greater);
  413. if assigned(p^.less) then
  414. n^.less:=copycaselabel(p^.less);
  415. copycaselabel:=n;
  416. end;
  417. procedure ppuwritecaselabel(ppufile:tcompilerppufile;p : pcaselabel);
  418. var
  419. b : byte;
  420. begin
  421. ppufile.putbyte(byte(p^.label_type = ltConstString));
  422. if (p^.label_type = ltConstString) then
  423. begin
  424. p^._low_str.ppuwrite(ppufile);
  425. p^._high_str.ppuwrite(ppufile);
  426. end
  427. else
  428. begin
  429. ppufile.putexprint(p^._low);
  430. ppufile.putexprint(p^._high);
  431. end;
  432. ppufile.putlongint(p^.blockid);
  433. b:=ord(assigned(p^.greater)) or (ord(assigned(p^.less)) shl 1);
  434. ppufile.putbyte(b);
  435. if assigned(p^.greater) then
  436. ppuwritecaselabel(ppufile,p^.greater);
  437. if assigned(p^.less) then
  438. ppuwritecaselabel(ppufile,p^.less);
  439. end;
  440. function ppuloadcaselabel(ppufile:tcompilerppufile):pcaselabel;
  441. var
  442. b : byte;
  443. p : pcaselabel;
  444. begin
  445. new(p);
  446. if boolean(ppufile.getbyte) then
  447. begin
  448. p^.label_type := ltConstString;
  449. p^._low_str := cstringconstnode.ppuload(stringconstn,ppufile);
  450. p^._high_str := cstringconstnode.ppuload(stringconstn,ppufile);
  451. end
  452. else
  453. begin
  454. p^.label_type := ltOrdinal;
  455. p^._low:=ppufile.getexprint;
  456. p^._high:=ppufile.getexprint;
  457. end;
  458. p^.blockid:=ppufile.getlongint;
  459. b:=ppufile.getbyte;
  460. if (b and 1)=1 then
  461. p^.greater:=ppuloadcaselabel(ppufile)
  462. else
  463. p^.greater:=nil;
  464. if (b and 2)=2 then
  465. p^.less:=ppuloadcaselabel(ppufile)
  466. else
  467. p^.less:=nil;
  468. ppuloadcaselabel:=p;
  469. end;
  470. {*****************************************************************************
  471. TCASENODE
  472. *****************************************************************************}
  473. constructor tcasenode.create(l:tnode);
  474. begin
  475. inherited create(casen,l);
  476. labels:=nil;
  477. blocks:=TFPList.create;
  478. elseblock:=nil;
  479. end;
  480. destructor tcasenode.destroy;
  481. var
  482. i : longint;
  483. hp : pcaseblock;
  484. begin
  485. elseblock.free;
  486. deletecaselabels(labels);
  487. for i:=0 to blocks.count-1 do
  488. begin
  489. pcaseblock(blocks[i])^.statement.free;
  490. hp:=pcaseblock(blocks[i]);
  491. dispose(hp);
  492. end;
  493. blocks.free;
  494. inherited destroy;
  495. end;
  496. constructor tcasenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  497. var
  498. cnt,i : longint;
  499. begin
  500. inherited ppuload(t,ppufile);
  501. elseblock:=ppuloadnode(ppufile);
  502. cnt:=ppufile.getlongint();
  503. blocks:=TFPList.create;
  504. for i:=0 to cnt-1 do
  505. addblock(i,ppuloadnode(ppufile));
  506. labels:=ppuloadcaselabel(ppufile);
  507. end;
  508. procedure tcasenode.ppuwrite(ppufile:tcompilerppufile);
  509. var
  510. i : longint;
  511. begin
  512. inherited ppuwrite(ppufile);
  513. ppuwritenode(ppufile,elseblock);
  514. ppufile.putlongint(blocks.count);
  515. for i:=0 to blocks.count-1 do
  516. ppuwritenode(ppufile,pcaseblock(blocks[i])^.statement);
  517. ppuwritecaselabel(ppufile,labels);
  518. end;
  519. procedure tcasenode.buildderefimpl;
  520. var
  521. i : integer;
  522. begin
  523. inherited buildderefimpl;
  524. if assigned(elseblock) then
  525. elseblock.buildderefimpl;
  526. for i:=0 to blocks.count-1 do
  527. pcaseblock(blocks[i])^.statement.buildderefimpl;
  528. end;
  529. procedure tcasenode.derefimpl;
  530. var
  531. i : integer;
  532. begin
  533. inherited derefimpl;
  534. if assigned(elseblock) then
  535. elseblock.derefimpl;
  536. for i:=0 to blocks.count-1 do
  537. pcaseblock(blocks[i])^.statement.derefimpl;
  538. end;
  539. function tcasenode.pass_typecheck : tnode;
  540. begin
  541. result:=nil;
  542. resultdef:=voidtype;
  543. end;
  544. function tcasenode.pass_1 : tnode;
  545. var
  546. i : integer;
  547. node_thenblock,node_elseblock,if_node : tnode;
  548. tempcaseexpr : ttempcreatenode;
  549. if_block, init_block,stmt_block : tblocknode;
  550. stmt : tstatementnode;
  551. endlabel : tlabelnode;
  552. function makeifblock(const labtree : pcaselabel; prevconditblock : tnode): tnode;
  553. var
  554. condit : tnode;
  555. begin
  556. if assigned(labtree^.less) then
  557. result := makeifblock(labtree^.less, prevconditblock)
  558. else
  559. result := prevconditblock;
  560. condit := caddnode.create(equaln, left.getcopy, labtree^._low_str.getcopy);
  561. if (labtree^._low_str.fullcompare(labtree^._high_str)<>0) then
  562. begin
  563. condit.nodetype := gten;
  564. condit := caddnode.create(
  565. andn, condit, caddnode.create(
  566. lten, left.getcopy, labtree^._high_str.getcopy));
  567. end;
  568. result :=
  569. cifnode.create(
  570. condit, cgotonode.create(pcaseblock(blocks[labtree^.blockid])^.statementlabel.labsym), result);
  571. if assigned(labtree^.greater) then
  572. result := makeifblock(labtree^.greater, result);
  573. typecheckpass(result);
  574. end;
  575. begin
  576. result:=nil;
  577. init_block:=nil;
  578. expectloc:=LOC_VOID;
  579. { evalutes the case expression }
  580. firstpass(left);
  581. set_varstate(left,vs_read,[vsf_must_be_valid]);
  582. if codegenerror then
  583. exit;
  584. { Load caseexpr into temp var if complex. }
  585. { No need to do this for ordinal, because }
  586. { in that case caseexpr is generated once }
  587. if (labels^.label_type = ltConstString) and (not valid_for_addr(left, false)) and
  588. (blocks.count > 0) then
  589. begin
  590. init_block := internalstatements(stmt);
  591. tempcaseexpr :=
  592. ctempcreatenode.create(
  593. left.resultdef, left.resultdef.size, tt_persistent, true);
  594. typecheckpass(tnode(tempcaseexpr));
  595. addstatement(stmt, tempcaseexpr);
  596. addstatement(
  597. stmt, cassignmentnode.create(
  598. ctemprefnode.create(tempcaseexpr), left));
  599. left := ctemprefnode.create(tempcaseexpr);
  600. typecheckpass(left);
  601. end;
  602. { first case }
  603. for i:=0 to blocks.count-1 do
  604. firstpass(pcaseblock(blocks[i])^.statement);
  605. { may be handle else tree }
  606. if assigned(elseblock) then
  607. begin
  608. firstpass(elseblock);
  609. { kill case? }
  610. if blocks.count=0 then
  611. begin
  612. result:=elseblock;
  613. elseblock:=nil;
  614. exit;
  615. end;
  616. end
  617. else
  618. if blocks.count=0 then
  619. begin
  620. result:=cnothingnode.create;
  621. exit;
  622. end;
  623. if (labels^.label_type = ltConstString) then
  624. begin
  625. endlabel:=clabelnode.create(cnothingnode.create,tlabelsym.create('$casestrofend'));
  626. stmt_block:=internalstatements(stmt);
  627. for i:=0 to blocks.count-1 do
  628. begin
  629. pcaseblock(blocks[i])^.statementlabel:=clabelnode.create(cnothingnode.create,tlabelsym.create('$casestrof'));
  630. addstatement(stmt,pcaseblock(blocks[i])^.statementlabel);
  631. addstatement(stmt,pcaseblock(blocks[i])^.statement);
  632. pcaseblock(blocks[i])^.statement:=nil;
  633. addstatement(stmt,cgotonode.create(endlabel.labsym));
  634. end;
  635. firstpass(tnode(stmt_block));
  636. if_node := makeifblock(labels, elseblock);
  637. if assigned(init_block) then
  638. firstpass(tnode(init_block));
  639. if_block := internalstatements(stmt);
  640. if assigned(init_block) then
  641. addstatement(stmt, init_block);
  642. addstatement(stmt, if_node);
  643. addstatement(stmt,cgotonode.create(endlabel.labsym));
  644. addstatement(stmt, stmt_block);
  645. addstatement(stmt, endlabel);
  646. result := if_block;
  647. elseblock := nil;
  648. exit;
  649. end;
  650. if is_boolean(left.resultdef) then
  651. begin
  652. case blocks.count of
  653. 2:
  654. begin
  655. if boolean(qword(labels^._low))=false then
  656. begin
  657. node_thenblock:=pcaseblock(blocks[labels^.greater^.blockid])^.statement;
  658. node_elseblock:=pcaseblock(blocks[labels^.blockid])^.statement;
  659. pcaseblock(blocks[labels^.greater^.blockid])^.statement:=nil;
  660. end
  661. else
  662. begin
  663. node_thenblock:=pcaseblock(blocks[labels^.blockid])^.statement;
  664. node_elseblock:=pcaseblock(blocks[labels^.less^.blockid])^.statement;
  665. pcaseblock(blocks[labels^.less^.blockid])^.statement:=nil;
  666. end;
  667. pcaseblock(blocks[labels^.blockid])^.statement:=nil;
  668. end;
  669. 1:
  670. begin
  671. if labels^._low=labels^._high then
  672. begin
  673. if boolean(qword(labels^._low))=false then
  674. begin
  675. node_thenblock:=elseblock;
  676. node_elseblock:=pcaseblock(blocks[labels^.blockid])^.statement;
  677. end
  678. else
  679. begin
  680. node_thenblock:=pcaseblock(blocks[labels^.blockid])^.statement;
  681. node_elseblock:=elseblock;
  682. end;
  683. pcaseblock(blocks[labels^.blockid])^.statement:=nil;
  684. elseblock:=nil;
  685. end
  686. else
  687. begin
  688. result:=pcaseblock(blocks[labels^.blockid])^.statement;
  689. pcaseblock(blocks[labels^.blockid])^.statement:=nil;
  690. elseblock:=nil;
  691. exit;
  692. end;
  693. end;
  694. else
  695. internalerror(200805031);
  696. end;
  697. result:=cifnode.create(left,node_thenblock,node_elseblock);
  698. left:=nil;
  699. end;
  700. end;
  701. function tcasenode.dogetcopy : tnode;
  702. var
  703. n : tcasenode;
  704. i : longint;
  705. begin
  706. n:=tcasenode(inherited dogetcopy);
  707. if assigned(elseblock) then
  708. n.elseblock:=elseblock.dogetcopy
  709. else
  710. n.elseblock:=nil;
  711. if assigned(labels) then
  712. n.labels:=copycaselabel(labels)
  713. else
  714. n.labels:=nil;
  715. if assigned(blocks) then
  716. begin
  717. n.blocks:=TFPList.create;
  718. for i:=0 to blocks.count-1 do
  719. begin
  720. if not assigned(blocks[i]) then
  721. internalerror(200411302);
  722. n.addblock(i,pcaseblock(blocks[i])^.statement.dogetcopy);
  723. end;
  724. end
  725. else
  726. n.blocks:=nil;
  727. dogetcopy:=n;
  728. end;
  729. procedure tcasenode.insertintolist(l : tnodelist);
  730. begin
  731. end;
  732. function caselabelsequal(n1,n2: pcaselabel): boolean;
  733. begin
  734. result :=
  735. (not assigned(n1) and not assigned(n2)) or
  736. (assigned(n1) and assigned(n2) and
  737. (n1^._low = n2^._low) and
  738. (n1^._high = n2^._high) and
  739. { the rest of the fields don't matter for equality (JM) }
  740. caselabelsequal(n1^.less,n2^.less) and
  741. caselabelsequal(n1^.greater,n2^.greater))
  742. end;
  743. function caseblocksequal(b1,b2:TFPList): boolean;
  744. var
  745. i : longint;
  746. begin
  747. result:=false;
  748. if b1.count<>b2.count then
  749. exit;
  750. for i:=0 to b1.count-1 do
  751. begin
  752. if not pcaseblock(b1[i])^.statement.isequal(pcaseblock(b2[i])^.statement) then
  753. exit;
  754. end;
  755. result:=true;
  756. end;
  757. function tcasenode.docompare(p: tnode): boolean;
  758. begin
  759. result :=
  760. inherited docompare(p) and
  761. caselabelsequal(labels,tcasenode(p).labels) and
  762. caseblocksequal(blocks,tcasenode(p).blocks) and
  763. elseblock.isequal(tcasenode(p).elseblock);
  764. end;
  765. procedure tcasenode.addblock(blockid:longint;instr:tnode);
  766. var
  767. hcaseblock : pcaseblock;
  768. begin
  769. new(hcaseblock);
  770. fillchar(hcaseblock^,sizeof(hcaseblock^),0);
  771. hcaseblock^.statement:=instr;
  772. if blockid>=blocks.count then
  773. blocks.count:=blockid+1;
  774. blocks[blockid]:=hcaseblock;
  775. end;
  776. procedure tcasenode.addelseblock(instr:tnode);
  777. begin
  778. elseblock:=instr;
  779. end;
  780. procedure tcasenode.addlabel(blockid:longint;l,h : TConstExprInt);
  781. var
  782. hcaselabel : pcaselabel;
  783. function insertlabel(var p : pcaselabel):pcaselabel;
  784. begin
  785. if p=nil then
  786. begin
  787. p:=hcaselabel;
  788. result:=p;
  789. end
  790. else
  791. if (p^._low>hcaselabel^._low) and
  792. (p^._low>hcaselabel^._high) then
  793. begin
  794. if (hcaselabel^.blockid = p^.blockid) and
  795. (p^._low = hcaselabel^._high + 1) then
  796. begin
  797. p^._low := hcaselabel^._low;
  798. dispose(hcaselabel);
  799. result:=p;
  800. end
  801. else
  802. result:=insertlabel(p^.less)
  803. end
  804. else
  805. if (p^._high<hcaselabel^._low) and
  806. (p^._high<hcaselabel^._high) then
  807. begin
  808. if (hcaselabel^.blockid = p^.blockid) and
  809. (p^._high+1 = hcaselabel^._low) then
  810. begin
  811. p^._high := hcaselabel^._high;
  812. dispose(hcaselabel);
  813. result:=p;
  814. end
  815. else
  816. result:=insertlabel(p^.greater);
  817. end
  818. else
  819. begin
  820. dispose(hcaselabel);
  821. Message(parser_e_double_caselabel);
  822. end
  823. end;
  824. begin
  825. new(hcaselabel);
  826. fillchar(hcaselabel^,sizeof(tcaselabel),0);
  827. hcaselabel^.blockid:=blockid;
  828. hcaselabel^.label_type:=ltOrdinal;
  829. hcaselabel^._low:=l;
  830. hcaselabel^._high:=h;
  831. insertlabel(labels);
  832. end;
  833. procedure tcasenode.addlabel(blockid: longint; l, h: tstringconstnode);
  834. var
  835. hcaselabel : pcaselabel;
  836. function insertlabel(var p : pcaselabel) : pcaselabel;
  837. begin
  838. if not assigned(p) then
  839. begin
  840. p := hcaselabel;
  841. result := p;
  842. end
  843. else
  844. if (p^._low_str.fullcompare(hcaselabel^._high_str) > 0) then
  845. result := insertlabel(p^.less)
  846. else
  847. if (p^._high_str.fullcompare(hcaselabel^._low_str) < 0) then
  848. result := insertlabel(p^.greater)
  849. else
  850. begin
  851. hcaselabel^._low_str.free;
  852. hcaselabel^._high_str.free;
  853. dispose(hcaselabel);
  854. Message(parser_e_double_caselabel);
  855. end;
  856. end;
  857. begin
  858. new(hcaselabel);
  859. fillchar(hcaselabel^, sizeof(tcaselabel), 0);
  860. hcaselabel^.blockid := blockid;
  861. hcaselabel^.label_type := ltConstString;
  862. hcaselabel^._low_str := tstringconstnode(l.getcopy);
  863. hcaselabel^._high_str := tstringconstnode(h.getcopy);
  864. insertlabel(labels);
  865. end;
  866. end.