nset.pas 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288
  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,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. labellabel : TAsmLabel;
  34. { range type }
  35. case label_type : TLabelType of
  36. ltOrdinal:
  37. (
  38. _low,
  39. _high : TConstExprInt;
  40. );
  41. ltConstString:
  42. (
  43. _low_str,
  44. _high_str : tstringconstnode;
  45. );
  46. end;
  47. pcaseblock = ^tcaseblock;
  48. tcaseblock = record
  49. { label (only used in pass_generate_code) }
  50. blocklabel : tasmlabel;
  51. { shortcut - set to true if blocklabel isn't actually unique to the
  52. case block due to one of the following conditions:
  53. - if the node contains a jump, then the label is set to that jump's destination,
  54. - if the node is empty, the label is set to the end label. }
  55. shortcut: Boolean;
  56. statementlabel : tlabelnode;
  57. { instructions }
  58. statement : tnode;
  59. end;
  60. tsetelementnode = class(tbinarynode)
  61. constructor create(l,r : tnode);virtual;
  62. function pass_typecheck:tnode;override;
  63. function pass_1 : tnode;override;
  64. end;
  65. tsetelementnodeclass = class of tsetelementnode;
  66. tinnode = class(tbinopnode)
  67. constructor create(l,r : tnode);virtual;reintroduce;
  68. function pass_typecheck:tnode;override;
  69. function simplify(forinline : boolean):tnode;override;
  70. function pass_1 : tnode;override;
  71. end;
  72. tinnodeclass = class of tinnode;
  73. trangenode = class(tbinarynode)
  74. constructor create(l,r : tnode);virtual;
  75. function pass_typecheck:tnode;override;
  76. function pass_1 : tnode;override;
  77. end;
  78. trangenodeclass = class of trangenode;
  79. tcasenode = class(tunarynode)
  80. strict private
  81. { Number of labels }
  82. flabelcnt: cardinal;
  83. { Number of individual values checked, counting each value in a range
  84. individually (e.g. 0..2 counts as 3). }
  85. flabelcoverage: qword;
  86. fcountsuptodate: boolean;
  87. function getlabelcnt: cardinal;
  88. function getlabelcoverage: qword;
  89. procedure updatecoverage;
  90. procedure checkordinalcoverage;
  91. public
  92. blocks : TFPList;
  93. elseblock : tnode;
  94. constructor create(l:tnode);virtual;
  95. destructor destroy;override;
  96. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  97. procedure ppuwrite(ppufile:tcompilerppufile);override;
  98. procedure buildderefimpl;override;
  99. procedure derefimpl;override;
  100. function dogetcopy : tnode;override;
  101. procedure printnodetree(var t:text);override;
  102. procedure insertintolist(l : tnodelist);override;
  103. function pass_typecheck:tnode;override;
  104. function pass_1 : tnode;override;
  105. function simplify(forinline:boolean):tnode;override;
  106. function docompare(p: tnode): boolean; override;
  107. procedure addlabel(blockid:longint;const l,h : TConstExprInt); overload;
  108. procedure addlabel(blockid:longint;l,h : tstringconstnode); overload;
  109. procedure addblock(blockid:longint;instr:tnode);
  110. procedure addelseblock(instr:tnode);
  111. property labelcnt: cardinal read getlabelcnt;
  112. property labelcoverage: qword read getlabelcoverage;
  113. protected
  114. flabels : pcaselabel;
  115. public
  116. property labels: pcaselabel read flabels;
  117. end;
  118. tcasenodeclass = class of tcasenode;
  119. var
  120. csetelementnode : tsetelementnodeclass = tsetelementnode;
  121. cinnode : tinnodeclass = tinnode;
  122. crangenode : trangenodeclass = trangenode;
  123. ccasenode : tcasenodeclass = tcasenode;
  124. { searches the highest label }
  125. function case_get_max(root : pcaselabel) : tconstexprint;
  126. { searches the lowest label }
  127. function case_get_min(root : pcaselabel) : tconstexprint;
  128. implementation
  129. uses
  130. verbose,cutils,
  131. symconst,symdef,symsym,symtable,defutil,defcmp,
  132. htypechk,pass_1,
  133. nadd,nbas,ncal,ncnv,nld,nutils,
  134. cgbase;
  135. {*****************************************************************************
  136. TSETELEMENTNODE
  137. *****************************************************************************}
  138. constructor tsetelementnode.create(l,r : tnode);
  139. begin
  140. inherited create(setelementn,l,r);
  141. end;
  142. function tsetelementnode.pass_typecheck:tnode;
  143. begin
  144. result:=nil;
  145. typecheckpass(left);
  146. if assigned(right) then
  147. typecheckpass(right);
  148. set_varstate(left,vs_read,[vsf_must_be_valid]);
  149. if codegenerror then
  150. exit;
  151. resultdef:=left.resultdef;
  152. end;
  153. function tsetelementnode.pass_1 : tnode;
  154. begin
  155. result:=nil;
  156. firstpass(left);
  157. if assigned(right) then
  158. firstpass(right);
  159. if codegenerror then
  160. exit;
  161. expectloc:=left.expectloc;
  162. end;
  163. {*****************************************************************************
  164. TINNODE
  165. *****************************************************************************}
  166. constructor tinnode.create(l,r : tnode);
  167. begin
  168. inherited create(inn,l,r);
  169. end;
  170. function tinnode.pass_typecheck:tnode;
  171. var
  172. t : tnode;
  173. function createsetconst(psd : tsetdef) : pconstset;
  174. var
  175. pcs : pconstset;
  176. i : longint;
  177. begin
  178. new(pcs);
  179. case psd.elementdef.typ of
  180. enumdef :
  181. begin
  182. for i := 0 to tenumdef(psd.elementdef).symtable.SymList.Count - 1 do
  183. begin
  184. include(pcs^,tenumsym(tenumdef(psd.elementdef).symtable.SymList[i]).value);
  185. end;
  186. end;
  187. orddef :
  188. begin
  189. for i:=int64(torddef(psd.elementdef).low) to int64(torddef(psd.elementdef).high) do
  190. include(pcs^,i);
  191. end;
  192. else
  193. internalerror(2019050516);
  194. end;
  195. createsetconst:=pcs;
  196. end;
  197. begin
  198. result:=nil;
  199. resultdef:=pasbool1type;
  200. typecheckpass(right);
  201. set_varstate(right,vs_read,[vsf_must_be_valid]);
  202. if codegenerror then
  203. exit;
  204. { Convert array constructor first to set }
  205. if is_array_constructor(right.resultdef) then
  206. begin
  207. arrayconstructor_to_set(right);
  208. firstpass(right);
  209. if codegenerror then
  210. exit;
  211. end;
  212. typecheckpass(left);
  213. set_varstate(left,vs_read,[vsf_must_be_valid]);
  214. if codegenerror then
  215. exit;
  216. if not assigned(left.resultdef) then
  217. internalerror(20021126);
  218. t:=self;
  219. if isbinaryoverloaded(t,[]) then
  220. begin
  221. result:=t;
  222. exit;
  223. end;
  224. if right.resultdef.typ<>setdef then
  225. CGMessage(sym_e_set_expected);
  226. if codegenerror then
  227. exit;
  228. if (m_tp7 in current_settings.modeswitches) then
  229. begin
  230. { insert a hint that a range check error might occur on non-byte
  231. elements with the in operator.
  232. }
  233. if (
  234. (left.resultdef.typ = orddef) and not
  235. (torddef(left.resultdef).ordtype in [s8bit,u8bit,uchar,pasbool1,pasbool8,bool8bit])
  236. )
  237. or
  238. (
  239. (left.resultdef.typ = enumdef) and
  240. (tenumdef(left.resultdef).maxval > 255)
  241. )
  242. then
  243. CGMessage(type_h_in_range_check);
  244. { type conversion/check }
  245. if assigned(tsetdef(right.resultdef).elementdef) then
  246. inserttypeconv(left,tsetdef(right.resultdef).elementdef);
  247. end
  248. else if not is_ordinal(left.resultdef) or (left.resultdef.size > u32inttype.size) then
  249. begin
  250. CGMessage(type_h_in_range_check);
  251. if is_signed(left.resultdef) then
  252. inserttypeconv(left,s32inttype)
  253. else
  254. inserttypeconv(left,u32inttype);
  255. end
  256. else if assigned(tsetdef(right.resultdef).elementdef) and
  257. not(is_integer(tsetdef(right.resultdef).elementdef) and
  258. is_integer(left.resultdef)) then
  259. { Type conversion to check things like 'char in set_of_byte'. }
  260. { Can't use is_subequal because that will fail for }
  261. { 'widechar in set_of_char' }
  262. { Can't use the type conversion for integers because then }
  263. { "longint in set_of_byte" will give a range check error }
  264. { instead of false }
  265. inserttypeconv(left,tsetdef(right.resultdef).elementdef);
  266. { empty set then return false }
  267. if not assigned(tsetdef(right.resultdef).elementdef) or
  268. ((right.nodetype = setconstn) and
  269. (tnormalset(tsetconstnode(right).value_set^) = [])) then
  270. begin
  271. t:=cordconstnode.create(0,pasbool1type,false);
  272. typecheckpass(t);
  273. result:=t;
  274. exit;
  275. end;
  276. result:=simplify(false);
  277. end;
  278. function tinnode.simplify(forinline : boolean):tnode;
  279. var
  280. t : tnode;
  281. begin
  282. result:=nil;
  283. { constant evaluation }
  284. if (left.nodetype=ordconstn) then
  285. begin
  286. if (right.nodetype=setconstn) then
  287. begin
  288. { tordconstnode.value is int64 -> signed -> the expression }
  289. { below will be converted to longint on 32 bit systems due }
  290. { to the rule above -> will give range check error if }
  291. { value > high(longint) if we don't take the signedness }
  292. { into account }
  293. if Tordconstnode(left).value.signed then
  294. t:=cordconstnode.create(byte(tordconstnode(left).value.svalue in Tsetconstnode(right).value_set^),
  295. pasbool1type,true)
  296. else
  297. t:=cordconstnode.create(byte(tordconstnode(left).value.uvalue in Tsetconstnode(right).value_set^),
  298. pasbool1type,true);
  299. typecheckpass(t);
  300. result:=t;
  301. exit;
  302. end
  303. else
  304. begin
  305. if (Tordconstnode(left).value<int64(tsetdef(right.resultdef).setbase)) or
  306. (Tordconstnode(left).value>int64(Tsetdef(right.resultdef).setmax)) then
  307. begin
  308. t:=cordconstnode.create(0, pasbool1type, true);
  309. typecheckpass(t);
  310. result:=t;
  311. exit;
  312. end;
  313. end;
  314. end;
  315. end;
  316. function tinnode.pass_1 : tnode;
  317. begin
  318. result:=nil;
  319. expectloc:=LOC_REGISTER;
  320. firstpass(right);
  321. firstpass(left);
  322. if codegenerror then
  323. exit;
  324. end;
  325. {*****************************************************************************
  326. TRANGENODE
  327. *****************************************************************************}
  328. constructor trangenode.create(l,r : tnode);
  329. var
  330. value: string;
  331. begin
  332. { if right is char and left is string then }
  333. { right should be treated as one-symbol string }
  334. if is_conststringnode(l) and is_constcharnode(r) then
  335. begin
  336. value := char(tordconstnode(r).value.uvalue) + ''#0;
  337. r.free;
  338. r := cstringconstnode.createstr(value);
  339. do_typecheckpass(r);
  340. end;
  341. inherited create(rangen,l,r);
  342. end;
  343. function trangenode.pass_typecheck : tnode;
  344. begin
  345. result:=nil;
  346. typecheckpass(left);
  347. typecheckpass(right);
  348. set_varstate(left,vs_read,[vsf_must_be_valid]);
  349. set_varstate(right,vs_read,[vsf_must_be_valid]);
  350. if codegenerror then
  351. exit;
  352. { both types must be compatible }
  353. if compare_defs(left.resultdef,right.resultdef,left.nodetype)=te_incompatible then
  354. IncompatibleTypes(left.resultdef,right.resultdef);
  355. { Check if only when its a constant set }
  356. if (left.nodetype=ordconstn) and (right.nodetype=ordconstn) then
  357. begin
  358. { upper limit must be greater or equal than lower limit }
  359. if (tordconstnode(left).value>tordconstnode(right).value) and
  360. ((tordconstnode(left).value<0) or (tordconstnode(right).value>=0)) then
  361. CGMessage(parser_e_upper_lower_than_lower);
  362. end;
  363. resultdef:=left.resultdef;
  364. end;
  365. function trangenode.pass_1 : tnode;
  366. begin
  367. result:=nil;
  368. firstpass(left);
  369. firstpass(right);
  370. if codegenerror then
  371. exit;
  372. expectloc:=left.expectloc;
  373. end;
  374. {*****************************************************************************
  375. Case Helpers
  376. *****************************************************************************}
  377. { labels is the number of case-labels, while cases includes each individual
  378. value in a range (e.g. "0..2" counts as 3) }
  379. procedure case_count_labels(root : pcaselabel; out labels, cases: longint);
  380. procedure count(p : pcaselabel);
  381. begin
  382. inc(labels);
  383. inc(cases, (p^._high.svalue - p^._low.svalue) + 1);
  384. if assigned(p^.less) then
  385. count(p^.less);
  386. if assigned(p^.greater) then
  387. count(p^.greater);
  388. end;
  389. begin
  390. labels:=0;
  391. cases:=0;
  392. count(root);
  393. end;
  394. function case_get_max(root : pcaselabel) : tconstexprint;
  395. var
  396. hp : pcaselabel;
  397. begin
  398. hp:=root;
  399. while assigned(hp^.greater) do
  400. hp:=hp^.greater;
  401. case_get_max:=hp^._high;
  402. end;
  403. function case_get_min(root : pcaselabel) : tconstexprint;
  404. var
  405. hp : pcaselabel;
  406. begin
  407. hp:=root;
  408. while assigned(hp^.less) do
  409. hp:=hp^.less;
  410. case_get_min:=hp^._low;
  411. end;
  412. procedure deletecaselabels(p : pcaselabel);
  413. begin
  414. if assigned(p^.greater) then
  415. deletecaselabels(p^.greater);
  416. if assigned(p^.less) then
  417. deletecaselabels(p^.less);
  418. if (p^.label_type = ltConstString) then
  419. begin
  420. p^._low_str.Free;
  421. p^._high_str.Free;
  422. end;
  423. dispose(p);
  424. end;
  425. function copycaselabel(p : pcaselabel) : pcaselabel;
  426. var
  427. n : pcaselabel;
  428. begin
  429. new(n);
  430. n^:=p^;
  431. if (p^.label_type = ltConstString) then
  432. begin
  433. n^._low_str := tstringconstnode(p^._low_str.getcopy);
  434. n^._high_str := tstringconstnode(p^._high_str.getcopy);
  435. end;
  436. if assigned(p^.greater) then
  437. n^.greater:=copycaselabel(p^.greater);
  438. if assigned(p^.less) then
  439. n^.less:=copycaselabel(p^.less);
  440. copycaselabel:=n;
  441. end;
  442. procedure ppuwritecaselabel(ppufile:tcompilerppufile;p : pcaselabel);
  443. var
  444. b : byte;
  445. begin
  446. ppufile.putboolean(p^.label_type = ltConstString);
  447. if (p^.label_type = ltConstString) then
  448. begin
  449. p^._low_str.ppuwrite(ppufile);
  450. p^._high_str.ppuwrite(ppufile);
  451. end
  452. else
  453. begin
  454. ppufile.putexprint(p^._low);
  455. ppufile.putexprint(p^._high);
  456. end;
  457. ppufile.putlongint(p^.blockid);
  458. b:=ord(assigned(p^.greater)) or (ord(assigned(p^.less)) shl 1);
  459. ppufile.putbyte(b);
  460. if assigned(p^.greater) then
  461. ppuwritecaselabel(ppufile,p^.greater);
  462. if assigned(p^.less) then
  463. ppuwritecaselabel(ppufile,p^.less);
  464. end;
  465. function ppuloadcaselabel(ppufile:tcompilerppufile):pcaselabel;
  466. var
  467. b : byte;
  468. p : pcaselabel;
  469. begin
  470. new(p);
  471. if ppufile.getboolean then
  472. begin
  473. p^.label_type := ltConstString;
  474. p^._low_str := cstringconstnode.ppuload(stringconstn,ppufile);
  475. p^._high_str := cstringconstnode.ppuload(stringconstn,ppufile);
  476. end
  477. else
  478. begin
  479. p^.label_type := ltOrdinal;
  480. p^._low:=ppufile.getexprint;
  481. p^._high:=ppufile.getexprint;
  482. end;
  483. p^.blockid:=ppufile.getlongint;
  484. b:=ppufile.getbyte;
  485. if (b and 1)=1 then
  486. p^.greater:=ppuloadcaselabel(ppufile)
  487. else
  488. p^.greater:=nil;
  489. if (b and 2)=2 then
  490. p^.less:=ppuloadcaselabel(ppufile)
  491. else
  492. p^.less:=nil;
  493. ppuloadcaselabel:=p;
  494. end;
  495. {*****************************************************************************
  496. TCASENODE
  497. *****************************************************************************}
  498. constructor tcasenode.create(l:tnode);
  499. begin
  500. inherited create(casen,l);
  501. flabels:=nil;
  502. blocks:=TFPList.create;
  503. elseblock:=nil;
  504. end;
  505. destructor tcasenode.destroy;
  506. var
  507. i : longint;
  508. hp : pcaseblock;
  509. begin
  510. elseblock.free;
  511. deletecaselabels(flabels);
  512. for i:=0 to blocks.count-1 do
  513. begin
  514. pcaseblock(blocks[i])^.statement.free;
  515. hp:=pcaseblock(blocks[i]);
  516. dispose(hp);
  517. end;
  518. blocks.free;
  519. inherited destroy;
  520. end;
  521. constructor tcasenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  522. var
  523. cnt,i : longint;
  524. begin
  525. inherited ppuload(t,ppufile);
  526. elseblock:=ppuloadnode(ppufile);
  527. cnt:=ppufile.getlongint();
  528. blocks:=TFPList.create;
  529. for i:=0 to cnt-1 do
  530. addblock(i,ppuloadnode(ppufile));
  531. flabels:=ppuloadcaselabel(ppufile);
  532. { we don't save/restore the label counts, but recalculate them if needed }
  533. fcountsuptodate:=false;
  534. end;
  535. procedure tcasenode.ppuwrite(ppufile:tcompilerppufile);
  536. var
  537. i : longint;
  538. begin
  539. inherited ppuwrite(ppufile);
  540. ppuwritenode(ppufile,elseblock);
  541. ppufile.putlongint(blocks.count);
  542. for i:=0 to blocks.count-1 do
  543. ppuwritenode(ppufile,pcaseblock(blocks[i])^.statement);
  544. ppuwritecaselabel(ppufile,flabels);
  545. { we don't save/restore the label counts, but recalculate them if needed }
  546. end;
  547. procedure tcasenode.buildderefimpl;
  548. var
  549. i : integer;
  550. begin
  551. inherited buildderefimpl;
  552. if assigned(elseblock) then
  553. elseblock.buildderefimpl;
  554. for i:=0 to blocks.count-1 do
  555. pcaseblock(blocks[i])^.statement.buildderefimpl;
  556. end;
  557. procedure tcasenode.derefimpl;
  558. var
  559. i : integer;
  560. begin
  561. inherited derefimpl;
  562. if assigned(elseblock) then
  563. elseblock.derefimpl;
  564. for i:=0 to blocks.count-1 do
  565. pcaseblock(blocks[i])^.statement.derefimpl;
  566. end;
  567. function tcasenode.pass_typecheck : tnode;
  568. var
  569. i : integer;
  570. begin
  571. result:=nil;
  572. do_typecheckpass(left);
  573. for i:=0 to blocks.count-1 do
  574. typecheckpass(pcaseblock(blocks[i])^.statement);
  575. if assigned(elseblock) then
  576. typecheckpass(elseblock);
  577. if not codegenerror and
  578. is_ordinal(left.resultdef) then
  579. checkordinalcoverage;
  580. resultdef:=voidtype;
  581. end;
  582. type
  583. TLinkedListCaseLabelItem = class(TLinkedListItem)
  584. casenode: pcaselabel;
  585. constructor create(c: pcaselabel);
  586. end;
  587. constructor TLinkedListCaseLabelItem.create(c: pcaselabel);
  588. begin
  589. inherited create;
  590. casenode:=c;
  591. end;
  592. function tcasenode.pass_1 : tnode;
  593. var
  594. i: integer;
  595. node_thenblock, node_elseblock, if_node,temp_cleanup : tnode;
  596. tempcaseexpr : ttempcreatenode;
  597. if_block, init_block: tblocknode;
  598. stmt: tstatementnode;
  599. procedure add_label_to_blockid_list(list: tfpobjectlist; lab: pcaselabel);
  600. begin
  601. if not assigned(lab) then
  602. exit;
  603. if not assigned(list[lab^.blockid]) then
  604. list[lab^.blockid]:=tfpobjectlist.create(true);
  605. tfpobjectlist(list[lab^.blockid]).add(TLinkedListCaseLabelItem.create(lab));
  606. add_label_to_blockid_list(list,lab^.less);
  607. add_label_to_blockid_list(list,lab^.greater);
  608. end;
  609. function order_labels_by_blockid: tfpobjectlist;
  610. begin
  611. result:=tfpobjectlist.create(true);
  612. result.count:=blocks.count;
  613. add_label_to_blockid_list(result,flabels);
  614. end;
  615. function makeifblock(elseblock : tnode): tnode;
  616. var
  617. i, j: longint;
  618. check: taddnode;
  619. newcheck: ^taddnode;
  620. blocklist, lablist: tfpobjectlist;
  621. labitem: pcaselabel;
  622. begin
  623. result:=elseblock;
  624. blocklist:=order_labels_by_blockid;
  625. { in reverse order so that the case options at the start of the case
  626. statement are evaluated first, as they presumably are the most
  627. common }
  628. for i:=blocklist.count-1 downto 0 do
  629. begin
  630. lablist:=tfpobjectlist(blocklist[i]);
  631. check:=nil;
  632. for j:=0 to lablist.count-1 do
  633. begin
  634. if assigned(check) then
  635. begin
  636. check:=caddnode.create(orn,check,nil);
  637. newcheck:[email protected]
  638. end
  639. else
  640. newcheck:=@check;
  641. labitem:=TLinkedListCaseLabelItem(lablist[j]).casenode;
  642. newcheck^:=caddnode.create(equaln,left.getcopy,labitem^._low_str.getcopy);
  643. if (labitem^._low_str.fullcompare(labitem^._high_str)<>0) then
  644. begin
  645. newcheck^.nodetype:=gten;
  646. newcheck^:=caddnode.create(
  647. andn,newcheck^,caddnode.create(
  648. lten,left.getcopy,labitem^._high_str.getcopy));
  649. end;
  650. end;
  651. result:=cifnode.create(check,
  652. pcaseblock(blocks[i])^.statement,result);
  653. pcaseblock(blocks[i])^.statement:=nil;
  654. end;
  655. { will free its elements too because of create(true) }
  656. blocklist.free;
  657. typecheckpass(result);
  658. end;
  659. begin
  660. result:=nil;
  661. init_block:=nil;
  662. temp_cleanup:=nil;
  663. expectloc:=LOC_VOID;
  664. { evalutes the case expression }
  665. firstpass(left);
  666. set_varstate(left,vs_read,[vsf_must_be_valid]);
  667. if codegenerror then
  668. exit;
  669. { Load caseexpr into temp var if complex. }
  670. { No need to do this for ordinal, because }
  671. { in that case caseexpr is generated once }
  672. if (flabels^.label_type = ltConstString) and (not valid_for_addr(left, false)) and
  673. (blocks.count > 0) then
  674. begin
  675. init_block := internalstatements(stmt);
  676. tempcaseexpr :=
  677. ctempcreatenode.create(
  678. left.resultdef, left.resultdef.size, tt_persistent, true);
  679. temp_cleanup := ctempdeletenode.create(tempcaseexpr);
  680. typecheckpass(tnode(tempcaseexpr));
  681. addstatement(stmt, tempcaseexpr);
  682. addstatement(
  683. stmt, cassignmentnode.create(
  684. ctemprefnode.create(tempcaseexpr), left));
  685. left := ctemprefnode.create(tempcaseexpr);
  686. typecheckpass(left);
  687. end;
  688. { first case }
  689. for i:=0 to blocks.count-1 do
  690. firstpass(pcaseblock(blocks[i])^.statement);
  691. { may be handle else tree }
  692. if assigned(elseblock) then
  693. begin
  694. firstpass(elseblock);
  695. { kill case? }
  696. if blocks.count=0 then
  697. begin
  698. result:=elseblock;
  699. elseblock:=nil;
  700. exit;
  701. end;
  702. end
  703. else
  704. if blocks.count=0 then
  705. begin
  706. result:=cnothingnode.create;
  707. exit;
  708. end;
  709. if (flabels^.label_type = ltConstString) then
  710. begin
  711. if_node:=makeifblock(elseblock);
  712. if assigned(init_block) then
  713. firstpass(tnode(init_block));
  714. if_block:=internalstatements(stmt);
  715. if assigned(init_block) then
  716. addstatement(stmt, init_block);
  717. addstatement(stmt,if_node);
  718. if assigned(temp_cleanup) then
  719. addstatement(stmt,temp_cleanup);
  720. result:=if_block;
  721. elseblock:= nil;
  722. exit;
  723. end;
  724. if is_boolean(left.resultdef) then
  725. begin
  726. case blocks.count of
  727. 2:
  728. begin
  729. if boolean(qword(flabels^._low))=false then
  730. begin
  731. node_thenblock:=pcaseblock(blocks[flabels^.greater^.blockid])^.statement;
  732. node_elseblock:=pcaseblock(blocks[flabels^.blockid])^.statement;
  733. pcaseblock(blocks[flabels^.greater^.blockid])^.statement:=nil;
  734. end
  735. else
  736. begin
  737. node_thenblock:=pcaseblock(blocks[flabels^.blockid])^.statement;
  738. node_elseblock:=pcaseblock(blocks[flabels^.less^.blockid])^.statement;
  739. pcaseblock(blocks[flabels^.less^.blockid])^.statement:=nil;
  740. end;
  741. pcaseblock(blocks[flabels^.blockid])^.statement:=nil;
  742. end;
  743. 1:
  744. begin
  745. if flabels^._low=flabels^._high then
  746. begin
  747. if boolean(qword(flabels^._low))=false then
  748. begin
  749. node_thenblock:=elseblock;
  750. node_elseblock:=pcaseblock(blocks[flabels^.blockid])^.statement;
  751. end
  752. else
  753. begin
  754. node_thenblock:=pcaseblock(blocks[flabels^.blockid])^.statement;
  755. node_elseblock:=elseblock;
  756. end;
  757. pcaseblock(blocks[flabels^.blockid])^.statement:=nil;
  758. elseblock:=nil;
  759. end
  760. else
  761. begin
  762. result:=pcaseblock(blocks[flabels^.blockid])^.statement;
  763. pcaseblock(blocks[flabels^.blockid])^.statement:=nil;
  764. elseblock:=nil;
  765. exit;
  766. end;
  767. end;
  768. else
  769. internalerror(200805031);
  770. end;
  771. result:=cifnode.create(left,node_thenblock,node_elseblock);
  772. left:=nil;
  773. end;
  774. end;
  775. function tcasenode.simplify(forinline:boolean):tnode;
  776. var
  777. tmp: pcaselabel;
  778. begin
  779. result:=nil;
  780. if left.nodetype=ordconstn then
  781. begin
  782. tmp:=flabels;
  783. { check all case labels until we find one that fits }
  784. while assigned(tmp) do
  785. begin
  786. if (tmp^._low<=tordconstnode(left).value) and
  787. (tmp^._high>=tordconstnode(left).value) then
  788. begin
  789. if tmp^.blockid>=blocks.count then
  790. internalerror(2014022101);
  791. result:=pcaseblock(blocks[tmp^.blockid])^.statement;
  792. if not assigned(result) then
  793. internalerror(2014022102);
  794. result:=result.getcopy;
  795. exit;
  796. end;
  797. if tmp^._high<tordconstnode(left).value then
  798. tmp:=tmp^.greater
  799. else
  800. tmp:=tmp^.less;
  801. end;
  802. { no label did match; use the else block if available }
  803. if assigned(elseblock) then
  804. result:=elseblock.getcopy
  805. else
  806. { no else block, so there is no code to execute at all }
  807. result:=cnothingnode.create;
  808. end;
  809. if assigned(elseblock) and
  810. has_no_code(elseblock) then
  811. begin
  812. elseblock.free;
  813. elseblock:=nil;
  814. end;
  815. end;
  816. function tcasenode.dogetcopy : tnode;
  817. var
  818. n : tcasenode;
  819. i : longint;
  820. begin
  821. n:=tcasenode(inherited dogetcopy);
  822. if assigned(elseblock) then
  823. n.elseblock:=elseblock.dogetcopy
  824. else
  825. n.elseblock:=nil;
  826. if assigned(flabels) then
  827. n.flabels:=copycaselabel(flabels)
  828. else
  829. n.flabels:=nil;
  830. if assigned(blocks) then
  831. begin
  832. n.blocks:=TFPList.create;
  833. for i:=0 to blocks.count-1 do
  834. begin
  835. if not assigned(blocks[i]) then
  836. internalerror(200411302);
  837. n.addblock(i,pcaseblock(blocks[i])^.statement.dogetcopy);
  838. end;
  839. end
  840. else
  841. n.blocks:=nil;
  842. n.fcountsuptodate:=fcountsuptodate;
  843. n.flabelcnt:=flabelcnt;
  844. n.flabelcoverage:=flabelcoverage;
  845. dogetcopy:=n;
  846. end;
  847. procedure tcasenode.printnodetree(var t: text);
  848. var
  849. i : longint;
  850. begin
  851. write(t,printnodeindention,'(');
  852. printnodeindent;
  853. printnodeinfo(t);
  854. writeln(t);
  855. printnode(t,left);
  856. i:=0;
  857. for i:=0 to blocks.count-1 do
  858. begin
  859. writeln(t,printnodeindention,'(caseblock blockid: ',i);
  860. printnodeindent;
  861. printnode(t,pcaseblock(blocks[i])^.statement);
  862. printnodeunindent;
  863. writeln(t,printnodeindention,')');
  864. end;
  865. if assigned(elseblock) then
  866. begin
  867. writeln(t,printnodeindention,'(else: ',i);
  868. printnodeindent;
  869. printnode(t,elseblock);
  870. printnodeunindent;
  871. writeln(t,printnodeindention,')');
  872. end;
  873. printnodeunindent;
  874. writeln(t,printnodeindention,')');
  875. end;
  876. procedure tcasenode.insertintolist(l : tnodelist);
  877. begin
  878. end;
  879. function caselabelsequal(n1,n2: pcaselabel): boolean;
  880. begin
  881. result :=
  882. (not assigned(n1) and not assigned(n2)) or
  883. (assigned(n1) and assigned(n2) and
  884. (n1^._low = n2^._low) and
  885. (n1^._high = n2^._high) and
  886. { the rest of the fields don't matter for equality (JM) }
  887. caselabelsequal(n1^.less,n2^.less) and
  888. caselabelsequal(n1^.greater,n2^.greater))
  889. end;
  890. function caseblocksequal(b1,b2:TFPList): boolean;
  891. var
  892. i : longint;
  893. begin
  894. result:=false;
  895. if b1.count<>b2.count then
  896. exit;
  897. for i:=0 to b1.count-1 do
  898. begin
  899. if not pcaseblock(b1[i])^.statement.isequal(pcaseblock(b2[i])^.statement) then
  900. exit;
  901. end;
  902. result:=true;
  903. end;
  904. function tcasenode.docompare(p: tnode): boolean;
  905. begin
  906. result :=
  907. inherited docompare(p) and
  908. caselabelsequal(flabels,tcasenode(p).flabels) and
  909. caseblocksequal(blocks,tcasenode(p).blocks) and
  910. elseblock.isequal(tcasenode(p).elseblock);
  911. end;
  912. procedure tcasenode.addblock(blockid:longint;instr:tnode);
  913. var
  914. hcaseblock : pcaseblock;
  915. begin
  916. new(hcaseblock);
  917. fillchar(hcaseblock^,sizeof(hcaseblock^),0);
  918. hcaseblock^.statement:=instr;
  919. if blockid>=blocks.count then
  920. blocks.count:=blockid+1;
  921. blocks[blockid]:=hcaseblock;
  922. end;
  923. procedure tcasenode.addelseblock(instr:tnode);
  924. begin
  925. elseblock:=instr;
  926. end;
  927. function tcasenode.getlabelcnt: cardinal;
  928. begin
  929. if not fcountsuptodate then
  930. updatecoverage;
  931. result:=flabelcnt;
  932. end;
  933. function tcasenode.getlabelcoverage: qword;
  934. begin
  935. if not fcountsuptodate then
  936. updatecoverage;
  937. result:=flabelcoverage;
  938. end;
  939. procedure tcasenode.updatecoverage;
  940. var
  941. isord: boolean;
  942. procedure count(p : pcaselabel);
  943. begin
  944. inc(flabelcnt);
  945. if isord then
  946. inc(flabelcoverage, (p^._high.svalue - p^._low.svalue) + 1);
  947. if assigned(p^.less) then
  948. count(p^.less);
  949. if assigned(p^.greater) then
  950. count(p^.greater);
  951. end;
  952. begin
  953. isord:=is_ordinal(left.resultdef);
  954. flabelcnt:=0;
  955. flabelcoverage:=0;
  956. count(flabels);
  957. fcountsuptodate:=true;
  958. end;
  959. procedure tcasenode.checkordinalcoverage;
  960. function orddefspansfullrange(def: torddef): boolean;
  961. var
  962. packedbitsize: cardinal;
  963. dummy: longint;
  964. val: qword;
  965. begin
  966. result:=false;
  967. packedbitsize:=def.packedbitsize;
  968. if ((packedbitsize mod 8) <> 0) or
  969. not ispowerof2(packedbitsize div 8,dummy) then
  970. exit;
  971. dec(packedbitsize);
  972. if is_signed(def) then
  973. begin
  974. if def.low<>(-(int64(1) shl packedbitsize)) then
  975. exit;
  976. if def.high<>((int64(1) shl packedbitsize)-1) then
  977. exit;
  978. end
  979. else
  980. begin
  981. if def.low<>0 then
  982. exit;
  983. val:=qword(1) shl packedbitsize;
  984. val:=(val-1)+val;
  985. if def.high<>val then
  986. exit;
  987. end;
  988. result:=true;
  989. end;
  990. var
  991. lv, hv, typcount: tconstexprint;
  992. begin
  993. { Check label type coverage for enumerations and small types }
  994. getrange(left.resultdef,lv,hv);
  995. typcount:=hv.svalue-lv.svalue+1;
  996. if not assigned(elseblock) then
  997. begin
  998. { unless cs_check_all_case_coverage is set, only check for enums, booleans and
  999. subrange types different from the default ones }
  1000. if (cs_check_all_case_coverage in current_settings.localswitches) or
  1001. (is_enum(left.resultdef) or
  1002. is_boolean(left.resultdef) or
  1003. not orddefspansfullrange(torddef(left.resultdef))) and
  1004. (labelcoverage<typcount) then
  1005. begin
  1006. { labels for some values of the operand are missing, and no else block is present }
  1007. if not(m_iso in current_settings.modeswitches) then
  1008. begin
  1009. cgmessage(cg_w_case_incomplete);
  1010. { in Extended Pascal, this is a dynamic violation error if it actually happens }
  1011. if (m_extpas in current_settings.modeswitches) then
  1012. elseblock:=ccallnode.createintern('fpc_rangeerror',nil);
  1013. end
  1014. else
  1015. { this is an error in ISO Pascal }
  1016. message(cg_e_case_incomplete);
  1017. end
  1018. end
  1019. else if labelcoverage=typcount then
  1020. begin
  1021. { labels for all values of the operand are present, but an extra else block is present }
  1022. MessagePos(elseblock.fileinfo, cg_w_unreachable_code);
  1023. end;
  1024. end;
  1025. procedure tcasenode.addlabel(blockid:longint;const l,h : TConstExprInt);
  1026. var
  1027. hcaselabel : pcaselabel;
  1028. function insertlabel(var p : pcaselabel):pcaselabel;
  1029. begin
  1030. if p=nil then
  1031. begin
  1032. p:=hcaselabel;
  1033. result:=p;
  1034. end
  1035. else
  1036. if (p^._low>hcaselabel^._low) and
  1037. (p^._low>hcaselabel^._high) then
  1038. begin
  1039. if (hcaselabel^.blockid = p^.blockid) and
  1040. (p^._low = hcaselabel^._high + 1) then
  1041. begin
  1042. p^._low := hcaselabel^._low;
  1043. dispose(hcaselabel);
  1044. result:=p;
  1045. end
  1046. else
  1047. result:=insertlabel(p^.less)
  1048. end
  1049. else
  1050. if (p^._high<hcaselabel^._low) and
  1051. (p^._high<hcaselabel^._high) then
  1052. begin
  1053. if (hcaselabel^.blockid = p^.blockid) and
  1054. (p^._high+1 = hcaselabel^._low) then
  1055. begin
  1056. p^._high := hcaselabel^._high;
  1057. dispose(hcaselabel);
  1058. result:=p;
  1059. end
  1060. else
  1061. result:=insertlabel(p^.greater);
  1062. end
  1063. else
  1064. begin
  1065. dispose(hcaselabel);
  1066. Message(parser_e_double_caselabel);
  1067. result:=nil;
  1068. end
  1069. end;
  1070. begin
  1071. new(hcaselabel);
  1072. fillchar(hcaselabel^,sizeof(tcaselabel),0);
  1073. hcaselabel^.blockid:=blockid;
  1074. hcaselabel^.label_type:=ltOrdinal;
  1075. hcaselabel^._low:=l;
  1076. hcaselabel^._high:=h;
  1077. insertlabel(flabels);
  1078. fcountsuptodate:=false;
  1079. end;
  1080. procedure tcasenode.addlabel(blockid: longint; l, h: tstringconstnode);
  1081. var
  1082. hcaselabel : pcaselabel;
  1083. function insertlabel(var p : pcaselabel) : pcaselabel;
  1084. begin
  1085. if not assigned(p) then
  1086. begin
  1087. p := hcaselabel;
  1088. result := p;
  1089. end
  1090. else
  1091. if (p^._low_str.fullcompare(hcaselabel^._high_str) > 0) then
  1092. result := insertlabel(p^.less)
  1093. else
  1094. if (p^._high_str.fullcompare(hcaselabel^._low_str) < 0) then
  1095. result := insertlabel(p^.greater)
  1096. else
  1097. begin
  1098. hcaselabel^._low_str.free;
  1099. hcaselabel^._high_str.free;
  1100. dispose(hcaselabel);
  1101. Message(parser_e_double_caselabel);
  1102. result:=nil;
  1103. end;
  1104. end;
  1105. begin
  1106. new(hcaselabel);
  1107. fillchar(hcaselabel^, sizeof(tcaselabel), 0);
  1108. hcaselabel^.blockid := blockid;
  1109. hcaselabel^.label_type := ltConstString;
  1110. hcaselabel^._low_str := tstringconstnode(l.getcopy);
  1111. hcaselabel^._high_str := tstringconstnode(h.getcopy);
  1112. insertlabel(flabels);
  1113. end;
  1114. end.