cg386set.pas 36 KB

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