cg386set.pas 40 KB

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