n386set.pas 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  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. verbose,globals,
  37. symconst,symdef,aasm,types,
  38. hcodegen,temp_gen,pass_2,
  39. ncon,
  40. cpubase,
  41. cgai386,tgcpu,n386util,regvars;
  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 : ungetregister(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,longint(hi(int64(t^._low))),hregister2);
  565. emitjmp(C_NZ,l1);
  566. emit_const_reg(A_CMP,S_L,longint(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,longint(hi(int64(t^._low))),hregister2);
  588. emitjmp(jmp_le,elselabel);
  589. emitjmp(jmp_gt,l1);
  590. emit_const_reg(A_CMP,S_L,longint(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,longint(hi(int64(t^._high))),hregister2);
  605. emitjmp(jmp_le,t^.statement);
  606. emitjmp(jmp_gt,l1);
  607. emit_const_reg(A_CMP,S_L,longint(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 (with_sign and
  669. (t^._low>get_min_value(left.resulttype))) or
  670. (not with_sign and
  671. (cardinal(t^._low) > cardinal(get_min_value(left.resulttype)))) then
  672. gensub(t^._low);
  673. end
  674. else
  675. begin
  676. { if there is no unused label between the last and the }
  677. { present label then the lower limit can be checked }
  678. { immediately. else check the range in between: }
  679. { note: you can't use gensub() here because dec doesn't }
  680. { change the carry flag (needed for jmp_lxx) (JM) }
  681. emit_const_reg(A_SUB,opsize,t^._low-last,hregister);
  682. emitjmp(jmp_le,elselabel);
  683. end;
  684. emit_const_reg(A_SUB,opsize,t^._high-t^._low,hregister);
  685. emitjmp(jmp_lee,t^.statement);
  686. last:=t^._high;
  687. end;
  688. first:=false;
  689. if assigned(t^.greater) then
  690. genitem(t^.greater);
  691. end;
  692. begin
  693. { do we need to generate cmps? }
  694. if (with_sign and (min_label<0)) then
  695. genlinearcmplist(hp)
  696. else
  697. begin
  698. last:=0;
  699. first:=true;
  700. genitem(hp);
  701. emitjmp(C_None,elselabel);
  702. end;
  703. end;
  704. procedure genjumptable(hp : pcaserecord;min_,max_ : longint);
  705. var
  706. table : pasmlabel;
  707. last : TConstExprInt;
  708. hr : preference;
  709. procedure genitem(t : pcaserecord);
  710. var
  711. i : longint;
  712. begin
  713. if assigned(t^.less) then
  714. genitem(t^.less);
  715. { fill possible hole }
  716. for i:=last+1 to t^._low-1 do
  717. jumpsegment^.concat(new(pai_const_symbol,init(elselabel)));
  718. for i:=t^._low to t^._high do
  719. jumpsegment^.concat(new(pai_const_symbol,init(t^.statement)));
  720. last:=t^._high;
  721. if assigned(t^.greater) then
  722. genitem(t^.greater);
  723. end;
  724. begin
  725. if not(jumptable_no_range) then
  726. begin
  727. emit_const_reg(A_CMP,opsize,min_,hregister);
  728. { case expr less than min_ => goto elselabel }
  729. emitjmp(jmp_le,elselabel);
  730. emit_const_reg(A_CMP,opsize,max_,hregister);
  731. emitjmp(jmp_gt,elselabel);
  732. end;
  733. getlabel(table);
  734. { extend with sign }
  735. if opsize=S_W then
  736. begin
  737. if with_sign then
  738. emit_reg_reg(A_MOVSX,S_WL,hregister,
  739. reg16toreg32(hregister))
  740. else
  741. emit_reg_reg(A_MOVZX,S_WL,hregister,
  742. reg16toreg32(hregister));
  743. hregister:=reg16toreg32(hregister);
  744. end
  745. else if opsize=S_B then
  746. begin
  747. if with_sign then
  748. emit_reg_reg(A_MOVSX,S_BL,hregister,
  749. reg8toreg32(hregister))
  750. else
  751. emit_reg_reg(A_MOVZX,S_BL,hregister,
  752. reg8toreg32(hregister));
  753. hregister:=reg8toreg32(hregister);
  754. end;
  755. new(hr);
  756. reset_reference(hr^);
  757. hr^.symbol:=table;
  758. hr^.offset:=(-min_)*4;
  759. hr^.index:=hregister;
  760. hr^.scalefactor:=4;
  761. emit_ref(A_JMP,S_NO,hr);
  762. { !!!!! generate tables
  763. if not(cs_littlesize in aktlocalswitches) then
  764. jumpsegment^.concat(new(paicpu,op_const(A_ALIGN,S_NO,4)));
  765. }
  766. jumpsegment^.concat(new(pai_label,init(table)));
  767. last:=min_;
  768. genitem(hp);
  769. { !!!!!!!
  770. if not(cs_littlesize in aktlocalswitches) then
  771. emit_const(A_ALIGN,S_NO,4);
  772. }
  773. end;
  774. var
  775. lv,hv,max_label,labels : longint;
  776. max_linear_list : longint;
  777. otl, ofl: pasmlabel;
  778. {$ifdef Delphi}
  779. dist : cardinal;
  780. {$else Delphi}
  781. dist : dword;
  782. {$endif Delphi}
  783. hr : preference;
  784. begin
  785. getlabel(endlabel);
  786. getlabel(elselabel);
  787. if (cs_create_smart in aktmoduleswitches) then
  788. jumpsegment:=procinfo^.aktlocaldata
  789. else
  790. jumpsegment:=datasegment;
  791. with_sign:=is_signed(left.resulttype);
  792. if with_sign then
  793. begin
  794. jmp_gt:=C_G;
  795. jmp_le:=C_L;
  796. jmp_lee:=C_LE;
  797. end
  798. else
  799. begin
  800. jmp_gt:=C_A;
  801. jmp_le:=C_B;
  802. jmp_lee:=C_BE;
  803. end;
  804. cleartempgen;
  805. { save current truelabel and falselabel (they are restored in }
  806. { locjump2reg) (JM) }
  807. if left.location.loc=LOC_JUMP then
  808. begin
  809. otl:=truelabel;
  810. getlabel(truelabel);
  811. ofl:=falselabel;
  812. getlabel(falselabel);
  813. end;
  814. secondpass(left);
  815. { determines the size of the operand }
  816. opsize:=bytes2Sxx[left.resulttype^.size];
  817. { copy the case expression to a register }
  818. case left.location.loc of
  819. LOC_REGISTER:
  820. begin
  821. if opsize=S_Q then
  822. begin
  823. hregister:=left.location.registerlow;
  824. hregister2:=left.location.registerhigh;
  825. end
  826. else
  827. hregister:=left.location.register;
  828. end;
  829. LOC_FLAGS :
  830. begin
  831. locflags2reg(left.location,opsize);
  832. hregister := left.location.register;
  833. end;
  834. LOC_JUMP:
  835. begin
  836. locjump2reg(left.location,opsize,otl,ofl);
  837. hregister := left.location.register;
  838. end;
  839. LOC_CREGISTER:
  840. begin
  841. hregister:=getregister32;
  842. case opsize of
  843. S_B:
  844. hregister:=reg32toreg8(hregister);
  845. S_W:
  846. hregister:=reg32toreg16(hregister);
  847. S_Q:
  848. hregister2:=R_EDI;
  849. end;
  850. if opsize=S_Q then
  851. begin
  852. emit_reg_reg(A_MOV,S_L,left.location.registerlow,hregister);
  853. hr:=newreference(left.location.reference);
  854. inc(hr^.offset,4);
  855. emit_reg_reg(A_MOV,S_L,left.location.registerhigh,hregister2);
  856. end
  857. else
  858. emit_reg_reg(A_MOV,opsize,
  859. left.location.register,hregister);
  860. end;
  861. LOC_MEM,LOC_REFERENCE:
  862. begin
  863. del_reference(left.location.reference);
  864. hregister:=getregister32;
  865. case opsize of
  866. S_B:
  867. hregister:=reg32toreg8(hregister);
  868. S_W:
  869. hregister:=reg32toreg16(hregister);
  870. S_Q:
  871. hregister2:=R_EDI;
  872. end;
  873. if opsize=S_Q then
  874. begin
  875. emit_ref_reg(A_MOV,S_L,newreference(
  876. left.location.reference),hregister);
  877. hr:=newreference(left.location.reference);
  878. inc(hr^.offset,4);
  879. emit_ref_reg(A_MOV,S_L,hr,hregister2);
  880. end
  881. else
  882. emit_ref_reg(A_MOV,opsize,newreference(
  883. left.location.reference),hregister);
  884. end;
  885. else internalerror(2002);
  886. end;
  887. { we need the min_label always to choose between }
  888. { cmps and subs/decs }
  889. min_label:=case_get_min(nodes);
  890. load_all_regvars(exprasmlist);
  891. { now generate the jumps }
  892. if opsize=S_Q then
  893. genlinearcmplist(nodes)
  894. else
  895. begin
  896. if cs_optimize in aktglobalswitches then
  897. begin
  898. { procedures are empirically passed on }
  899. { consumption can also be calculated }
  900. { but does it pay on the different }
  901. { processors? }
  902. { moreover can the size only be appro- }
  903. { ximated as it is not known if rel8, }
  904. { rel16 or rel32 jumps are used }
  905. max_label:=case_get_max(nodes);
  906. labels:=case_count_labels(nodes);
  907. { can we omit the range check of the jump table ? }
  908. getrange(left.resulttype,lv,hv);
  909. jumptable_no_range:=(lv=min_label) and (hv=max_label);
  910. { hack a little bit, because the range can be greater }
  911. { than the positive range of a longint }
  912. if (min_label<0) and (max_label>0) then
  913. begin
  914. {$ifdef Delphi}
  915. if min_label=longint($80000000) then
  916. dist:=Cardinal(max_label)+Cardinal($80000000)
  917. else
  918. dist:=Cardinal(max_label)+Cardinal(-min_label)
  919. {$else Delphi}
  920. if min_label=$80000000 then
  921. dist:=dword(max_label)+dword($80000000)
  922. else
  923. dist:=dword(max_label)+dword(-min_label)
  924. {$endif Delphi}
  925. end
  926. else
  927. dist:=max_label-min_label;
  928. { optimize for size ? }
  929. if cs_littlesize in aktglobalswitches then
  930. begin
  931. if (labels<=2) or
  932. ((max_label-min_label)<0) or
  933. ((max_label-min_label)>3*labels) then
  934. { a linear list is always smaller than a jump tree }
  935. genlinearlist(nodes)
  936. else
  937. { if the labels less or more a continuum then }
  938. genjumptable(nodes,min_label,max_label);
  939. end
  940. else
  941. begin
  942. if jumptable_no_range then
  943. max_linear_list:=4
  944. else
  945. max_linear_list:=2;
  946. { a jump table crashes the pipeline! }
  947. if aktoptprocessor=Class386 then
  948. inc(max_linear_list,3);
  949. if aktoptprocessor=ClassP5 then
  950. inc(max_linear_list,6);
  951. if aktoptprocessor>=ClassP6 then
  952. inc(max_linear_list,9);
  953. if (labels<=max_linear_list) then
  954. genlinearlist(nodes)
  955. else
  956. begin
  957. if (dist>4*cardinal(labels)) then
  958. begin
  959. if labels>16 then
  960. gentreejmp(nodes)
  961. else
  962. genlinearlist(nodes);
  963. end
  964. else
  965. genjumptable(nodes,min_label,max_label);
  966. end;
  967. end;
  968. end
  969. else
  970. { it's always not bad }
  971. genlinearlist(nodes);
  972. end;
  973. ungetregister(hregister);
  974. { now generate the instructions }
  975. hp:=right;
  976. while assigned(hp) do
  977. begin
  978. cleartempgen;
  979. secondpass(tbinarynode(hp).right);
  980. { don't come back to case line }
  981. aktfilepos:=exprasmlist^.getlasttaifilepos^;
  982. load_all_regvars(exprasmlist);
  983. emitjmp(C_None,endlabel);
  984. hp:=tbinarynode(hp).left;
  985. end;
  986. emitlab(elselabel);
  987. { ...and the else block }
  988. if assigned(elseblock) then
  989. begin
  990. cleartempgen;
  991. secondpass(elseblock);
  992. load_all_regvars(exprasmlist);
  993. end;
  994. emitlab(endlabel);
  995. end;
  996. begin
  997. csetelementnode:=ti386setelementnode;
  998. cinnode:=ti386innode;
  999. ccasenode:=ti386casenode;
  1000. end.
  1001. {
  1002. $Log$
  1003. Revision 1.8 2000-12-16 15:58:18 jonas
  1004. * removed warnings about possible range check errors
  1005. Revision 1.7 2000/12/05 11:44:34 jonas
  1006. + new integer regvar handling, should be much more efficient
  1007. Revision 1.6 2000/11/29 00:30:49 florian
  1008. * unused units removed from uses clause
  1009. * some changes for widestrings
  1010. Revision 1.5 2000/11/17 14:09:00 jonas
  1011. * fixed webbug 1222 ("merged")
  1012. Revision 1.4 2000/11/13 14:44:36 jonas
  1013. * fixes so no more range errors with improved range checking code
  1014. Revision 1.3 2000/10/31 22:02:57 peter
  1015. * symtable splitted, no real code changes
  1016. Revision 1.2 2000/10/26 15:53:27 jonas
  1017. * fixed web bug1192 (changed an ungetregister32 to ungetregister)
  1018. ("merged" from fixes)
  1019. Revision 1.1 2000/10/15 09:33:32 peter
  1020. * moved n386*.pas to i386/ cpu_target dir
  1021. Revision 1.4 2000/10/14 10:14:49 peter
  1022. * moehrendorf oct 2000 rewrite
  1023. Revision 1.3 2000/09/30 16:08:45 peter
  1024. * more cg11 updates
  1025. Revision 1.2 2000/09/24 20:17:44 florian
  1026. * more conversion work done
  1027. Revision 1.1 2000/09/24 19:38:39 florian
  1028. * initial implementation
  1029. }