nset.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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,cpuinfo,aasm;
  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 : tnode;
  53. constructor create(l,r : tnode;n : pcaserecord);virtual;
  54. destructor destroy;override;
  55. function getcopy : tnode;override;
  56. procedure insertintolist(l : tnodelist);override;
  57. function pass_1 : tnode;override;
  58. end;
  59. var
  60. csetelementnode : class of tsetelementnode;
  61. cinnode : class of tinnode;
  62. crangenode : class of trangenode;
  63. ccasenode : class of tcasenode;
  64. { counts the labels }
  65. function case_count_labels(root : pcaserecord) : longint;
  66. { searches the highest label }
  67. function case_get_max(root : pcaserecord) : longint;
  68. { searches the lowest label }
  69. function case_get_min(root : pcaserecord) : longint;
  70. function gencasenode(l,r : tnode;nodes : pcaserecord) : tnode;
  71. implementation
  72. uses
  73. globtype,systems,
  74. cobjects,verbose,globals,
  75. symconst,symdef,symsym,symtable,types,
  76. htypechk,pass_1,
  77. ncnv,ncon,cpubase,nld
  78. {$ifdef newcg}
  79. ,cgbase
  80. ,tgcpu
  81. {$else newcg}
  82. ,hcodegen
  83. {$ifdef i386}
  84. ,tgeni386
  85. {$endif}
  86. {$ifdef m68k}
  87. ,tgen68k
  88. {$endif}
  89. {$endif newcg}
  90. ;
  91. function gencasenode(l,r : tnode;nodes : pcaserecord) : tnode;
  92. var
  93. t : tnode;
  94. begin
  95. t:=ccasenode.create(l,r,nodes);
  96. gencasenode:=t;
  97. end;
  98. {*****************************************************************************
  99. TSETELEMENTNODE
  100. *****************************************************************************}
  101. constructor tsetelementnode.create(l,r : tnode);
  102. begin
  103. inherited create(setelementn,l,r);
  104. end;
  105. function tsetelementnode.pass_1 : tnode;
  106. begin
  107. pass_1:=nil;
  108. firstpass(left);
  109. set_varstate(left,true);
  110. if codegenerror then
  111. exit;
  112. if assigned(right) then
  113. begin
  114. firstpass(right);
  115. if codegenerror then
  116. exit;
  117. end;
  118. calcregisters(self,0,0,0);
  119. resulttype:=left.resulttype;
  120. set_location(location,left.location);
  121. end;
  122. {*****************************************************************************
  123. TINNODE
  124. *****************************************************************************}
  125. constructor tinnode.create(l,r : tnode);
  126. begin
  127. inherited create(inn,l,r);
  128. end;
  129. function tinnode.pass_1 : tnode;
  130. type
  131. byteset = set of byte;
  132. var
  133. t : tnode;
  134. pst : pconstset;
  135. function createsetconst(psd : psetdef) : pconstset;
  136. var
  137. pcs : pconstset;
  138. pes : penumsym;
  139. i : longint;
  140. begin
  141. new(pcs);
  142. case psd^.elementtype.def^.deftype of
  143. enumdef :
  144. begin
  145. pes:=penumsym(penumdef(psd^.elementtype.def)^.firstenum);
  146. while assigned(pes) do
  147. begin
  148. pcs^[pes^.value div 8]:=pcs^[pes^.value div 8] or (1 shl (pes^.value mod 8));
  149. pes:=pes^.nextenum;
  150. end;
  151. end;
  152. orddef :
  153. begin
  154. for i:=porddef(psd^.elementtype.def)^.low to porddef(psd^.elementtype.def)^.high do
  155. begin
  156. pcs^[i div 8]:=pcs^[i div 8] or (1 shl (i mod 8));
  157. end;
  158. end;
  159. end;
  160. createsetconst:=pcs;
  161. end;
  162. begin
  163. pass_1:=nil;
  164. location.loc:=LOC_FLAGS;
  165. resulttype:=booldef;
  166. firstpass(right);
  167. set_varstate(right,true);
  168. if codegenerror then
  169. exit;
  170. { Convert array constructor first to set }
  171. if is_array_constructor(right.resulttype) then
  172. begin
  173. arrayconstructor_to_set(tarrayconstructornode(right));
  174. firstpass(right);
  175. if codegenerror then
  176. exit;
  177. end;
  178. { if right is a typen then the def
  179. is in typenodetype PM }
  180. if right.nodetype=typen then
  181. right.resulttype:=ttypenode(right).typenodetype;
  182. if right.resulttype^.deftype<>setdef then
  183. CGMessage(sym_e_set_expected);
  184. if codegenerror then
  185. exit;
  186. if (right.nodetype=typen) then
  187. begin
  188. { we need to create a setconstn }
  189. pst:=createsetconst(psetdef(ttypenode(right).typenodetype));
  190. t:=gensetconstnode(pst,psetdef(ttypenode(right).typenodetype));
  191. dispose(pst);
  192. right.free;
  193. right:=t;
  194. end;
  195. firstpass(left);
  196. set_varstate(left,true);
  197. if codegenerror then
  198. exit;
  199. { empty set then return false }
  200. if not assigned(psetdef(right.resulttype)^.elementtype.def) then
  201. begin
  202. t:=genordinalconstnode(0,booldef);
  203. firstpass(t);
  204. pass_1:=t;
  205. exit;
  206. end;
  207. { type conversion/check }
  208. left:=gentypeconvnode(left,psetdef(right.resulttype)^.elementtype.def);
  209. firstpass(left);
  210. if codegenerror then
  211. exit;
  212. { constant evaulation }
  213. if (left.nodetype=ordconstn) and (right.nodetype=setconstn) then
  214. begin
  215. t:=genordinalconstnode(byte(tordconstnode(left).value in byteset(tsetconstnode(right).value_set^)),booldef);
  216. firstpass(t);
  217. pass_1:=t;
  218. exit;
  219. end;
  220. left_right_max;
  221. { this is not allways true due to optimization }
  222. { but if we don't set this we get problems with optimizing self code }
  223. if psetdef(right.resulttype)^.settype<>smallset then
  224. procinfo^.flags:=procinfo^.flags or pi_do_call
  225. else
  226. begin
  227. { a smallset needs maybe an misc. register }
  228. if (left.nodetype<>ordconstn) and
  229. not(right.location.loc in [LOC_CREGISTER,LOC_REGISTER]) and
  230. (right.registers32<1) then
  231. inc(registers32);
  232. end;
  233. end;
  234. {*****************************************************************************
  235. TRANGENODE
  236. *****************************************************************************}
  237. constructor trangenode.create(l,r : tnode);
  238. begin
  239. inherited create(rangen,l,r);
  240. end;
  241. function trangenode.pass_1 : tnode;
  242. var
  243. ct : tconverttype;
  244. begin
  245. pass_1:=nil;
  246. firstpass(left);
  247. set_varstate(left,true);
  248. firstpass(right);
  249. set_varstate(right,true);
  250. if codegenerror then
  251. exit;
  252. { both types must be compatible }
  253. if not(is_equal(left.resulttype,right.resulttype)) and
  254. (isconvertable(left.resulttype,right.resulttype,ct,nil,ordconstn,false)=0) then
  255. CGMessage(type_e_mismatch);
  256. { Check if only when its a constant set }
  257. if (left.nodetype=ordconstn) and (right.nodetype=ordconstn) then
  258. begin
  259. { upper limit must be greater or equal than lower limit }
  260. { not if u32bit }
  261. if (tordconstnode(left).value>tordconstnode(right).value) and
  262. ((tordconstnode(left).value<0) or (tordconstnode(right).value>=0)) then
  263. CGMessage(cg_e_upper_lower_than_lower);
  264. end;
  265. left_right_max;
  266. resulttype:=left.resulttype;
  267. set_location(location,left.location);
  268. end;
  269. {*****************************************************************************
  270. Case Helpers
  271. *****************************************************************************}
  272. function case_count_labels(root : pcaserecord) : longint;
  273. var
  274. _l : longint;
  275. procedure count(p : pcaserecord);
  276. begin
  277. inc(_l);
  278. if assigned(p^.less) then
  279. count(p^.less);
  280. if assigned(p^.greater) then
  281. count(p^.greater);
  282. end;
  283. begin
  284. _l:=0;
  285. count(root);
  286. case_count_labels:=_l;
  287. end;
  288. function case_get_max(root : pcaserecord) : longint;
  289. var
  290. hp : pcaserecord;
  291. begin
  292. hp:=root;
  293. while assigned(hp^.greater) do
  294. hp:=hp^.greater;
  295. case_get_max:=hp^._high;
  296. end;
  297. function case_get_min(root : pcaserecord) : longint;
  298. var
  299. hp : pcaserecord;
  300. begin
  301. hp:=root;
  302. while assigned(hp^.less) do
  303. hp:=hp^.less;
  304. case_get_min:=hp^._low;
  305. end;
  306. procedure deletecaselabels(p : pcaserecord);
  307. begin
  308. if assigned(p^.greater) then
  309. deletecaselabels(p^.greater);
  310. if assigned(p^.less) then
  311. deletecaselabels(p^.less);
  312. dispose(p);
  313. end;
  314. function copycaserecord(p : pcaserecord) : pcaserecord;
  315. var
  316. n : pcaserecord;
  317. begin
  318. new(n);
  319. n^:=p^;
  320. if assigned(p^.greater) then
  321. n^.greater:=copycaserecord(p^.greater);
  322. if assigned(p^.less) then
  323. n^.less:=copycaserecord(p^.less);
  324. copycaserecord:=n;
  325. end;
  326. {*****************************************************************************
  327. TCASENODE
  328. *****************************************************************************}
  329. constructor tcasenode.create(l,r : tnode;n : pcaserecord);
  330. begin
  331. inherited create(casen,l,r);
  332. nodes:=n;
  333. elseblock:=nil;
  334. set_file_line(l);
  335. end;
  336. destructor tcasenode.destroy;
  337. begin
  338. elseblock.free;
  339. deletecaselabels(nodes);
  340. inherited destroy;
  341. end;
  342. function tcasenode.pass_1 : tnode;
  343. var
  344. old_t_times : longint;
  345. hp : tbinarynode;
  346. begin
  347. pass_1:=nil;
  348. { evalutes the case expression }
  349. {$ifdef newcg}
  350. tg.cleartempgen;
  351. {$else newcg}
  352. cleartempgen;
  353. {$endif newcg}
  354. firstpass(left);
  355. set_varstate(left,true);
  356. if codegenerror then
  357. exit;
  358. registers32:=left.registers32;
  359. registersfpu:=left.registersfpu;
  360. {$ifdef SUPPORT_MMX}
  361. registersmmx:=left.registersmmx;
  362. {$endif SUPPORT_MMX}
  363. { walk through all instructions }
  364. { estimates the repeat of each instruction }
  365. old_t_times:=t_times;
  366. if not(cs_littlesize in aktglobalswitches) then
  367. begin
  368. t_times:=t_times div case_count_labels(nodes);
  369. if t_times<1 then
  370. t_times:=1;
  371. end;
  372. { first case }
  373. hp:=tbinarynode(right);
  374. while assigned(hp) do
  375. begin
  376. {$ifdef newcg}
  377. tg.cleartempgen;
  378. {$else newcg}
  379. cleartempgen;
  380. {$endif newcg}
  381. firstpass(hp.right);
  382. { searchs max registers }
  383. if hp.right.registers32>registers32 then
  384. registers32:=hp.right.registers32;
  385. if hp.right.registersfpu>registersfpu then
  386. registersfpu:=hp.right.registersfpu;
  387. {$ifdef SUPPORT_MMX}
  388. if hp.right.registersmmx>registersmmx then
  389. registersmmx:=hp.right.registersmmx;
  390. {$endif SUPPORT_MMX}
  391. hp:=tbinarynode(hp.left);
  392. end;
  393. { may be handle else tree }
  394. if assigned(elseblock) then
  395. begin
  396. {$ifdef newcg}
  397. tg.cleartempgen;
  398. {$else newcg}
  399. cleartempgen;
  400. {$endif newcg}
  401. firstpass(elseblock);
  402. if codegenerror then
  403. exit;
  404. if registers32<elseblock.registers32 then
  405. registers32:=elseblock.registers32;
  406. if registersfpu<elseblock.registersfpu then
  407. registersfpu:=elseblock.registersfpu;
  408. {$ifdef SUPPORT_MMX}
  409. if registersmmx<elseblock.registersmmx then
  410. registersmmx:=elseblock.registersmmx;
  411. {$endif SUPPORT_MMX}
  412. end;
  413. t_times:=old_t_times;
  414. { there is one register required for the case expression }
  415. { for 64 bit ints we cheat: the high dword is stored in EDI }
  416. { so we don't need an extra register }
  417. if registers32<1 then registers32:=1;
  418. end;
  419. function tcasenode.getcopy : tnode;
  420. var
  421. p : tcasenode;
  422. begin
  423. p:=tcasenode(inherited getcopy);
  424. if assigned(elseblock) then
  425. p.elseblock:=elseblock.getcopy
  426. else
  427. p.elseblock:=nil;
  428. p.nodes:=copycaserecord(nodes);
  429. getcopy:=p;
  430. end;
  431. procedure tcasenode.insertintolist(l : tnodelist);
  432. begin
  433. end;
  434. begin
  435. csetelementnode:=tsetelementnode;
  436. cinnode:=tinnode;
  437. crangenode:=trangenode;
  438. ccasenode:=tcasenode;
  439. end.
  440. {
  441. $Log$
  442. Revision 1.8 2000-11-04 14:25:20 florian
  443. + merged Attila's changes for interfaces, not tested yet
  444. Revision 1.7 2000/10/31 22:02:49 peter
  445. * symtable splitted, no real code changes
  446. Revision 1.6 2000/10/21 18:16:11 florian
  447. * a lot of changes:
  448. - basic dyn. array support
  449. - basic C++ support
  450. - some work for interfaces done
  451. ....
  452. Revision 1.5 2000/10/14 10:14:51 peter
  453. * moehrendorf oct 2000 rewrite
  454. Revision 1.4 2000/10/01 19:48:25 peter
  455. * lot of compile updates for cg11
  456. Revision 1.3 2000/09/27 18:14:31 florian
  457. * fixed a lot of syntax errors in the n*.pas stuff
  458. Revision 1.2 2000/09/24 20:17:44 florian
  459. * more conversion work done
  460. Revision 1.1 2000/09/24 19:38:39 florian
  461. * initial implementation
  462. }