nset.pas 40 KB

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