nset.pas 44 KB

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