nset.pas 42 KB

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