nset.pas 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373
  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. exit;
  791. end;
  792. { convert single case branch into if-statement }
  793. if (flabels^.greater=nil) and (flabels^.less=nil) then
  794. if flabels^.label_type=ltOrdinal then
  795. begin
  796. if flabels^._low=flabels^._high then
  797. begin
  798. result:=cifnode.create_internal(
  799. caddnode.create_internal(equaln,left.getcopy,cordconstnode.create(flabels^._low,left.resultdef,false)),
  800. pcaseblock(blocks[flabels^.blockid])^.statement.getcopy,elseblock);
  801. end
  802. else
  803. begin
  804. result:=cifnode.create_internal(
  805. caddnode.create_internal(andn,
  806. caddnode.create_internal(gten,left.getcopy,cordconstnode.create(flabels^._low,left.resultdef,false)),
  807. caddnode.create_internal(lten,left.getcopy,cordconstnode.create(flabels^._high,left.resultdef,false))
  808. ),
  809. pcaseblock(blocks[flabels^.blockid])^.statement.getcopy,elseblock);
  810. end;
  811. elseblock:=nil;
  812. exit;
  813. end;
  814. end;
  815. function tcasenode.simplify(forinline:boolean):tnode;
  816. var
  817. tmp: pcaselabel;
  818. begin
  819. result:=nil;
  820. if left.nodetype=ordconstn then
  821. begin
  822. tmp:=flabels;
  823. { check all case labels until we find one that fits }
  824. while assigned(tmp) do
  825. begin
  826. if (tmp^._low<=tordconstnode(left).value) and
  827. (tmp^._high>=tordconstnode(left).value) then
  828. begin
  829. if tmp^.blockid>=blocks.count then
  830. internalerror(2014022101);
  831. result:=pcaseblock(blocks[tmp^.blockid])^.statement;
  832. if not assigned(result) then
  833. internalerror(2014022102);
  834. result:=result.getcopy;
  835. exit;
  836. end;
  837. if tmp^._high<tordconstnode(left).value then
  838. tmp:=tmp^.greater
  839. else
  840. tmp:=tmp^.less;
  841. end;
  842. { no label did match; use the else block if available }
  843. if assigned(elseblock) then
  844. result:=elseblock.getcopy
  845. else
  846. begin
  847. if ([m_iso,m_extpas]*current_settings.modeswitches)<>[] then
  848. cgmessage1(cg_e_case_missing_value,tostr(tordconstnode(left).value))
  849. else
  850. cgmessage(cg_w_case_incomplete);
  851. { no else block, so there is no code to execute at all }
  852. result:=cnothingnode.create;
  853. end;
  854. end;
  855. end;
  856. function tcasenode.dogetcopy : tnode;
  857. var
  858. n : tcasenode;
  859. i : longint;
  860. begin
  861. n:=tcasenode(inherited dogetcopy);
  862. if assigned(elseblock) then
  863. n.elseblock:=elseblock.dogetcopy
  864. else
  865. n.elseblock:=nil;
  866. if assigned(flabels) then
  867. n.flabels:=copycaselabel(flabels)
  868. else
  869. n.flabels:=nil;
  870. if assigned(blocks) then
  871. begin
  872. n.blocks:=TFPList.create;
  873. for i:=0 to blocks.count-1 do
  874. begin
  875. if not assigned(blocks[i]) then
  876. internalerror(200411302);
  877. n.addblock(i,pcaseblock(blocks[i])^.statement.dogetcopy);
  878. end;
  879. end
  880. else
  881. n.blocks:=nil;
  882. n.fcountsuptodate:=fcountsuptodate;
  883. n.flabelcnt:=flabelcnt;
  884. n.flabelcoverage:=flabelcoverage;
  885. dogetcopy:=n;
  886. end;
  887. procedure tcasenode.printnodetree(var t: text);
  888. var
  889. i : longint;
  890. begin
  891. write(t,printnodeindention,'(');
  892. printnodeindent;
  893. printnodeinfo(t);
  894. writeln(t);
  895. printnode(t,left);
  896. i:=0;
  897. for i:=0 to blocks.count-1 do
  898. begin
  899. writeln(t,printnodeindention,'(caseblock blockid: ',i);
  900. printnodeindent;
  901. printnode(t,pcaseblock(blocks[i])^.statement);
  902. printnodeunindent;
  903. writeln(t,printnodeindention,')');
  904. end;
  905. if assigned(elseblock) then
  906. begin
  907. writeln(t,printnodeindention,'(else: ',i);
  908. printnodeindent;
  909. printnode(t,elseblock);
  910. printnodeunindent;
  911. writeln(t,printnodeindention,')');
  912. end;
  913. printnodeunindent;
  914. writeln(t,printnodeindention,')');
  915. end;
  916. {$ifdef DEBUG_NODE_XML}
  917. procedure TCaseNode.XMLPrintNodeTree(var T: Text);
  918. var
  919. i : longint;
  920. begin
  921. Write(T, PrintNodeIndention, '<', nodetype2str[nodetype]);
  922. XMLPrintNodeInfo(T);
  923. WriteLn(T, '>');
  924. PrintNodeIndent;
  925. WriteLn(T, PrintNodeIndention, '<condition>');
  926. PrintNodeIndent;
  927. XMLPrintNode(T, Left);
  928. PrintNodeUnindent;
  929. WriteLn(T, PrintNodeIndention, '</condition>');
  930. i:=0;
  931. for i:=0 to blocks.count-1 do
  932. begin
  933. WriteLn(T, PrintNodeIndention, '<block id="', i, '">');
  934. PrintNodeIndent;
  935. XMLPrintNode(T, PCaseBlock(blocks[i])^.statement);
  936. PrintNodeUnindent;
  937. WriteLn(T, PrintNodeIndention, '</block>');
  938. end;
  939. if assigned(elseblock) then
  940. begin
  941. WriteLn(T, PrintNodeIndention, '<block id="else">');;
  942. PrintNodeIndent;
  943. XMLPrintNode(T, ElseBlock);
  944. PrintNodeUnindent;
  945. WriteLn(T, PrintNodeIndention, '</block>');
  946. end;
  947. PrintNodeUnindent;
  948. WriteLn(T, PrintNodeIndention, '</', nodetype2str[nodetype], '>');
  949. end;
  950. {$endif DEBUG_NODE_XML}
  951. procedure tcasenode.insertintolist(l : tnodelist);
  952. begin
  953. end;
  954. function caselabelsequal(n1,n2: pcaselabel): boolean;
  955. begin
  956. result :=
  957. (not assigned(n1) and not assigned(n2)) or
  958. (assigned(n1) and assigned(n2) and
  959. (n1^._low = n2^._low) and
  960. (n1^._high = n2^._high) and
  961. { the rest of the fields don't matter for equality (JM) }
  962. caselabelsequal(n1^.less,n2^.less) and
  963. caselabelsequal(n1^.greater,n2^.greater))
  964. end;
  965. function caseblocksequal(b1,b2:TFPList): boolean;
  966. var
  967. i : longint;
  968. begin
  969. result:=false;
  970. if b1.count<>b2.count then
  971. exit;
  972. for i:=0 to b1.count-1 do
  973. begin
  974. if not pcaseblock(b1[i])^.statement.isequal(pcaseblock(b2[i])^.statement) then
  975. exit;
  976. end;
  977. result:=true;
  978. end;
  979. function tcasenode.docompare(p: tnode): boolean;
  980. begin
  981. result :=
  982. inherited docompare(p) and
  983. caselabelsequal(flabels,tcasenode(p).flabels) and
  984. caseblocksequal(blocks,tcasenode(p).blocks) and
  985. elseblock.isequal(tcasenode(p).elseblock);
  986. end;
  987. procedure tcasenode.addblock(blockid:longint;instr:tnode);
  988. var
  989. hcaseblock : pcaseblock;
  990. begin
  991. new(hcaseblock);
  992. fillchar(hcaseblock^,sizeof(hcaseblock^),0);
  993. hcaseblock^.statement:=instr;
  994. if blockid>=blocks.count then
  995. blocks.count:=blockid+1;
  996. blocks[blockid]:=hcaseblock;
  997. end;
  998. procedure tcasenode.addelseblock(instr:tnode);
  999. begin
  1000. elseblock:=instr;
  1001. end;
  1002. function tcasenode.getlabelcnt: cardinal;
  1003. begin
  1004. if not fcountsuptodate then
  1005. updatecoverage;
  1006. result:=flabelcnt;
  1007. end;
  1008. function tcasenode.getlabelcoverage: qword;
  1009. begin
  1010. if not fcountsuptodate then
  1011. updatecoverage;
  1012. result:=flabelcoverage;
  1013. end;
  1014. procedure tcasenode.updatecoverage;
  1015. var
  1016. isord, first: boolean;
  1017. procedure count(p : pcaselabel);
  1018. begin
  1019. inc(flabelcnt);
  1020. if isord then
  1021. begin
  1022. flabelcoverage:=flabelcoverage + (p^._high - p^._low);
  1023. { ensure we don't overflow in case it covers the
  1024. full range of qword }
  1025. if not first then
  1026. inc(flabelcoverage);
  1027. first:=false;
  1028. end;
  1029. if assigned(p^.less) then
  1030. count(p^.less);
  1031. if assigned(p^.greater) then
  1032. count(p^.greater);
  1033. end;
  1034. begin
  1035. isord:=is_ordinal(left.resultdef);
  1036. flabelcnt:=0;
  1037. flabelcoverage:=0;
  1038. first:=true;
  1039. count(flabels);
  1040. fcountsuptodate:=true;
  1041. end;
  1042. procedure tcasenode.checkordinalcoverage;
  1043. function orddefspansfullrange(def: torddef): boolean;
  1044. var
  1045. packedbitsize: cardinal;
  1046. dummy: longint;
  1047. val: qword;
  1048. begin
  1049. result:=false;
  1050. packedbitsize:=def.packedbitsize;
  1051. if ((packedbitsize mod 8) <> 0) or
  1052. not ispowerof2(packedbitsize div 8,dummy) then
  1053. exit;
  1054. dec(packedbitsize);
  1055. if is_signed(def) then
  1056. begin
  1057. {$push}{$q-}
  1058. if def.low<>(-(int64(1) shl packedbitsize)) then
  1059. exit;
  1060. if def.high<>((int64(1) shl packedbitsize)-1) then
  1061. exit;
  1062. {$pop}
  1063. end
  1064. else
  1065. begin
  1066. if def.low<>0 then
  1067. exit;
  1068. val:=qword(1) shl packedbitsize;
  1069. val:=(val-1)+val;
  1070. if def.high<>val then
  1071. exit;
  1072. end;
  1073. result:=true;
  1074. end;
  1075. var
  1076. lv, hv, typcount: tconstexprint;
  1077. begin
  1078. { Check label type coverage for enumerations and small types }
  1079. getrange(left.resultdef,lv,hv);
  1080. typcount:=hv-lv;
  1081. if not assigned(elseblock) then
  1082. begin
  1083. { unless cs_check_all_case_coverage is set, only check for enums, booleans and
  1084. subrange types different from the default ones }
  1085. if (cs_check_all_case_coverage in current_settings.localswitches) or
  1086. (is_enum(left.resultdef) or
  1087. is_boolean(left.resultdef) or
  1088. not orddefspansfullrange(torddef(left.resultdef))) and
  1089. (labelcoverage<typcount) then
  1090. begin
  1091. { labels for some values of the operand are missing, and no else block is present }
  1092. cgmessage(cg_w_case_incomplete);
  1093. { in Standard/Extended Pascal, this is a dynamic violation error if it actually happens }
  1094. if ([m_extpas,m_iso]*current_settings.modeswitches)<>[] then
  1095. begin
  1096. elseblock:=ccallnode.createintern('fpc_rangeerror',nil);
  1097. typecheckpass(elseblock);
  1098. end;
  1099. end
  1100. end
  1101. else if labelcoverage=typcount then
  1102. begin
  1103. { labels for all values of the operand are present, but an extra else block is present }
  1104. MessagePos(elseblock.fileinfo, cg_w_unreachable_code);
  1105. end;
  1106. end;
  1107. procedure tcasenode.addlabel(blockid:longint;const l,h : TConstExprInt);
  1108. var
  1109. hcaselabel : pcaselabel;
  1110. function insertlabel(var p : pcaselabel):pcaselabel;
  1111. begin
  1112. if p=nil then
  1113. begin
  1114. p:=hcaselabel;
  1115. result:=p;
  1116. end
  1117. else
  1118. if (p^._low>hcaselabel^._low) and
  1119. (p^._low>hcaselabel^._high) then
  1120. begin
  1121. if (hcaselabel^.blockid = p^.blockid) and
  1122. (p^._low = hcaselabel^._high + 1) then
  1123. begin
  1124. p^._low := hcaselabel^._low;
  1125. dispose(hcaselabel);
  1126. result:=p;
  1127. end
  1128. else
  1129. result:=insertlabel(p^.less)
  1130. end
  1131. else
  1132. if (p^._high<hcaselabel^._low) and
  1133. (p^._high<hcaselabel^._high) then
  1134. begin
  1135. if (hcaselabel^.blockid = p^.blockid) and
  1136. (p^._high+1 = hcaselabel^._low) then
  1137. begin
  1138. p^._high := hcaselabel^._high;
  1139. dispose(hcaselabel);
  1140. result:=p;
  1141. end
  1142. else
  1143. result:=insertlabel(p^.greater);
  1144. end
  1145. else
  1146. begin
  1147. dispose(hcaselabel);
  1148. Message(parser_e_double_caselabel);
  1149. result:=nil;
  1150. end
  1151. end;
  1152. begin
  1153. new(hcaselabel);
  1154. fillchar(hcaselabel^,sizeof(tcaselabel),0);
  1155. hcaselabel^.blockid:=blockid;
  1156. hcaselabel^.label_type:=ltOrdinal;
  1157. hcaselabel^._low:=l;
  1158. hcaselabel^._high:=h;
  1159. insertlabel(flabels);
  1160. fcountsuptodate:=false;
  1161. end;
  1162. procedure tcasenode.addlabel(blockid: longint; l, h: tstringconstnode);
  1163. var
  1164. hcaselabel : pcaselabel;
  1165. function insertlabel(var p : pcaselabel) : pcaselabel;
  1166. begin
  1167. if not assigned(p) then
  1168. begin
  1169. p := hcaselabel;
  1170. result := p;
  1171. end
  1172. else
  1173. if (p^._low_str.fullcompare(hcaselabel^._high_str) > 0) then
  1174. result := insertlabel(p^.less)
  1175. else
  1176. if (p^._high_str.fullcompare(hcaselabel^._low_str) < 0) then
  1177. result := insertlabel(p^.greater)
  1178. else
  1179. begin
  1180. hcaselabel^._low_str.free;
  1181. hcaselabel^._high_str.free;
  1182. dispose(hcaselabel);
  1183. Message(parser_e_double_caselabel);
  1184. result:=nil;
  1185. end;
  1186. end;
  1187. begin
  1188. new(hcaselabel);
  1189. fillchar(hcaselabel^, sizeof(tcaselabel), 0);
  1190. hcaselabel^.blockid := blockid;
  1191. hcaselabel^.label_type := ltConstString;
  1192. hcaselabel^._low_str := tstringconstnode(l.getcopy);
  1193. hcaselabel^._high_str := tstringconstnode(h.getcopy);
  1194. insertlabel(flabels);
  1195. end;
  1196. end.