n386set.pas 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  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. getexplicitregister32(R_EDI);
  354. emit_ref_reg(A_MOVZX,S_BL,
  355. newreference(left.location.reference),R_EDI);
  356. hr:=R_EDI;
  357. del_reference(left.location.reference);
  358. end;
  359. end;
  360. case right.location.loc of
  361. LOC_REGISTER,
  362. LOC_CREGISTER :
  363. begin
  364. emit_reg_reg(A_BT,S_L,hr,
  365. right.location.register);
  366. ungetregister32(right.location.register);
  367. end
  368. else
  369. begin
  370. del_reference(right.location.reference);
  371. if right.location.reference.is_immediate then
  372. begin
  373. { We have to load the value into a register because
  374. btl does not accept values only refs or regs (PFV) }
  375. hr2:=getregister32;
  376. emit_const_reg(A_MOV,S_L,
  377. right.location.reference.offset,hr2);
  378. emit_reg_reg(A_BT,S_L,hr,hr2);
  379. ungetregister32(hr2);
  380. end
  381. else
  382. emit_reg_ref(A_BT,S_L,hr,
  383. newreference(right.location.reference));
  384. end;
  385. end;
  386. { simply to indicate EDI is deallocated here too (JM) }
  387. ungetregister32(hr);
  388. location.loc:=LOC_FLAGS;
  389. location.resflags:=F_C;
  390. end;
  391. end
  392. else
  393. begin
  394. if right.location.reference.is_immediate then
  395. begin
  396. location.resflags:=F_C;
  397. getlabel(l);
  398. getlabel(l2);
  399. { Is this treated in firstpass ?? }
  400. if left.treetype=ordconstn then
  401. begin
  402. hr:=getregister32;
  403. left.location.loc:=LOC_REGISTER;
  404. left.location.register:=hr;
  405. emit_const_reg(A_MOV,S_L,
  406. left.value,hr);
  407. end;
  408. case left.location.loc of
  409. LOC_REGISTER,
  410. LOC_CREGISTER:
  411. begin
  412. hr:=left.location.register;
  413. emit_to_reg32(hr);
  414. emit_const_reg(A_CMP,S_L,31,hr);
  415. emitjmp(C_NA,l);
  416. { reset carry flag }
  417. emit_none(A_CLC,S_NO);
  418. emitjmp(C_NONE,l2);
  419. emitlab(l);
  420. { We have to load the value into a register because
  421. btl does not accept values only refs or regs (PFV) }
  422. hr2:=getregister32;
  423. emit_const_reg(A_MOV,S_L,right.location.reference.offset,hr2);
  424. emit_reg_reg(A_BT,S_L,hr,hr2);
  425. ungetregister32(hr2);
  426. end;
  427. else
  428. begin
  429. {$ifdef CORRECT_SET_IN_FPC}
  430. if m_tp in aktmodeswitches then
  431. begin
  432. {***WARNING only correct if
  433. reference is 32 bits (PM) *****}
  434. emit_const_ref(A_CMP,S_L,
  435. 31,newreference(left.location.reference));
  436. end
  437. else
  438. {$endif CORRECT_SET_IN_FPC}
  439. begin
  440. emit_const_ref(A_CMP,S_B,
  441. 31,newreference(left.location.reference));
  442. end;
  443. emitjmp(C_NA,l);
  444. { reset carry flag }
  445. emit_none(A_CLC,S_NO);
  446. emitjmp(C_NONE,l2);
  447. emitlab(l);
  448. del_reference(left.location.reference);
  449. hr:=getregister32;
  450. emit_ref_reg(A_MOV,S_L,
  451. newreference(left.location.reference),hr);
  452. { We have to load the value into a register because
  453. btl does not accept values only refs or regs (PFV) }
  454. hr2:=getregister32;
  455. emit_const_reg(A_MOV,S_L,
  456. right.location.reference.offset,hr2);
  457. emit_reg_reg(A_BT,S_L,hr,hr2);
  458. ungetregister32(hr2);
  459. end;
  460. end;
  461. emitlab(l2);
  462. end { of right.location.reference.is_immediate }
  463. { do search in a normal set which could have >32 elementsm
  464. but also used if the left side contains higher values > 32 }
  465. else if left.treetype=ordconstn then
  466. begin
  467. location.resflags:=F_NE;
  468. inc(right.location.reference.offset,left.value shr 3);
  469. emit_const_ref(A_TEST,S_B,1 shl (left.value and 7),
  470. newreference(right.location.reference));
  471. del_reference(right.location.reference);
  472. end
  473. else
  474. begin
  475. pushsetelement(left);
  476. emitpushreferenceaddr(right.location.reference);
  477. del_reference(right.location.reference);
  478. { registers need not be save. that happens in SET_IN_BYTE }
  479. { (EDI is changed) }
  480. emitcall('FPC_SET_IN_BYTE');
  481. { ungetiftemp(right.location.reference); }
  482. location.loc:=LOC_FLAGS;
  483. location.resflags:=F_C;
  484. end;
  485. end;
  486. end;
  487. if (right.location.loc in [LOC_MEM,LOC_REFERENCE]) then
  488. ungetiftemp(right.location.reference);
  489. end;
  490. {*****************************************************************************
  491. TI386CASENODE
  492. *****************************************************************************}
  493. procedure ti386casenode.pass_2;
  494. var
  495. with_sign : boolean;
  496. opsize : topsize;
  497. jmp_gt,jmp_le,jmp_lee : tasmcond;
  498. hp : tnode;
  499. { register with case expression }
  500. hregister,hregister2 : tregister;
  501. endlabel,elselabel : pasmlabel;
  502. { true, if we can omit the range check of the jump table }
  503. jumptable_no_range : boolean;
  504. { where to put the jump table }
  505. jumpsegment : paasmoutput;
  506. min_label : TConstExprInt;
  507. procedure gentreejmp(p : pcaserecord);
  508. var
  509. lesslabel,greaterlabel : pasmlabel;
  510. begin
  511. emitlab(_at);
  512. { calculate labels for left and right }
  513. if (less=nil) then
  514. lesslabel:=elselabel
  515. else
  516. lesslabel:=less^._at;
  517. if (greater=nil) then
  518. greaterlabel:=elselabel
  519. else
  520. greaterlabel:=greater^._at;
  521. { calculate labels for left and right }
  522. { no range label: }
  523. if _low=_high then
  524. begin
  525. emit_const_reg(A_CMP,opsize,_low,hregister);
  526. if greaterlabel=lesslabel then
  527. emitjmp(C_NE,lesslabel)
  528. else
  529. begin
  530. emitjmp(jmp_le,lesslabel);
  531. emitjmp(jmp_gt,greaterlabel);
  532. end;
  533. emitjmp(C_None,statement);
  534. end
  535. else
  536. begin
  537. emit_const_reg(A_CMP,opsize,_low,hregister);
  538. emitjmp(jmp_le,lesslabel);
  539. emit_const_reg(A_CMP,opsize,_high,hregister);
  540. emitjmp(jmp_gt,greaterlabel);
  541. emitjmp(C_None,statement);
  542. end;
  543. if assigned(less) then
  544. gentreejmp(less);
  545. if assigned(greater) then
  546. gentreejmp(greater);
  547. end;
  548. procedure genlinearcmplist(hp : pcaserecord);
  549. var
  550. first : boolean;
  551. last : TConstExprInt;
  552. procedure genitem(t : pcaserecord);
  553. var
  554. l1 : pasmlabel;
  555. begin
  556. if assigned(t^.less) then
  557. genitem(t^.less);
  558. if t^._low=t^._high then
  559. begin
  560. if opsize=S_Q then
  561. begin
  562. getlabel(l1);
  563. emit_const_reg(A_CMP,S_L,hi(int64(t^._low)),hregister2);
  564. emitjmp(C_NZ,l1);
  565. emit_const_reg(A_CMP,S_L,lo(int64(t^._low)),hregister);
  566. emitjmp(C_Z,t^.statement);
  567. emitlab(l1);
  568. end
  569. else
  570. begin
  571. emit_const_reg(A_CMP,opsize,t^._low,hregister);
  572. emitjmp(C_Z,t^.statement);
  573. last:=t^._low;
  574. end;
  575. end
  576. else
  577. begin
  578. { if there is no unused label between the last and the }
  579. { present label then the lower limit can be checked }
  580. { immediately. else check the range in between: }
  581. if first or (t^._low-last>1) then
  582. begin
  583. if opsize=S_Q then
  584. begin
  585. getlabel(l1);
  586. emit_const_reg(A_CMP,S_L,hi(int64(t^._low)),hregister2);
  587. emitjmp(jmp_le,elselabel);
  588. emitjmp(jmp_gt,l1);
  589. emit_const_reg(A_CMP,S_L,lo(int64(t^._low)),hregister);
  590. { the comparisation of the low dword must be always unsigned! }
  591. emitjmp(C_B,elselabel);
  592. emitlab(l1);
  593. end
  594. else
  595. begin
  596. emit_const_reg(A_CMP,opsize,t^._low,hregister);
  597. emitjmp(jmp_le,elselabel);
  598. end;
  599. end;
  600. if opsize=S_Q then
  601. begin
  602. getlabel(l1);
  603. emit_const_reg(A_CMP,S_L,hi(int64(t^._high)),hregister2);
  604. emitjmp(jmp_le,t^.statement);
  605. emitjmp(jmp_gt,l1);
  606. emit_const_reg(A_CMP,S_L,lo(int64(t^._high)),hregister);
  607. { the comparisation of the low dword must be always unsigned! }
  608. emitjmp(C_BE,t^.statement);
  609. emitlab(l1);
  610. end
  611. else
  612. begin
  613. emit_const_reg(A_CMP,opsize,t^._high,hregister);
  614. emitjmp(jmp_lee,t^.statement);
  615. end;
  616. last:=t^._high;
  617. end;
  618. first:=false;
  619. if assigned(t^.greater) then
  620. genitem(t^.greater);
  621. end;
  622. begin
  623. last:=0;
  624. first:=true;
  625. genitem(hp);
  626. emitjmp(C_None,elselabel);
  627. end;
  628. procedure genlinearlist(hp : pcaserecord);
  629. var
  630. first : boolean;
  631. last : TConstExprInt;
  632. {helplabel : longint;}
  633. procedure genitem(t : pcaserecord);
  634. procedure gensub(value:longint);
  635. begin
  636. if value=1 then
  637. emit_reg(A_DEC,opsize,hregister)
  638. else
  639. emit_const_reg(A_SUB,opsize,value,hregister);
  640. end;
  641. begin
  642. if assigned(t^.less) then
  643. genitem(t^.less);
  644. { need we to test the first value }
  645. if first and (t^._low>get_min_value(left.resulttype)) then
  646. begin
  647. emit_const_reg(A_CMP,opsize,t^._low,hregister);
  648. emitjmp(jmp_le,elselabel);
  649. end;
  650. if t^._low=t^._high then
  651. begin
  652. if t^._low-last=0 then
  653. emit_reg_reg(A_OR,opsize,hregister,hregister)
  654. else
  655. gensub(t^._low-last);
  656. last:=t^._low;
  657. emitjmp(C_Z,t^.statement);
  658. end
  659. else
  660. begin
  661. { it begins with the smallest label, if the value }
  662. { is even smaller then jump immediately to the }
  663. { ELSE-label }
  664. if first then
  665. begin
  666. { have we to ajust the first value ? }
  667. if t^._low>get_min_value(left.resulttype) then
  668. gensub(t^._low);
  669. end
  670. else
  671. begin
  672. { if there is no unused label between the last and the }
  673. { present label then the lower limit can be checked }
  674. { immediately. else check the range in between: }
  675. emit_const_reg(A_SUB,opsize,t^._low-last,hregister);
  676. emitjmp(jmp_le,elselabel);
  677. end;
  678. emit_const_reg(A_SUB,opsize,t^._high-t^._low,hregister);
  679. emitjmp(jmp_lee,t^.statement);
  680. last:=t^._high;
  681. end;
  682. first:=false;
  683. if assigned(t^.greater) then
  684. genitem(t^.greater);
  685. end;
  686. begin
  687. { do we need to generate cmps? }
  688. if (with_sign and (min_label<0)) then
  689. genlinearcmplist(hp)
  690. else
  691. begin
  692. last:=0;
  693. first:=true;
  694. genitem(hp);
  695. emitjmp(C_None,elselabel);
  696. end;
  697. end;
  698. procedure genjumptable(hp : pcaserecord;min_,max_ : longint);
  699. var
  700. table : pasmlabel;
  701. last : TConstExprInt;
  702. hr : preference;
  703. procedure genitem(t : pcaserecord);
  704. var
  705. i : longint;
  706. begin
  707. if assigned(t^.less) then
  708. genitem(t^.less);
  709. { fill possible hole }
  710. for i:=last+1 to t^._low-1 do
  711. jumpsegment^.concat(new(pai_const_symbol,init(elselabel)));
  712. for i:=t^._low to t^._high do
  713. jumpsegment^.concat(new(pai_const_symbol,init(t^.statement)));
  714. last:=t^._high;
  715. if assigned(t^.greater) then
  716. genitem(t^.greater);
  717. end;
  718. begin
  719. if not(jumptable_no_range) then
  720. begin
  721. emit_const_reg(A_CMP,opsize,min_,hregister);
  722. { case expr less than min_ => goto elselabel }
  723. emitjmp(jmp_le,elselabel);
  724. emit_const_reg(A_CMP,opsize,max_,hregister);
  725. emitjmp(jmp_gt,elselabel);
  726. end;
  727. getlabel(table);
  728. { extend with sign }
  729. if opsize=S_W then
  730. begin
  731. if with_sign then
  732. emit_reg_reg(A_MOVSX,S_WL,hregister,
  733. reg16toreg32(hregister))
  734. else
  735. emit_reg_reg(A_MOVZX,S_WL,hregister,
  736. reg16toreg32(hregister));
  737. hregister:=reg16toreg32(hregister);
  738. end
  739. else if opsize=S_B then
  740. begin
  741. if with_sign then
  742. emit_reg_reg(A_MOVSX,S_BL,hregister,
  743. reg8toreg32(hregister))
  744. else
  745. emit_reg_reg(A_MOVZX,S_BL,hregister,
  746. reg8toreg32(hregister));
  747. hregister:=reg8toreg32(hregister);
  748. end;
  749. new(hr);
  750. reset_reference(hr^);
  751. hr^.symbol:=table;
  752. hr^.offset:=(-min_)*4;
  753. hr^.index:=hregister;
  754. hr^.scalefactor:=4;
  755. emit_ref(A_JMP,S_NO,hr);
  756. { !!!!! generate tables
  757. if not(cs_littlesize in aktlocalswitches) then
  758. jumpsegment^.concat(new(paicpu,op_const(A_ALIGN,S_NO,4)));
  759. }
  760. jumpsegment^.concat(new(pai_label,init(table)));
  761. last:=min_;
  762. genitem(hp);
  763. { !!!!!!!
  764. if not(cs_littlesize in aktlocalswitches) then
  765. emit_const(A_ALIGN,S_NO,4);
  766. }
  767. end;
  768. var
  769. lv,hv,max_label,labels : longint;
  770. max_linear_list : longint;
  771. otl, ofl: pasmlabel;
  772. {$ifdef Delphi}
  773. dist : cardinal;
  774. {$else Delphi}
  775. dist : dword;
  776. {$endif Delphi}
  777. hr : preference;
  778. begin
  779. getlabel(endlabel);
  780. getlabel(elselabel);
  781. if (cs_create_smart in aktmoduleswitches) then
  782. jumpsegment:=procinfo^.aktlocaldata
  783. else
  784. jumpsegment:=datasegment;
  785. with_sign:=is_signed(left.resulttype);
  786. if with_sign then
  787. begin
  788. jmp_gt:=C_G;
  789. jmp_le:=C_L;
  790. jmp_lee:=C_LE;
  791. end
  792. else
  793. begin
  794. jmp_gt:=C_A;
  795. jmp_le:=C_B;
  796. jmp_lee:=C_BE;
  797. end;
  798. cleartempgen;
  799. { save current truelabel and falselabel (they are restored in }
  800. { locjump2reg) (JM) }
  801. if left.location.loc=LOC_JUMP then
  802. begin
  803. otl:=truelabel;
  804. getlabel(truelabel);
  805. ofl:=falselabel;
  806. getlabel(falselabel);
  807. end;
  808. secondpass(left);
  809. { determines the size of the operand }
  810. opsize:=bytes2Sxx[left.resulttype^.size];
  811. { copy the case expression to a register }
  812. case left.location.loc of
  813. LOC_REGISTER:
  814. begin
  815. if opsize=S_Q then
  816. begin
  817. hregister:=left.location.registerlow;
  818. hregister2:=left.location.registerhigh;
  819. end
  820. else
  821. hregister:=left.location.register;
  822. end;
  823. LOC_FLAGS :
  824. begin
  825. locflags2reg(left.location,opsize);
  826. hregister := left.location.register;
  827. end;
  828. LOC_JUMP:
  829. begin
  830. locjump2reg(left.location,opsize,otl,ofl);
  831. hregister := left.location.register;
  832. end;
  833. LOC_CREGISTER:
  834. begin
  835. hregister:=getregister32;
  836. case opsize of
  837. S_B:
  838. hregister:=reg32toreg8(hregister);
  839. S_W:
  840. hregister:=reg32toreg16(hregister);
  841. S_Q:
  842. hregister2:=R_EDI;
  843. end;
  844. if opsize=S_Q then
  845. begin
  846. emit_reg_reg(A_MOV,S_L,left.location.registerlow,hregister);
  847. hr:=newreference(left.location.reference);
  848. inc(hr^.offset,4);
  849. emit_reg_reg(A_MOV,S_L,left.location.registerhigh,hregister2);
  850. end
  851. else
  852. emit_reg_reg(A_MOV,opsize,
  853. left.location.register,hregister);
  854. end;
  855. LOC_MEM,LOC_REFERENCE:
  856. begin
  857. del_reference(left.location.reference);
  858. hregister:=getregister32;
  859. case opsize of
  860. S_B:
  861. hregister:=reg32toreg8(hregister);
  862. S_W:
  863. hregister:=reg32toreg16(hregister);
  864. S_Q:
  865. hregister2:=R_EDI;
  866. end;
  867. if opsize=S_Q then
  868. begin
  869. emit_ref_reg(A_MOV,S_L,newreference(
  870. left.location.reference),hregister);
  871. hr:=newreference(left.location.reference);
  872. inc(hr^.offset,4);
  873. emit_ref_reg(A_MOV,S_L,hr,hregister2);
  874. end
  875. else
  876. emit_ref_reg(A_MOV,opsize,newreference(
  877. left.location.reference),hregister);
  878. end;
  879. else internalerror(2002);
  880. end;
  881. { we need the min_label always to choose between }
  882. { cmps and subs/decs }
  883. min_label:=case_get_min(nodes);
  884. { now generate the jumps }
  885. if opsize=S_Q then
  886. genlinearcmplist(nodes)
  887. else
  888. begin
  889. if cs_optimize in aktglobalswitches then
  890. begin
  891. { procedures are empirically passed on }
  892. { consumption can also be calculated }
  893. { but does it pay on the different }
  894. { processors? }
  895. { moreover can the size only be appro- }
  896. { ximated as it is not known if rel8, }
  897. { rel16 or rel32 jumps are used }
  898. max_label:=case_get_max(nodes);
  899. labels:=case_count_labels(nodes);
  900. { can we omit the range check of the jump table ? }
  901. getrange(left.resulttype,lv,hv);
  902. jumptable_no_range:=(lv=min_label) and (hv=max_label);
  903. { hack a little bit, because the range can be greater }
  904. { than the positive range of a longint }
  905. if (min_label<0) and (max_label>0) then
  906. begin
  907. {$ifdef Delphi}
  908. if min_label=longint($80000000) then
  909. dist:=Cardinal(max_label)+Cardinal($80000000)
  910. else
  911. dist:=Cardinal(max_label)+Cardinal(-min_label)
  912. {$else Delphi}
  913. if min_label=$80000000 then
  914. dist:=dword(max_label)+dword($80000000)
  915. else
  916. dist:=dword(max_label)+dword(-min_label)
  917. {$endif Delphi}
  918. end
  919. else
  920. dist:=max_label-min_label;
  921. { optimize for size ? }
  922. if cs_littlesize in aktglobalswitches then
  923. begin
  924. if (labels<=2) or
  925. ((max_label-min_label)<0) or
  926. ((max_label-min_label)>3*labels) then
  927. { a linear list is always smaller than a jump tree }
  928. genlinearlist(nodes)
  929. else
  930. { if the labels less or more a continuum then }
  931. genjumptable(nodes,min_label,max_label);
  932. end
  933. else
  934. begin
  935. if jumptable_no_range then
  936. max_linear_list:=4
  937. else
  938. max_linear_list:=2;
  939. { a jump table crashes the pipeline! }
  940. if aktoptprocessor=Class386 then
  941. inc(max_linear_list,3);
  942. if aktoptprocessor=ClassP5 then
  943. inc(max_linear_list,6);
  944. if aktoptprocessor>=ClassP6 then
  945. inc(max_linear_list,9);
  946. if (labels<=max_linear_list) then
  947. genlinearlist(nodes)
  948. else
  949. begin
  950. if (dist>4*labels) then
  951. begin
  952. if labels>16 then
  953. gentreejmp(nodes)
  954. else
  955. genlinearlist(nodes);
  956. end
  957. else
  958. genjumptable(nodes,min_label,max_label);
  959. end;
  960. end;
  961. end
  962. else
  963. { it's always not bad }
  964. genlinearlist(nodes);
  965. end;
  966. ungetregister(hregister);
  967. { now generate the instructions }
  968. hp:=right;
  969. while assigned(hp) do
  970. begin
  971. cleartempgen;
  972. secondpass(hp.right);
  973. { don't come back to case line }
  974. aktfilepos:=exprasmlist^.getlasttaifilepos^;
  975. emitjmp(C_None,endlabel);
  976. hp:=hp.left;
  977. end;
  978. emitlab(elselabel);
  979. { ...and the else block }
  980. if assigned(elseblock) then
  981. begin
  982. cleartempgen;
  983. secondpass(elseblock);
  984. end;
  985. emitlab(endlabel);
  986. end;
  987. begin
  988. csetelementnode:=ti386setelementnode;
  989. cinnode:=ti386innode;
  990. ccasenode:=ti386casenode;
  991. end.
  992. {
  993. $Log$
  994. Revision 1.3 2000-09-30 16:08:45 peter
  995. * more cg11 updates
  996. Revision 1.2 2000/09/24 20:17:44 florian
  997. * more conversion work done
  998. Revision 1.1 2000/09/24 19:38:39 florian
  999. * initial implementation
  1000. }