nset.pas 31 KB

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