n386set.pas 39 KB

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