nset.pas 31 KB

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