nset.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. {
  2. $Id$
  3. Copyright (c) 2000 by Florian Klaempfl
  4. Type checking and register allocation for set/case nodes
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit nset;
  19. {$i defines.inc}
  20. interface
  21. uses
  22. node;
  23. type
  24. pcaserecord = ^tcaserecord;
  25. tcaserecord = record
  26. { range }
  27. _low,_high : TConstExprInt;
  28. { only used by gentreejmp }
  29. _at : pasmlabel;
  30. { label of instruction }
  31. statement : pasmlabel;
  32. { is this the first of an case entry, needed to release statement
  33. label (PFV) }
  34. firstlabel : boolean;
  35. { left and right tree node }
  36. less,greater : pcaserecord;
  37. end;
  38. tsetelementnode = class(tbinarynode)
  39. constructor create(l,r : tnode);virtual;
  40. function pass_1 : tnode;override;
  41. end;
  42. tinnode = class(tbinopnode);
  43. constructor create(l,r : tnode);virtual;
  44. function pass_1 : tnode;override;
  45. end;
  46. trangenode = class(tbinarynode)
  47. constructor create(l,r : tnode);virtual;
  48. function pass_1 : tnode;override;
  49. end;
  50. tcasenode = class(tbinarynode)
  51. nodes : pcaserecord;
  52. elseblock : ptree;
  53. constructor create(l,r : tnode;n : pnodes);virtual;
  54. destructor destroy;override;
  55. function getcopy : tnode;override;
  56. end;
  57. var
  58. csetelementnode : class of tsetelementnode;
  59. cinnode : class of tinnode;
  60. crangenode : class of trangenode;
  61. ccasenode : class of tcasenode;
  62. { counts the labels }
  63. function case_count_labels(root : pcaserecord) : longint;
  64. { searches the highest label }
  65. function case_get_max(root : pcaserecord) : longint;
  66. { searches the lowest label }
  67. function case_get_min(root : pcaserecord) : longint;
  68. function gencasenode(l,r : tnode;nodes : pcaserecord) : tnode;
  69. implementation
  70. uses
  71. globtype,systems,
  72. cobjects,verbose,globals,
  73. symconst,symtable,aasm,types,
  74. htypechk,pass_1,
  75. ncnv,ncon,cpubase
  76. {$ifdef newcg}
  77. ,cgbase
  78. ,tgcpu
  79. {$else newcg}
  80. ,hcodegen
  81. {$ifdef i386}
  82. ,tgeni386
  83. {$endif}
  84. {$ifdef m68k}
  85. ,tgen68k
  86. {$endif}
  87. {$endif newcg}
  88. ;
  89. function gencasenode(l,r : ptree;nodes : pcaserecord) : ptree;
  90. var
  91. t : tnode;
  92. begin
  93. t:=ccasenode.create(l,r,nodes);
  94. gencasenode:=t;
  95. end;
  96. {*****************************************************************************
  97. TSETELEMENTNODE
  98. *****************************************************************************}
  99. constructor tsetelementnode.create(l,r : tnode);
  100. begin
  101. inherited create(setelementn,l,r);
  102. end;
  103. function tsetelementnode.pass_1 : tnode;
  104. begin
  105. pass_1:=nil;
  106. firstpass(left);
  107. set_varstate(left,true);
  108. if codegenerror then
  109. exit;
  110. if assigned(right) then
  111. begin
  112. firstpass(right);
  113. if codegenerror then
  114. exit;
  115. end;
  116. calcregisters(p,0,0,0);
  117. resulttype:=left.resulttype;
  118. set_location(location,left.location);
  119. end;
  120. {*****************************************************************************
  121. TINNODE
  122. *****************************************************************************}
  123. constructor tinnode.create(l,r : tnode);
  124. begin
  125. inherited create(inn,l,r);
  126. end;
  127. function tinnode.pass_1 : tnode;
  128. type
  129. byteset = set of byte;
  130. var
  131. t : ptree;
  132. pst : pconstset;
  133. function createsetconst(psd : psetdef) : pconstset;
  134. var
  135. pcs : pconstset;
  136. pes : penumsym;
  137. i : longint;
  138. begin
  139. new(pcs);
  140. case psd^.elementtype.def^.deftype of
  141. enumdef :
  142. begin
  143. pes:=penumdef(psd^.elementtype.def)^.firstenum;
  144. while assigned(pes) do
  145. begin
  146. pcs^[pes^.value div 8]:=pcs^[pes^.value div 8] or (1 shl (pes^.value mod 8));
  147. pes:=pes^.nextenum;
  148. end;
  149. end;
  150. orddef :
  151. begin
  152. for i:=porddef(psd^.elementtype.def)^.low to porddef(psd^.elementtype.def)^.high do
  153. begin
  154. pcs^[i div 8]:=pcs^[i div 8] or (1 shl (i mod 8));
  155. end;
  156. end;
  157. end;
  158. createsetconst:=pcs;
  159. end;
  160. begin
  161. pass_1:=nil;
  162. location.loc:=LOC_FLAGS;
  163. resulttype:=booldef;
  164. firstpass(right);
  165. set_varstate(right,true);
  166. if codegenerror then
  167. exit;
  168. { Convert array constructor first to set }
  169. if is_array_constructor(right.resulttype) then
  170. begin
  171. arrayconstructor_to_set(right);
  172. firstpass(right);
  173. if codegenerror then
  174. exit;
  175. end;
  176. { if right is a typen then the def
  177. is in typenodetype PM }
  178. if right.treetype=typen then
  179. right.resulttype:=right.typenodetype;
  180. if right.resulttype^.deftype<>setdef then
  181. CGMessage(sym_e_set_expected);
  182. if codegenerror then
  183. exit;
  184. if (right.treetype=typen) then
  185. begin
  186. { we need to create a setconstn }
  187. pst:=createsetconst(psetdef(right.typenodetype));
  188. t:=gensetconstnode(pst,psetdef(right.typenodetype));
  189. dispose(pst);
  190. right.free;
  191. right:=t;
  192. end;
  193. firstpass(left);
  194. set_varstate(left,true);
  195. if codegenerror then
  196. exit;
  197. { empty set then return false }
  198. if not assigned(psetdef(right.resulttype)^.elementtype.def) then
  199. begin
  200. t:=genordinalconstnode(0,booldef);
  201. firstpass(t);
  202. pass_1:=t;
  203. exit;
  204. end;
  205. { type conversion/check }
  206. left:=gentypeconvnode(left,psetdef(right.resulttype)^.elementtype.def);
  207. firstpass(left);
  208. if codegenerror then
  209. exit;
  210. { constant evaulation }
  211. if (left.treetype=ordconstn) and (right.treetype=setconstn) then
  212. begin
  213. t:=genordinalconstnode(byte(left.value in byteset(right.value_set^)),booldef);
  214. firstpass(t);
  215. pass_1:=t;
  216. exit;
  217. end;
  218. left_right_max(p);
  219. { this is not allways true due to optimization }
  220. { but if we don't set this we get problems with optimizing self code }
  221. if psetdef(right.resulttype)^.settype<>smallset then
  222. procinfo^.flags:=procinfo^.flags or pi_do_call
  223. else
  224. begin
  225. { a smallset needs maybe an misc. register }
  226. if (left.treetype<>ordconstn) and
  227. not(right.location.loc in [LOC_CREGISTER,LOC_REGISTER]) and
  228. (right.registers32<1) then
  229. inc(registers32);
  230. end;
  231. end;
  232. {*****************************************************************************
  233. TRANGENODE
  234. *****************************************************************************}
  235. constructor trangenode.create(l,r : tnode);
  236. begin
  237. inherited create(rangen,l,r);
  238. end;
  239. function trangenode.pass_1 : tnode;
  240. var
  241. ct : tconverttype;
  242. begin
  243. pass_1:=nil;
  244. firstpass(left);
  245. set_varstate(left,true);
  246. firstpass(right);
  247. set_varstate(right,true);
  248. if codegenerror then
  249. exit;
  250. { both types must be compatible }
  251. if not(is_equal(left.resulttype,right.resulttype)) and
  252. (isconvertable(left.resulttype,right.resulttype,ct,ordconstn,false)=0) then
  253. CGMessage(type_e_mismatch);
  254. { Check if only when its a constant set }
  255. if (left.treetype=ordconstn) and (right.treetype=ordconstn) then
  256. begin
  257. { upper limit must be greater or equal than lower limit }
  258. { not if u32bit }
  259. if (left.value>right.value) and
  260. (( left.value<0) or (right.value>=0)) then
  261. CGMessage(cg_e_upper_lower_than_lower);
  262. end;
  263. left_right_max(p);
  264. resulttype:=left.resulttype;
  265. set_location(location,left.location);
  266. end;
  267. {*****************************************************************************
  268. Case Helpers
  269. *****************************************************************************}
  270. function case_count_labels(root : pcaserecord) : longint;
  271. var
  272. _l : longint;
  273. procedure count(p : pcaserecord);
  274. begin
  275. inc(_l);
  276. if assigned(less) then
  277. count(less);
  278. if assigned(greater) then
  279. count(greater);
  280. end;
  281. begin
  282. _l:=0;
  283. count(root);
  284. case_count_labels:=_l;
  285. end;
  286. function case_get_max(root : pcaserecord) : longint;
  287. var
  288. hp : pcaserecord;
  289. begin
  290. hp:=root;
  291. while assigned(hp.greater) do
  292. hp:=hp.greater;
  293. case_get_max:=hp._high;
  294. end;
  295. function case_get_min(root : pcaserecord) : longint;
  296. var
  297. hp : pcaserecord;
  298. begin
  299. hp:=root;
  300. while assigned(hp.less) do
  301. hp:=hp.less;
  302. case_get_min:=hp._low;
  303. end;
  304. procedure deletecaselabels(p : pcaserecord);
  305. begin
  306. if assigned(greater) then
  307. deletecaselabels(greater);
  308. if assigned(less) then
  309. deletecaselabels(less);
  310. dispose(p);
  311. end;
  312. function copycaserecord(p : pcaserecord) : pcaserecord;
  313. var
  314. n : pcaserecord;
  315. begin
  316. new(n);
  317. n^:=p^;
  318. if assigned(p^.greater) then
  319. n^.greater:=copycaserecord(p^.greater);
  320. if assigned(p^.less) then
  321. n^.less:=copycaserecord(p^.less);
  322. copycaserecord:=n;
  323. end;
  324. {*****************************************************************************
  325. TCASENODE
  326. *****************************************************************************}
  327. constructor tcasenode.create(l,r : tnode;n : pnodes);
  328. begin
  329. inherited create(casen,l,r);
  330. nodes:=n;
  331. elseblock:=nil;
  332. set_file_pos(l);
  333. end;
  334. destructor tcasenode.destroy;
  335. begin
  336. elseblock.free;
  337. deletecaselables(nodes);
  338. inherited destroy;
  339. end;
  340. function tcasenode.pass_1 : tnode;
  341. var
  342. old_t_times : longint;
  343. hp : tnode;
  344. begin
  345. pass_1:=nil;
  346. { evalutes the case expression }
  347. {$ifdef newcg}
  348. tg.cleartempgen;
  349. {$else newcg}
  350. cleartempgen;
  351. {$endif newcg}
  352. firstpass(left);
  353. set_varstate(left,true);
  354. if codegenerror then
  355. exit;
  356. registers32:=left.registers32;
  357. registersfpu:=left.registersfpu;
  358. {$ifdef SUPPORT_MMX}
  359. registersmmx:=left.registersmmx;
  360. {$endif SUPPORT_MMX}
  361. { walk through all instructions }
  362. { estimates the repeat of each instruction }
  363. old_t_times:=t_times;
  364. if not(cs_littlesize in aktglobalswitches) then
  365. begin
  366. t_times:=t_times div case_count_labels(nodes);
  367. if t_times<1 then
  368. t_times:=1;
  369. end;
  370. { first case }
  371. hp:=right;
  372. while assigned(hp) do
  373. begin
  374. {$ifdef newcg}
  375. tg.cleartempgen;
  376. {$else newcg}
  377. cleartempgen;
  378. {$endif newcg}
  379. firstpass(hp.right);
  380. { searchs max registers }
  381. if hp.right.registers32>registers32 then
  382. registers32:=hp.right.registers32;
  383. if hp.right.registersfpu>registersfpu then
  384. registersfpu:=hp.right.registersfpu;
  385. {$ifdef SUPPORT_MMX}
  386. if hp.right.registersmmx>registersmmx then
  387. registersmmx:=hp.right.registersmmx;
  388. {$endif SUPPORT_MMX}
  389. hp:=hp.left;
  390. end;
  391. { may be handle else tree }
  392. if assigned(elseblock) then
  393. begin
  394. {$ifdef newcg}
  395. tg.cleartempgen;
  396. {$else newcg}
  397. cleartempgen;
  398. {$endif newcg}
  399. firstpass(elseblock);
  400. if codegenerror then
  401. exit;
  402. if registers32<elseblock.registers32 then
  403. registers32:=elseblock.registers32;
  404. if registersfpu<elseblock.registersfpu then
  405. registersfpu:=elseblock.registersfpu;
  406. {$ifdef SUPPORT_MMX}
  407. if registersmmx<elseblock.registersmmx then
  408. registersmmx:=elseblock.registersmmx;
  409. {$endif SUPPORT_MMX}
  410. end;
  411. t_times:=old_t_times;
  412. { there is one register required for the case expression }
  413. { for 64 bit ints we cheat: the high dword is stored in EDI }
  414. { so we don't need an extra register }
  415. if registers32<1 then registers32:=1;
  416. end;
  417. function tcasenode.getcopy : tnode;
  418. var
  419. p : tcasenode;
  420. begin
  421. p:=tcasenode(inherited getcopy);
  422. p.elseblock:=elseblock.getcopy;
  423. p.nodes:=copycaserecord(nodes);
  424. getcopy:=p;
  425. end;
  426. begin
  427. csetelementnode:=tsetelementnode;
  428. cinnode:=tinnode;
  429. crangenode:=trangenode;
  430. ccasenode:=tcasenode;
  431. end.
  432. {
  433. $Log$
  434. Revision 1.2 2000-09-24 20:17:44 florian
  435. * more conversion work done
  436. Revision 1.1 2000/09/24 19:38:39 florian
  437. * initial implementation
  438. }