nset.pas 31 KB

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