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