nset.pas 35 KB

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