nset.pas 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  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 (strcomp(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. { Load caseexpr into temp var if complex. }
  678. { No need to do this for ordinal, because }
  679. { in that case caseexpr is generated once }
  680. if (labels^.label_type = ltConstString) and (not valid_for_addr(left, false)) then
  681. begin
  682. init_block := internalstatements(stmt);
  683. tempcaseexpr :=
  684. ctempcreatenode.create(
  685. left.resultdef, left.resultdef.size, tt_persistent, true);
  686. typecheckpass(tnode(tempcaseexpr));
  687. addstatement(stmt, tempcaseexpr);
  688. addstatement(
  689. stmt, cassignmentnode.create(
  690. ctemprefnode.create(tempcaseexpr), left));
  691. left := ctemprefnode.create(tempcaseexpr);
  692. typecheckpass(left);
  693. end;
  694. { evalutes the case expression }
  695. firstpass(left);
  696. set_varstate(left,vs_read,[vsf_must_be_valid]);
  697. if codegenerror then
  698. exit;
  699. { first case }
  700. for i:=0 to blocks.count-1 do
  701. firstpass(pcaseblock(blocks[i])^.statement);
  702. { may be handle else tree }
  703. if assigned(elseblock) then
  704. begin
  705. firstpass(elseblock);
  706. { kill case? }
  707. if blocks.count=0 then
  708. begin
  709. result:=elseblock;
  710. elseblock:=nil;
  711. exit;
  712. end;
  713. end
  714. else
  715. if blocks.count=0 then
  716. begin
  717. result:=cnothingnode.create;
  718. exit;
  719. end;
  720. if (labels^.label_type = ltConstString) then
  721. begin
  722. if_node := makeifblock(labels, elseblock);
  723. if assigned(init_block) then
  724. begin
  725. firstpass(tnode(init_block));
  726. if_block := internalstatements(stmt);
  727. addstatement(stmt, init_block);
  728. addstatement(stmt, if_node);
  729. result := if_block;
  730. end
  731. else
  732. result := if_node;
  733. init_block := nil;
  734. elseblock := nil;
  735. exit;
  736. end;
  737. if is_boolean(left.resultdef) then
  738. begin
  739. case blocks.count of
  740. 2:
  741. begin
  742. if boolean(qword(labels^._low))=false then
  743. begin
  744. node_thenblock:=pcaseblock(blocks[labels^.greater^.blockid])^.statement;
  745. node_elseblock:=pcaseblock(blocks[labels^.blockid])^.statement;
  746. pcaseblock(blocks[labels^.greater^.blockid])^.statement:=nil;
  747. end
  748. else
  749. begin
  750. node_thenblock:=pcaseblock(blocks[labels^.blockid])^.statement;
  751. node_elseblock:=pcaseblock(blocks[labels^.less^.blockid])^.statement;
  752. pcaseblock(blocks[labels^.less^.blockid])^.statement:=nil;
  753. end;
  754. pcaseblock(blocks[labels^.blockid])^.statement:=nil;
  755. end;
  756. 1:
  757. begin
  758. if labels^._low=labels^._high then
  759. begin
  760. if boolean(qword(labels^._low))=false then
  761. begin
  762. node_thenblock:=elseblock;
  763. node_elseblock:=pcaseblock(blocks[labels^.blockid])^.statement;
  764. end
  765. else
  766. begin
  767. node_thenblock:=pcaseblock(blocks[labels^.blockid])^.statement;
  768. node_elseblock:=elseblock;
  769. end;
  770. pcaseblock(blocks[labels^.blockid])^.statement:=nil;
  771. elseblock:=nil;
  772. end
  773. else
  774. begin
  775. result:=pcaseblock(blocks[labels^.blockid])^.statement;
  776. pcaseblock(blocks[labels^.blockid])^.statement:=nil;
  777. elseblock:=nil;
  778. exit;
  779. end;
  780. end;
  781. else
  782. internalerror(200805031);
  783. end;
  784. result:=cifnode.create(left,node_thenblock,node_elseblock);
  785. left:=nil;
  786. end;
  787. end;
  788. function tcasenode.dogetcopy : tnode;
  789. var
  790. n : tcasenode;
  791. i : longint;
  792. begin
  793. n:=tcasenode(inherited dogetcopy);
  794. if assigned(elseblock) then
  795. n.elseblock:=elseblock.dogetcopy
  796. else
  797. n.elseblock:=nil;
  798. if assigned(labels) then
  799. n.labels:=copycaselabel(labels)
  800. else
  801. n.labels:=nil;
  802. if assigned(blocks) then
  803. begin
  804. n.blocks:=TFPList.create;
  805. for i:=0 to blocks.count-1 do
  806. begin
  807. if not assigned(blocks[i]) then
  808. internalerror(200411302);
  809. n.addblock(i,pcaseblock(blocks[i])^.statement.dogetcopy);
  810. end;
  811. end
  812. else
  813. n.blocks:=nil;
  814. dogetcopy:=n;
  815. end;
  816. procedure tcasenode.insertintolist(l : tnodelist);
  817. begin
  818. end;
  819. function caselabelsequal(n1,n2: pcaselabel): boolean;
  820. begin
  821. result :=
  822. (not assigned(n1) and not assigned(n2)) or
  823. (assigned(n1) and assigned(n2) and
  824. (n1^._low = n2^._low) and
  825. (n1^._high = n2^._high) and
  826. { the rest of the fields don't matter for equality (JM) }
  827. caselabelsequal(n1^.less,n2^.less) and
  828. caselabelsequal(n1^.greater,n2^.greater))
  829. end;
  830. function caseblocksequal(b1,b2:TFPList): boolean;
  831. var
  832. i : longint;
  833. begin
  834. result:=false;
  835. if b1.count<>b2.count then
  836. exit;
  837. for i:=0 to b1.count-1 do
  838. begin
  839. if not pcaseblock(b1[i])^.statement.isequal(pcaseblock(b2[i])^.statement) then
  840. exit;
  841. end;
  842. result:=true;
  843. end;
  844. function tcasenode.docompare(p: tnode): boolean;
  845. begin
  846. result :=
  847. inherited docompare(p) and
  848. caselabelsequal(labels,tcasenode(p).labels) and
  849. caseblocksequal(blocks,tcasenode(p).blocks) and
  850. elseblock.isequal(tcasenode(p).elseblock);
  851. end;
  852. procedure tcasenode.addblock(blockid:longint;instr:tnode);
  853. var
  854. hcaseblock : pcaseblock;
  855. begin
  856. new(hcaseblock);
  857. fillchar(hcaseblock^,sizeof(hcaseblock^),0);
  858. hcaseblock^.statement:=instr;
  859. if blockid>=blocks.count then
  860. blocks.count:=blockid+1;
  861. blocks[blockid]:=hcaseblock;
  862. end;
  863. procedure tcasenode.addelseblock(instr:tnode);
  864. begin
  865. elseblock:=instr;
  866. end;
  867. procedure tcasenode.addlabel(blockid:longint;l,h : TConstExprInt);
  868. var
  869. hcaselabel : pcaselabel;
  870. function insertlabel(var p : pcaselabel):pcaselabel;
  871. begin
  872. if p=nil then
  873. begin
  874. p:=hcaselabel;
  875. result:=p;
  876. end
  877. else
  878. if (p^._low>hcaselabel^._low) and
  879. (p^._low>hcaselabel^._high) then
  880. begin
  881. if (hcaselabel^.blockid = p^.blockid) and
  882. (p^._low = hcaselabel^._high + 1) then
  883. begin
  884. p^._low := hcaselabel^._low;
  885. dispose(hcaselabel);
  886. result:=p;
  887. end
  888. else
  889. result:=insertlabel(p^.less)
  890. end
  891. else
  892. if (p^._high<hcaselabel^._low) and
  893. (p^._high<hcaselabel^._high) then
  894. begin
  895. if (hcaselabel^.blockid = p^.blockid) and
  896. (p^._high+1 = hcaselabel^._low) then
  897. begin
  898. p^._high := hcaselabel^._high;
  899. dispose(hcaselabel);
  900. result:=p;
  901. end
  902. else
  903. result:=insertlabel(p^.greater);
  904. end
  905. else
  906. Message(parser_e_double_caselabel);
  907. end;
  908. begin
  909. new(hcaselabel);
  910. fillchar(hcaselabel^,sizeof(tcaselabel),0);
  911. hcaselabel^.blockid:=blockid;
  912. hcaselabel^.label_type:=ltOrdinal;
  913. hcaselabel^._low:=l;
  914. hcaselabel^._high:=h;
  915. insertlabel(labels);
  916. end;
  917. procedure tcasenode.addlabel(blockid : longint; l, h : TConstString; str_type : TConstStringType);
  918. function str_compare(l, h : TConstString) : longint;
  919. begin
  920. if (str_type in [cst_widestring, cst_unicodestring]) then
  921. result := comparewidestrings(pcompilerwidestring(l), pcompilerwidestring(h))
  922. else
  923. result := strcomp(l, h);
  924. end;
  925. var
  926. hcaselabel : pcaselabel;
  927. function insertlabel(var p : pcaselabel) : pcaselabel;
  928. begin
  929. if not assigned(p) then
  930. begin
  931. p := hcaselabel;
  932. result := p;
  933. end
  934. else
  935. if (str_compare(p^._low_str, hcaselabel^._high_str) > 0) then
  936. result := insertlabel(p^.less)
  937. else
  938. if (str_compare(p^._high_str, hcaselabel^._low_str) < 0) then
  939. result := insertlabel(p^.greater)
  940. else
  941. Message(parser_e_double_caselabel);
  942. end;
  943. begin
  944. new(hcaselabel);
  945. fillchar(hcaselabel^, sizeof(tcaselabel), 0);
  946. hcaselabel^.blockid := blockid;
  947. hcaselabel^.label_type := ltConstString;
  948. if (str_type in [cst_widestring, cst_unicodestring]) then
  949. begin
  950. initwidestring(pcompilerwidestring(hcaselabel^._low_str));
  951. initwidestring(pcompilerwidestring(hcaselabel^._high_str));
  952. copywidestring(pcompilerwidestring(l), pcompilerwidestring(hcaselabel^._low_str));
  953. copywidestring(pcompilerwidestring(h), pcompilerwidestring(hcaselabel^._high_str));
  954. end
  955. else
  956. begin
  957. getmem(hcaselabel^._low_str, strlen(l) + 1);
  958. getmem(hcaselabel^._high_str, strlen(h) + 1);
  959. strcopy(hcaselabel^._low_str, l);
  960. strcopy(hcaselabel^._high_str, h);
  961. hcaselabel^._low_str[strlen(l)] := #0;
  962. hcaselabel^._high_str[strlen(h)] := #0;
  963. end;
  964. hcaselabel^._str_type := str_type;
  965. insertlabel(labels);
  966. end;
  967. begin
  968. csetelementnode:=tsetelementnode;
  969. cinnode:=tinnode;
  970. crangenode:=trangenode;
  971. ccasenode:=tcasenode;
  972. end.