n386set.pas 39 KB

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