n386set.pas 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  4. Generate i386 assembler for in 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 n386set;
  19. {$i defines.inc}
  20. interface
  21. uses
  22. node,nset;
  23. type
  24. ti386setelementnode = class(tsetelementnode)
  25. procedure pass_2;override;
  26. end;
  27. ti386innode = class(tsetinnode)
  28. procedure pass_2;override;
  29. end;
  30. ti386casenode = class(tcasenode)
  31. procedure pass_2;override;
  32. end;
  33. implementation
  34. uses
  35. globtype,systems,cpuinfo,
  36. cobjects,verbose,globals,
  37. symconst,symtable,aasm,types,
  38. hcodegen,temp_gen,pass_2,
  39. cpubase,cpuasm,
  40. cgai386,tgeni386;
  41. const
  42. bytes2Sxx:array[1..8] of Topsize=(S_B,S_W,S_NO,S_L,S_NO,S_NO,S_NO,S_Q);
  43. {*****************************************************************************
  44. TI386SETELEMENTNODE
  45. *****************************************************************************}
  46. procedure ti386setelementnode.pass_2;
  47. begin
  48. { load first value in 32bit register }
  49. secondpass(left);
  50. if left.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  51. emit_to_reg32(left.location.register);
  52. { also a second value ? }
  53. if assigned(right) then
  54. begin
  55. secondpass(right);
  56. if right.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  57. emit_to_reg32(right.location.register);
  58. end;
  59. { we doesn't modify the left side, we check only the type }
  60. set_location(location,left.location);
  61. end;
  62. {*****************************************************************************
  63. TI386INNODE
  64. *****************************************************************************}
  65. procedure ti386innode.pass_2;
  66. type
  67. Tsetpart=record
  68. range : boolean; {Part is a range.}
  69. start,stop : byte; {Start/stop when range; Stop=element when an element.}
  70. end;
  71. var
  72. genjumps,
  73. use_small,
  74. pushed,
  75. ranges : boolean;
  76. hr,hr2,
  77. pleftreg : tregister;
  78. opsize : topsize;
  79. setparts : array[1..8] of Tsetpart;
  80. i,numparts : byte;
  81. {href,href2 : Treference;}
  82. l,l2 : pasmlabel;
  83. {$ifdef CORRECT_SET_IN_FPC}
  84. AM : tasmop;
  85. {$endif CORRECT_SET_IN_FPC}
  86. function analizeset(Aset:pconstset;is_small:boolean):boolean;
  87. type
  88. byteset=set of byte;
  89. var
  90. compares,maxcompares:word;
  91. i:byte;
  92. begin
  93. analizeset:=false;
  94. ranges:=false;
  95. numparts:=0;
  96. compares:=0;
  97. { Lots of comparisions take a lot of time, so do not allow
  98. too much comparisions. 8 comparisions are, however, still
  99. smalller than emitting the set }
  100. if cs_littlesize in aktglobalswitches then
  101. maxcompares:=8
  102. else
  103. maxcompares:=5;
  104. { when smallset is possible allow only 3 compares the smallset
  105. code is for littlesize also smaller when more compares are used }
  106. if is_small then
  107. maxcompares:=3;
  108. for i:=0 to 255 do
  109. if i in byteset(Aset^) then
  110. begin
  111. if (numparts=0) or (i<>setparts[numparts].stop+1) then
  112. begin
  113. {Set element is a separate element.}
  114. inc(compares);
  115. if compares>maxcompares then
  116. exit;
  117. inc(numparts);
  118. setparts[numparts].range:=false;
  119. setparts[numparts].stop:=i;
  120. end
  121. else
  122. {Set element is part of a range.}
  123. if not setparts[numparts].range then
  124. begin
  125. {Transform an element into a range.}
  126. setparts[numparts].range:=true;
  127. setparts[numparts].start:=setparts[numparts].stop;
  128. setparts[numparts].stop:=i;
  129. inc(compares);
  130. if compares>maxcompares then
  131. exit;
  132. end
  133. else
  134. begin
  135. {Extend a range.}
  136. setparts[numparts].stop:=i;
  137. {A range of two elements can better
  138. be checked as two separate ones.
  139. When extending a range, our range
  140. becomes larger than two elements.}
  141. ranges:=true;
  142. end;
  143. end;
  144. analizeset:=true;
  145. end;
  146. begin
  147. { We check first if we can generate jumps, this can be done
  148. because the resulttype is already set in firstpass }
  149. { check if we can use smallset operation using btl which is limited
  150. to 32 bits, the left side may also not contain higher values !! }
  151. use_small:=(psetdef(right.resulttype)^.settype=smallset) and
  152. ((left.resulttype^.deftype=orddef) and (porddef(left.resulttype)^.high<=32) or
  153. (left.resulttype^.deftype=enumdef) and (penumdef(left.resulttype)^.max<=32));
  154. { Can we generate jumps? Possible for all types of sets }
  155. genjumps:=(right.treetype=setconstn) and
  156. analizeset(right.value_set,use_small);
  157. { calculate both operators }
  158. { the complex one first }
  159. firstcomplex(p);
  160. secondpass(left);
  161. { Only process the right if we are not generating jumps }
  162. if not genjumps then
  163. begin
  164. pushed:=maybe_push(right.registers32,left,false);
  165. secondpass(right);
  166. if pushed then
  167. restore(left,false);
  168. end;
  169. if codegenerror then
  170. exit;
  171. { ofcourse not commutative }
  172. if swaped then
  173. swaptree(p);
  174. if genjumps then
  175. begin
  176. { It gives us advantage to check for the set elements
  177. separately instead of using the SET_IN_BYTE procedure.
  178. To do: Build in support for LOC_JUMP }
  179. { If register is used, use only lower 8 bits }
  180. if left.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  181. begin
  182. pleftreg:=left.location.register;
  183. if pleftreg in [R_AX..R_DX] then
  184. begin
  185. emit_const_reg(A_AND,S_W,255,pleftreg);
  186. opsize:=S_W;
  187. end
  188. else
  189. if pleftreg in [R_EAX..R_EDI] then
  190. begin
  191. emit_const_reg(A_AND,S_L,255,pleftreg);
  192. opsize:=S_L;
  193. end
  194. else
  195. opsize:=S_B;
  196. end;
  197. { Get a label to jump to the end }
  198. location.loc:=LOC_FLAGS;
  199. { It's better to use the zero flag when there are
  200. no ranges }
  201. if ranges then
  202. location.resflags:=F_C
  203. else
  204. location.resflags:=F_E;
  205. getlabel(l);
  206. for i:=1 to numparts do
  207. if setparts[i].range then
  208. begin
  209. { Check if left is in a range }
  210. { Get a label to jump over the check }
  211. getlabel(l2);
  212. if setparts[i].start=setparts[i].stop-1 then
  213. begin
  214. case left.location.loc of
  215. LOC_REGISTER,
  216. LOC_CREGISTER : emit_const_reg(A_CMP,opsize,
  217. setparts[i].start,pleftreg);
  218. else
  219. emit_const_ref(A_CMP,S_B,
  220. setparts[i].start,newreference(left.location.reference));
  221. end;
  222. { Result should be in carry flag when ranges are used }
  223. if ranges then
  224. emit_none(A_STC,S_NO);
  225. { If found, jump to end }
  226. emitjmp(C_E,l);
  227. case left.location.loc of
  228. LOC_REGISTER,
  229. LOC_CREGISTER : emit_const_reg(A_CMP,opsize,
  230. setparts[i].stop,pleftreg);
  231. else
  232. emit_const_ref(A_CMP,S_B,
  233. setparts[i].stop,newreference(left.location.reference));
  234. end;
  235. { Result should be in carry flag when ranges are used }
  236. if ranges then
  237. emit_none(A_STC,S_NO);
  238. { If found, jump to end }
  239. emitjmp(C_E,l);
  240. end
  241. else
  242. begin
  243. if setparts[i].start<>0 then
  244. begin
  245. { We only check for the lower bound if it is > 0, because
  246. set elements lower than 0 dont exist }
  247. case left.location.loc of
  248. LOC_REGISTER,
  249. LOC_CREGISTER :
  250. emit_const_reg(A_CMP,opsize,
  251. setparts[i].start,pleftreg);
  252. else
  253. emit_const_ref(A_CMP,S_B,
  254. setparts[i].start,newreference(left.location.reference));
  255. end;
  256. { If lower, jump to next check }
  257. emitjmp(C_B,l2);
  258. end;
  259. { We only check for the high bound if it is < 255, because
  260. set elements higher than 255 do nt exist, the its always true,
  261. so only a JMP is generated }
  262. if setparts[i].stop<>255 then
  263. begin
  264. case left.location.loc of
  265. LOC_REGISTER,
  266. LOC_CREGISTER : emit_const_reg(A_CMP,opsize,
  267. setparts[i].stop+1,pleftreg);
  268. else
  269. emit_const_ref(A_CMP,S_B,
  270. setparts[i].stop+1,newreference(left.location.reference));
  271. end;
  272. { If higher, element is in set }
  273. emitjmp(C_B,l);
  274. end
  275. else
  276. begin
  277. emit_none(A_STC,S_NO);
  278. emitjmp(C_None,l);
  279. end;
  280. end;
  281. { Emit the jump over label }
  282. emitlab(l2);
  283. end
  284. else
  285. begin
  286. { Emit code to check if left is an element }
  287. case left.location.loc of
  288. LOC_REGISTER,
  289. LOC_CREGISTER : emit_const_reg(A_CMP,opsize,
  290. setparts[i].stop,pleftreg);
  291. else
  292. emit_const_ref(A_CMP,S_B,
  293. setparts[i].stop,newreference(left.location.reference));
  294. end;
  295. { Result should be in carry flag when ranges are used }
  296. if ranges then
  297. emit_none(A_STC,S_NO);
  298. { If found, jump to end }
  299. emitjmp(C_E,l);
  300. end;
  301. if ranges then
  302. emit_none(A_CLC,S_NO);
  303. { To compensate for not doing a second pass }
  304. right.location.reference.symbol:=nil;
  305. { Now place the end label }
  306. emitlab(l);
  307. case left.location.loc of
  308. LOC_REGISTER,
  309. LOC_CREGISTER : ungetregister32(pleftreg);
  310. else
  311. del_reference(left.location.reference);
  312. end;
  313. end
  314. else
  315. begin
  316. { We will now generated code to check the set itself, no jmps,
  317. handle smallsets separate, because it allows faster checks }
  318. if use_small then
  319. begin
  320. if left.treetype=ordconstn then
  321. begin
  322. location.resflags:=F_NE;
  323. case right.location.loc of
  324. LOC_REGISTER,
  325. LOC_CREGISTER:
  326. begin
  327. emit_const_reg(A_TEST,S_L,
  328. 1 shl (left.value and 31),right.location.register);
  329. ungetregister32(right.location.register);
  330. end
  331. else
  332. begin
  333. emit_const_ref(A_TEST,S_L,1 shl (left.value and 31),
  334. newreference(right.location.reference));
  335. del_reference(right.location.reference);
  336. end;
  337. end;
  338. end
  339. else
  340. begin
  341. case left.location.loc of
  342. LOC_REGISTER,
  343. LOC_CREGISTER:
  344. begin
  345. hr:=left.location.register;
  346. emit_to_reg32(hr);
  347. end;
  348. else
  349. begin
  350. { the set element isn't never samller than a byte }
  351. { and because it's a small set we need only 5 bits }
  352. { but 8 bits are easier to load }
  353. {$ifndef noAllocEdi}
  354. getexplicitregister32(R_EDI);
  355. {$endif noAllocEdi}
  356. emit_ref_reg(A_MOVZX,S_BL,
  357. newreference(left.location.reference),R_EDI);
  358. hr:=R_EDI;
  359. del_reference(left.location.reference);
  360. end;
  361. end;
  362. case right.location.loc of
  363. LOC_REGISTER,
  364. LOC_CREGISTER :
  365. begin
  366. emit_reg_reg(A_BT,S_L,hr,
  367. right.location.register);
  368. ungetregister32(right.location.register);
  369. end
  370. else
  371. begin
  372. del_reference(right.location.reference);
  373. if right.location.reference.is_immediate then
  374. begin
  375. { We have to load the value into a register because
  376. btl does not accept values only refs or regs (PFV) }
  377. hr2:=getregister32;
  378. emit_const_reg(A_MOV,S_L,
  379. right.location.reference.offset,hr2);
  380. emit_reg_reg(A_BT,S_L,hr,hr2);
  381. ungetregister32(hr2);
  382. end
  383. else
  384. emit_reg_ref(A_BT,S_L,hr,
  385. newreference(right.location.reference));
  386. end;
  387. end;
  388. {$ifndef noAllocEdi}
  389. { simply to indicate EDI is deallocated here too (JM) }
  390. ungetregister32(hr);
  391. {$else noAllocEdi}
  392. ungetregister32(hr);
  393. {$endif noAllocEdi}
  394. location.loc:=LOC_FLAGS;
  395. location.resflags:=F_C;
  396. end;
  397. end
  398. else
  399. begin
  400. if right.location.reference.is_immediate then
  401. begin
  402. location.resflags:=F_C;
  403. getlabel(l);
  404. getlabel(l2);
  405. { Is this treated in firstpass ?? }
  406. if left.treetype=ordconstn then
  407. begin
  408. hr:=getregister32;
  409. left.location.loc:=LOC_REGISTER;
  410. left.location.register:=hr;
  411. emit_const_reg(A_MOV,S_L,
  412. left.value,hr);
  413. end;
  414. case left.location.loc of
  415. LOC_REGISTER,
  416. LOC_CREGISTER:
  417. begin
  418. hr:=left.location.register;
  419. emit_to_reg32(hr);
  420. emit_const_reg(A_CMP,S_L,31,hr);
  421. emitjmp(C_NA,l);
  422. { reset carry flag }
  423. emit_none(A_CLC,S_NO);
  424. emitjmp(C_NONE,l2);
  425. emitlab(l);
  426. { We have to load the value into a register because
  427. btl does not accept values only refs or regs (PFV) }
  428. hr2:=getregister32;
  429. emit_const_reg(A_MOV,S_L,right.location.reference.offset,hr2);
  430. emit_reg_reg(A_BT,S_L,hr,hr2);
  431. ungetregister32(hr2);
  432. end;
  433. else
  434. begin
  435. {$ifdef CORRECT_SET_IN_FPC}
  436. if m_tp in aktmodeswitches then
  437. begin
  438. {***WARNING only correct if
  439. reference is 32 bits (PM) *****}
  440. emit_const_ref(A_CMP,S_L,
  441. 31,newreference(left.location.reference));
  442. end
  443. else
  444. {$endif CORRECT_SET_IN_FPC}
  445. begin
  446. emit_const_ref(A_CMP,S_B,
  447. 31,newreference(left.location.reference));
  448. end;
  449. emitjmp(C_NA,l);
  450. { reset carry flag }
  451. emit_none(A_CLC,S_NO);
  452. emitjmp(C_NONE,l2);
  453. emitlab(l);
  454. del_reference(left.location.reference);
  455. hr:=getregister32;
  456. emit_ref_reg(A_MOV,S_L,
  457. newreference(left.location.reference),hr);
  458. { We have to load the value into a register because
  459. btl does not accept values only refs or regs (PFV) }
  460. hr2:=getregister32;
  461. emit_const_reg(A_MOV,S_L,
  462. right.location.reference.offset,hr2);
  463. emit_reg_reg(A_BT,S_L,hr,hr2);
  464. ungetregister32(hr2);
  465. end;
  466. end;
  467. emitlab(l2);
  468. end { of right.location.reference.is_immediate }
  469. { do search in a normal set which could have >32 elementsm
  470. but also used if the left side contains higher values > 32 }
  471. else if left.treetype=ordconstn then
  472. begin
  473. location.resflags:=F_NE;
  474. inc(right.location.reference.offset,left.value shr 3);
  475. emit_const_ref(A_TEST,S_B,1 shl (left.value and 7),
  476. newreference(right.location.reference));
  477. del_reference(right.location.reference);
  478. end
  479. else
  480. begin
  481. pushsetelement(left);
  482. emitpushreferenceaddr(right.location.reference);
  483. del_reference(right.location.reference);
  484. { registers need not be save. that happens in SET_IN_BYTE }
  485. { (EDI is changed) }
  486. emitcall('FPC_SET_IN_BYTE');
  487. { ungetiftemp(right.location.reference); }
  488. location.loc:=LOC_FLAGS;
  489. location.resflags:=F_C;
  490. end;
  491. end;
  492. end;
  493. if (right.location.loc in [LOC_MEM,LOC_REFERENCE]) then
  494. ungetiftemp(right.location.reference);
  495. end;
  496. {*****************************************************************************
  497. TI386CASENODE
  498. *****************************************************************************}
  499. procedure ti386casenode.pass_2;
  500. var
  501. with_sign : boolean;
  502. opsize : topsize;
  503. jmp_gt,jmp_le,jmp_lee : tasmcond;
  504. hp : tnode;
  505. { register with case expression }
  506. hregister,hregister2 : tregister;
  507. endlabel,elselabel : pasmlabel;
  508. { true, if we can omit the range check of the jump table }
  509. jumptable_no_range : boolean;
  510. { where to put the jump table }
  511. jumpsegment : paasmoutput;
  512. min_label : TConstExprInt;
  513. procedure gentreejmp(p : pcaserecord);
  514. var
  515. lesslabel,greaterlabel : pasmlabel;
  516. begin
  517. emitlab(_at);
  518. { calculate labels for left and right }
  519. if (less=nil) then
  520. lesslabel:=elselabel
  521. else
  522. lesslabel:=less^._at;
  523. if (greater=nil) then
  524. greaterlabel:=elselabel
  525. else
  526. greaterlabel:=greater^._at;
  527. { calculate labels for left and right }
  528. { no range label: }
  529. if _low=_high then
  530. begin
  531. emit_const_reg(A_CMP,opsize,_low,hregister);
  532. if greaterlabel=lesslabel then
  533. emitjmp(C_NE,lesslabel)
  534. else
  535. begin
  536. emitjmp(jmp_le,lesslabel);
  537. emitjmp(jmp_gt,greaterlabel);
  538. end;
  539. emitjmp(C_None,statement);
  540. end
  541. else
  542. begin
  543. emit_const_reg(A_CMP,opsize,_low,hregister);
  544. emitjmp(jmp_le,lesslabel);
  545. emit_const_reg(A_CMP,opsize,_high,hregister);
  546. emitjmp(jmp_gt,greaterlabel);
  547. emitjmp(C_None,statement);
  548. end;
  549. if assigned(less) then
  550. gentreejmp(less);
  551. if assigned(greater) then
  552. gentreejmp(greater);
  553. end;
  554. procedure genlinearcmplist(hp : pcaserecord);
  555. var
  556. first : boolean;
  557. last : TConstExprInt;
  558. procedure genitem(t : pcaserecord);
  559. var
  560. l1 : pasmlabel;
  561. begin
  562. if assigned(t^.less) then
  563. genitem(t^.less);
  564. if t^._low=t^._high then
  565. begin
  566. if opsize=S_Q then
  567. begin
  568. getlabel(l1);
  569. emit_const_reg(A_CMP,S_L,hi(int64(t^._low)),hregister2);
  570. emitjmp(C_NZ,l1);
  571. emit_const_reg(A_CMP,S_L,lo(int64(t^._low)),hregister);
  572. emitjmp(C_Z,t^.statement);
  573. emitlab(l1);
  574. end
  575. else
  576. begin
  577. emit_const_reg(A_CMP,opsize,t^._low,hregister);
  578. emitjmp(C_Z,t^.statement);
  579. last:=t^._low;
  580. end;
  581. end
  582. else
  583. begin
  584. { if there is no unused label between the last and the }
  585. { present label then the lower limit can be checked }
  586. { immediately. else check the range in between: }
  587. if first or (t^._low-last>1) then
  588. begin
  589. if opsize=S_Q then
  590. begin
  591. getlabel(l1);
  592. emit_const_reg(A_CMP,S_L,hi(int64(t^._low)),hregister2);
  593. emitjmp(jmp_le,elselabel);
  594. emitjmp(jmp_gt,l1);
  595. emit_const_reg(A_CMP,S_L,lo(int64(t^._low)),hregister);
  596. { the comparisation of the low dword must be always unsigned! }
  597. emitjmp(C_B,elselabel);
  598. emitlab(l1);
  599. end
  600. else
  601. begin
  602. emit_const_reg(A_CMP,opsize,t^._low,hregister);
  603. emitjmp(jmp_le,elselabel);
  604. end;
  605. end;
  606. if opsize=S_Q then
  607. begin
  608. getlabel(l1);
  609. emit_const_reg(A_CMP,S_L,hi(int64(t^._high)),hregister2);
  610. emitjmp(jmp_le,t^.statement);
  611. emitjmp(jmp_gt,l1);
  612. emit_const_reg(A_CMP,S_L,lo(int64(t^._high)),hregister);
  613. { the comparisation of the low dword must be always unsigned! }
  614. emitjmp(C_BE,t^.statement);
  615. emitlab(l1);
  616. end
  617. else
  618. begin
  619. emit_const_reg(A_CMP,opsize,t^._high,hregister);
  620. emitjmp(jmp_lee,t^.statement);
  621. end;
  622. last:=t^._high;
  623. end;
  624. first:=false;
  625. if assigned(t^.greater) then
  626. genitem(t^.greater);
  627. end;
  628. begin
  629. last:=0;
  630. first:=true;
  631. genitem(hp);
  632. emitjmp(C_None,elselabel);
  633. end;
  634. procedure genlinearlist(hp : pcaserecord);
  635. var
  636. first : boolean;
  637. last : TConstExprInt;
  638. {helplabel : longint;}
  639. procedure genitem(t : pcaserecord);
  640. procedure gensub(value:longint);
  641. begin
  642. if value=1 then
  643. emit_reg(A_DEC,opsize,hregister)
  644. else
  645. emit_const_reg(A_SUB,opsize,value,hregister);
  646. end;
  647. begin
  648. if assigned(t^.less) then
  649. genitem(t^.less);
  650. { need we to test the first value }
  651. if first and (t^._low>get_min_value(left.resulttype)) then
  652. begin
  653. emit_const_reg(A_CMP,opsize,t^._low,hregister);
  654. emitjmp(jmp_le,elselabel);
  655. end;
  656. if t^._low=t^._high then
  657. begin
  658. if t^._low-last=0 then
  659. emit_reg_reg(A_OR,opsize,hregister,hregister)
  660. else
  661. gensub(t^._low-last);
  662. last:=t^._low;
  663. emitjmp(C_Z,t^.statement);
  664. end
  665. else
  666. begin
  667. { it begins with the smallest label, if the value }
  668. { is even smaller then jump immediately to the }
  669. { ELSE-label }
  670. if first then
  671. begin
  672. { have we to ajust the first value ? }
  673. if t^._low>get_min_value(left.resulttype) then
  674. gensub(t^._low);
  675. end
  676. else
  677. begin
  678. { if there is no unused label between the last and the }
  679. { present label then the lower limit can be checked }
  680. { immediately. else check the range in between: }
  681. emit_const_reg(A_SUB,opsize,t^._low-last,hregister);
  682. emitjmp(jmp_le,elselabel);
  683. end;
  684. emit_const_reg(A_SUB,opsize,t^._high-t^._low,hregister);
  685. emitjmp(jmp_lee,t^.statement);
  686. last:=t^._high;
  687. end;
  688. first:=false;
  689. if assigned(t^.greater) then
  690. genitem(t^.greater);
  691. end;
  692. begin
  693. { do we need to generate cmps? }
  694. if (with_sign and (min_label<0)) then
  695. genlinearcmplist(hp)
  696. else
  697. begin
  698. last:=0;
  699. first:=true;
  700. genitem(hp);
  701. emitjmp(C_None,elselabel);
  702. end;
  703. end;
  704. procedure genjumptable(hp : pcaserecord;min_,max_ : longint);
  705. var
  706. table : pasmlabel;
  707. last : TConstExprInt;
  708. hr : preference;
  709. procedure genitem(t : pcaserecord);
  710. var
  711. i : longint;
  712. begin
  713. if assigned(t^.less) then
  714. genitem(t^.less);
  715. { fill possible hole }
  716. for i:=last+1 to t^._low-1 do
  717. jumpsegment^.concat(new(pai_const_symbol,init(elselabel)));
  718. for i:=t^._low to t^._high do
  719. jumpsegment^.concat(new(pai_const_symbol,init(t^.statement)));
  720. last:=t^._high;
  721. if assigned(t^.greater) then
  722. genitem(t^.greater);
  723. end;
  724. begin
  725. if not(jumptable_no_range) then
  726. begin
  727. emit_const_reg(A_CMP,opsize,min_,hregister);
  728. { case expr less than min_ => goto elselabel }
  729. emitjmp(jmp_le,elselabel);
  730. emit_const_reg(A_CMP,opsize,max_,hregister);
  731. emitjmp(jmp_gt,elselabel);
  732. end;
  733. getlabel(table);
  734. { extend with sign }
  735. if opsize=S_W then
  736. begin
  737. if with_sign then
  738. emit_reg_reg(A_MOVSX,S_WL,hregister,
  739. reg16toreg32(hregister))
  740. else
  741. emit_reg_reg(A_MOVZX,S_WL,hregister,
  742. reg16toreg32(hregister));
  743. hregister:=reg16toreg32(hregister);
  744. end
  745. else if opsize=S_B then
  746. begin
  747. if with_sign then
  748. emit_reg_reg(A_MOVSX,S_BL,hregister,
  749. reg8toreg32(hregister))
  750. else
  751. emit_reg_reg(A_MOVZX,S_BL,hregister,
  752. reg8toreg32(hregister));
  753. hregister:=reg8toreg32(hregister);
  754. end;
  755. new(hr);
  756. reset_reference(hr^);
  757. hr^.symbol:=table;
  758. hr^.offset:=(-min_)*4;
  759. hr^.index:=hregister;
  760. hr^.scalefactor:=4;
  761. emit_ref(A_JMP,S_NO,hr);
  762. { !!!!! generate tables
  763. if not(cs_littlesize in aktlocalswitches) then
  764. jumpsegment^.concat(new(paicpu,op_const(A_ALIGN,S_NO,4)));
  765. }
  766. jumpsegment^.concat(new(pai_label,init(table)));
  767. last:=min_;
  768. genitem(hp);
  769. { !!!!!!!
  770. if not(cs_littlesize in aktlocalswitches) then
  771. emit_const(A_ALIGN,S_NO,4);
  772. }
  773. end;
  774. var
  775. lv,hv,max_label,labels : longint;
  776. max_linear_list : longint;
  777. otl, ofl: pasmlabel;
  778. {$ifdef Delphi}
  779. dist : cardinal;
  780. {$else Delphi}
  781. dist : dword;
  782. {$endif Delphi}
  783. hr : preference;
  784. begin
  785. getlabel(endlabel);
  786. getlabel(elselabel);
  787. if (cs_create_smart in aktmoduleswitches) then
  788. jumpsegment:=procinfo^.aktlocaldata
  789. else
  790. jumpsegment:=datasegment;
  791. with_sign:=is_signed(left.resulttype);
  792. if with_sign then
  793. begin
  794. jmp_gt:=C_G;
  795. jmp_le:=C_L;
  796. jmp_lee:=C_LE;
  797. end
  798. else
  799. begin
  800. jmp_gt:=C_A;
  801. jmp_le:=C_B;
  802. jmp_lee:=C_BE;
  803. end;
  804. cleartempgen;
  805. { save current truelabel and falselabel (they are restored in }
  806. { locjump2reg) (JM) }
  807. if left.location.loc=LOC_JUMP then
  808. begin
  809. otl:=truelabel;
  810. getlabel(truelabel);
  811. ofl:=falselabel;
  812. getlabel(falselabel);
  813. end;
  814. secondpass(left);
  815. { determines the size of the operand }
  816. opsize:=bytes2Sxx[left.resulttype^.size];
  817. { copy the case expression to a register }
  818. case left.location.loc of
  819. LOC_REGISTER:
  820. begin
  821. if opsize=S_Q then
  822. begin
  823. hregister:=left.location.registerlow;
  824. hregister2:=left.location.registerhigh;
  825. end
  826. else
  827. hregister:=left.location.register;
  828. end;
  829. LOC_FLAGS :
  830. begin
  831. locflags2reg(left.location,opsize);
  832. hregister := left.location.register;
  833. end;
  834. LOC_JUMP:
  835. begin
  836. locjump2reg(left.location,opsize,otl,ofl);
  837. hregister := left.location.register;
  838. end;
  839. LOC_CREGISTER:
  840. begin
  841. hregister:=getregister32;
  842. case opsize of
  843. S_B:
  844. hregister:=reg32toreg8(hregister);
  845. S_W:
  846. hregister:=reg32toreg16(hregister);
  847. S_Q:
  848. hregister2:=R_EDI;
  849. end;
  850. if opsize=S_Q then
  851. begin
  852. emit_reg_reg(A_MOV,S_L,left.location.registerlow,hregister);
  853. hr:=newreference(left.location.reference);
  854. inc(hr^.offset,4);
  855. emit_reg_reg(A_MOV,S_L,left.location.registerhigh,hregister2);
  856. end
  857. else
  858. emit_reg_reg(A_MOV,opsize,
  859. left.location.register,hregister);
  860. end;
  861. LOC_MEM,LOC_REFERENCE:
  862. begin
  863. del_reference(left.location.reference);
  864. hregister:=getregister32;
  865. case opsize of
  866. S_B:
  867. hregister:=reg32toreg8(hregister);
  868. S_W:
  869. hregister:=reg32toreg16(hregister);
  870. S_Q:
  871. hregister2:=R_EDI;
  872. end;
  873. if opsize=S_Q then
  874. begin
  875. emit_ref_reg(A_MOV,S_L,newreference(
  876. left.location.reference),hregister);
  877. hr:=newreference(left.location.reference);
  878. inc(hr^.offset,4);
  879. emit_ref_reg(A_MOV,S_L,hr,hregister2);
  880. end
  881. else
  882. emit_ref_reg(A_MOV,opsize,newreference(
  883. left.location.reference),hregister);
  884. end;
  885. else internalerror(2002);
  886. end;
  887. { we need the min_label always to choose between }
  888. { cmps and subs/decs }
  889. min_label:=case_get_min(nodes);
  890. { now generate the jumps }
  891. if opsize=S_Q then
  892. genlinearcmplist(nodes)
  893. else
  894. begin
  895. if cs_optimize in aktglobalswitches then
  896. begin
  897. { procedures are empirically passed on }
  898. { consumption can also be calculated }
  899. { but does it pay on the different }
  900. { processors? }
  901. { moreover can the size only be appro- }
  902. { ximated as it is not known if rel8, }
  903. { rel16 or rel32 jumps are used }
  904. max_label:=case_get_max(nodes);
  905. labels:=case_count_labels(nodes);
  906. { can we omit the range check of the jump table ? }
  907. getrange(left.resulttype,lv,hv);
  908. jumptable_no_range:=(lv=min_label) and (hv=max_label);
  909. { hack a little bit, because the range can be greater }
  910. { than the positive range of a longint }
  911. if (min_label<0) and (max_label>0) then
  912. begin
  913. {$ifdef Delphi}
  914. if min_label=longint($80000000) then
  915. dist:=Cardinal(max_label)+Cardinal($80000000)
  916. else
  917. dist:=Cardinal(max_label)+Cardinal(-min_label)
  918. {$else Delphi}
  919. if min_label=$80000000 then
  920. dist:=dword(max_label)+dword($80000000)
  921. else
  922. dist:=dword(max_label)+dword(-min_label)
  923. {$endif Delphi}
  924. end
  925. else
  926. dist:=max_label-min_label;
  927. { optimize for size ? }
  928. if cs_littlesize in aktglobalswitches then
  929. begin
  930. if (labels<=2) or
  931. ((max_label-min_label)<0) or
  932. ((max_label-min_label)>3*labels) then
  933. { a linear list is always smaller than a jump tree }
  934. genlinearlist(nodes)
  935. else
  936. { if the labels less or more a continuum then }
  937. genjumptable(nodes,min_label,max_label);
  938. end
  939. else
  940. begin
  941. if jumptable_no_range then
  942. max_linear_list:=4
  943. else
  944. max_linear_list:=2;
  945. { a jump table crashes the pipeline! }
  946. if aktoptprocessor=Class386 then
  947. inc(max_linear_list,3);
  948. if aktoptprocessor=ClassP5 then
  949. inc(max_linear_list,6);
  950. if aktoptprocessor>=ClassP6 then
  951. inc(max_linear_list,9);
  952. if (labels<=max_linear_list) then
  953. genlinearlist(nodes)
  954. else
  955. begin
  956. if (dist>4*labels) then
  957. begin
  958. if labels>16 then
  959. gentreejmp(nodes)
  960. else
  961. genlinearlist(nodes);
  962. end
  963. else
  964. genjumptable(nodes,min_label,max_label);
  965. end;
  966. end;
  967. end
  968. else
  969. { it's always not bad }
  970. genlinearlist(nodes);
  971. end;
  972. ungetregister(hregister);
  973. { now generate the instructions }
  974. hp:=right;
  975. while assigned(hp) do
  976. begin
  977. cleartempgen;
  978. secondpass(hp.right);
  979. { don't come back to case line }
  980. aktfilepos:=exprasmlist^.getlasttaifilepos^;
  981. emitjmp(C_None,endlabel);
  982. hp:=hp.left;
  983. end;
  984. emitlab(elselabel);
  985. { ...and the else block }
  986. if assigned(elseblock) then
  987. begin
  988. cleartempgen;
  989. secondpass(elseblock);
  990. end;
  991. emitlab(endlabel);
  992. end;
  993. begin
  994. csetelementnode:=ti386setelementnode;
  995. cinnode:=ti386innode;
  996. ccasenode:=ti386casenode;
  997. end.
  998. {
  999. $Log$
  1000. Revision 1.2 2000-09-24 20:17:44 florian
  1001. * more conversion work done
  1002. Revision 1.1 2000/09/24 19:38:39 florian
  1003. * initial implementation
  1004. }