nset.pas 31 KB

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