nset.pas 44 KB

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