n386set.pas 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181
  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. cgbase,temp_gen,pass_2,
  39. ncon,
  40. cpubase,
  41. cga,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. var
  49. pushed: boolean;
  50. begin
  51. { load first value in 32bit register }
  52. secondpass(left);
  53. if left.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  54. emit_to_reg32(left.location.register);
  55. { also a second value ? }
  56. if assigned(right) then
  57. begin
  58. pushed:=maybe_push(right.registers32,left,false);
  59. secondpass(right);
  60. if codegenerror then
  61. exit;
  62. if pushed then
  63. restore(left,false);
  64. if right.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  65. emit_to_reg32(right.location.register);
  66. end;
  67. { we doesn't modify the left side, we check only the type }
  68. set_location(location,left.location);
  69. end;
  70. {*****************************************************************************
  71. TI386INNODE
  72. *****************************************************************************}
  73. procedure ti386innode.pass_2;
  74. type
  75. Tsetpart=record
  76. range : boolean; {Part is a range.}
  77. start,stop : byte; {Start/stop when range; Stop=element when an element.}
  78. end;
  79. var
  80. genjumps,
  81. use_small,
  82. pushed,
  83. ranges : boolean;
  84. hr,hr2,
  85. pleftreg : tregister;
  86. opsize : topsize;
  87. setparts : array[1..8] of Tsetpart;
  88. i,numparts : byte;
  89. adjustment : longint;
  90. {href,href2 : Treference;}
  91. l,l2 : tasmlabel;
  92. {$ifdef CORRECT_SET_IN_FPC}
  93. AM : tasmop;
  94. {$endif CORRECT_SET_IN_FPC}
  95. function analizeset(Aset:pconstset;is_small:boolean):boolean;
  96. type
  97. byteset=set of byte;
  98. var
  99. compares,maxcompares:word;
  100. i:byte;
  101. begin
  102. analizeset:=false;
  103. ranges:=false;
  104. numparts:=0;
  105. compares:=0;
  106. { Lots of comparisions take a lot of time, so do not allow
  107. too much comparisions. 8 comparisions are, however, still
  108. smalller than emitting the set }
  109. if cs_littlesize in aktglobalswitches then
  110. maxcompares:=8
  111. else
  112. maxcompares:=5;
  113. { when smallset is possible allow only 3 compares the smallset
  114. code is for littlesize also smaller when more compares are used }
  115. if is_small then
  116. maxcompares:=3;
  117. for i:=0 to 255 do
  118. if i in byteset(Aset^) then
  119. begin
  120. if (numparts=0) or (i<>setparts[numparts].stop+1) then
  121. begin
  122. {Set element is a separate element.}
  123. inc(compares);
  124. if compares>maxcompares then
  125. exit;
  126. inc(numparts);
  127. setparts[numparts].range:=false;
  128. setparts[numparts].stop:=i;
  129. end
  130. else
  131. {Set element is part of a range.}
  132. if not setparts[numparts].range then
  133. begin
  134. {Transform an element into a range.}
  135. setparts[numparts].range:=true;
  136. setparts[numparts].start:=setparts[numparts].stop;
  137. setparts[numparts].stop:=i;
  138. ranges := true;
  139. { there's only one compare per range anymore. Only a }
  140. { sub is added, but that's much faster than a }
  141. { cmp/jcc combo so neglect its effect }
  142. { inc(compares);
  143. if compares>maxcompares then
  144. exit; }
  145. end
  146. else
  147. begin
  148. {Extend a range.}
  149. setparts[numparts].stop:=i;
  150. end;
  151. end;
  152. analizeset:=true;
  153. end;
  154. begin
  155. { We check first if we can generate jumps, this can be done
  156. because the resulttype.def is already set in firstpass }
  157. { check if we can use smallset operation using btl which is limited
  158. to 32 bits, the left side may also not contain higher values !! }
  159. use_small:=(tsetdef(right.resulttype.def).settype=smallset) and
  160. ((left.resulttype.def.deftype=orddef) and (torddef(left.resulttype.def).high<=32) or
  161. (left.resulttype.def.deftype=enumdef) and (tenumdef(left.resulttype.def).max<=32));
  162. { Can we generate jumps? Possible for all types of sets }
  163. genjumps:=(right.nodetype=setconstn) and
  164. analizeset(tsetconstnode(right).value_set,use_small);
  165. { calculate both operators }
  166. { the complex one first }
  167. firstcomplex(self);
  168. secondpass(left);
  169. { Only process the right if we are not generating jumps }
  170. if not genjumps then
  171. begin
  172. pushed:=maybe_push(right.registers32,left,false);
  173. secondpass(right);
  174. if pushed then
  175. restore(left,false);
  176. end;
  177. if codegenerror then
  178. exit;
  179. { ofcourse not commutative }
  180. if nf_swaped in flags then
  181. swapleftright;
  182. if genjumps then
  183. begin
  184. { It gives us advantage to check for the set elements
  185. separately instead of using the SET_IN_BYTE procedure.
  186. To do: Build in support for LOC_JUMP }
  187. opsize := def_opsize(left.resulttype.def);
  188. { If register is used, use only lower 8 bits }
  189. if left.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  190. begin
  191. pleftreg:=left.location.register;
  192. { for ranges we always need a 32bit register, because then we }
  193. { use the register as base in a reference (JM) }
  194. if ranges then
  195. begin
  196. pleftreg := makereg32(pleftreg);
  197. if opsize <> S_L then
  198. emit_const_reg(A_AND,S_L,255,pleftreg);
  199. opsize := S_L;
  200. end
  201. else
  202. { otherwise simply use the lower 8 bits (no "and" }
  203. { necessary this way) (JM) }
  204. begin
  205. pleftreg := makereg8(pleftreg);
  206. opsize := S_B;
  207. end;
  208. end
  209. else
  210. begin
  211. { load the value in a register }
  212. pleftreg := getexplicitregister32(R_EDI);
  213. opsize := S_L;
  214. emit_ref_reg(A_MOVZX,S_BL,newreference(left.location.reference),
  215. pleftreg);
  216. end;
  217. { Get a label to jump to the end }
  218. location.loc:=LOC_FLAGS;
  219. { It's better to use the zero flag when there are
  220. no ranges }
  221. if ranges then
  222. location.resflags:=F_C
  223. else
  224. location.resflags:=F_E;
  225. getlabel(l);
  226. { how much have we already substracted from the x in the }
  227. { "x in [y..z]" expression }
  228. adjustment := 0;
  229. for i:=1 to numparts do
  230. if setparts[i].range then
  231. { use fact that a <= x <= b <=> cardinal(x-a) <= cardinal(b-a) }
  232. begin
  233. { is the range different from all legal values? }
  234. if (setparts[i].stop-setparts[i].start <> 255) then
  235. begin
  236. { yes, is the lower bound <> 0? }
  237. if (setparts[i].start <> 0) then
  238. { we're going to substract from the left register, }
  239. { so in case of a LOC_CREGISTER first move the value }
  240. { to edi (not done before because now we can do the }
  241. { move and substract in one instruction with LEA) }
  242. if (pleftreg <> R_EDI) and
  243. (left.location.loc = LOC_CREGISTER) then
  244. begin
  245. ungetregister(pleftreg);
  246. getexplicitregister32(R_EDI);
  247. emit_ref_reg(A_LEA,S_L,
  248. new_reference(pleftreg,-setparts[i].start),R_EDI);
  249. { only now change pleftreg since previous value is }
  250. { still used in previous instruction }
  251. pleftreg := R_EDI;
  252. opsize := S_L;
  253. end
  254. else
  255. begin
  256. { otherwise, the value is already in a register }
  257. { that can be modified }
  258. if setparts[i].start-adjustment <> 1 then
  259. emit_const_reg(A_SUB,opsize,
  260. setparts[i].start-adjustment,pleftreg)
  261. else emit_reg(A_DEC,opsize,pleftreg);
  262. end;
  263. { new total value substracted from x: }
  264. { adjustment + (setparts[i].start - adjustment) }
  265. adjustment := setparts[i].start;
  266. { check if result < b-a+1 (not "result <= b-a", since }
  267. { we need a carry in case the element is in the range }
  268. { (this will never overflow since we check at the }
  269. { beginning whether stop-start <> 255) }
  270. emit_const_reg(A_CMP,opsize,
  271. setparts[i].stop-setparts[i].start+1,pleftreg);
  272. { use C_C instead of C_B: the meaning is the same, but }
  273. { then the optimizer can easier trace the jump to its }
  274. { final destination since the resultflag of this node }
  275. { is set to the carryflag }
  276. emitjmp(C_C,l);
  277. end
  278. else
  279. { if setparts[i].start = 0 and setparts[i].stop = 255, }
  280. { it's always true since "in" is only allowed for bytes }
  281. begin
  282. emit_none(A_STC,S_NO);
  283. emitjmp(C_NONE,l);
  284. end;
  285. end
  286. else
  287. begin
  288. { Emit code to check if left is an element }
  289. emit_const_reg(A_CMP,opsize,setparts[i].stop-adjustment,
  290. pleftreg);
  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 and
  298. { if the last one was a range, the carry flag is already }
  299. { set appropriately }
  300. not(setparts[numparts].range) then
  301. emit_none(A_CLC,S_NO);
  302. { To compensate for not doing a second pass }
  303. right.location.reference.symbol:=nil;
  304. { Now place the end label }
  305. emitlab(l);
  306. case left.location.loc of
  307. LOC_REGISTER,
  308. LOC_CREGISTER : ungetregister(pleftreg);
  309. else
  310. begin
  311. del_reference(left.location.reference);
  312. ungetregister(R_EDI);
  313. end
  314. end
  315. end
  316. else
  317. begin
  318. { We will now generated code to check the set itself, no jmps,
  319. handle smallsets separate, because it allows faster checks }
  320. if use_small then
  321. begin
  322. if left.nodetype=ordconstn then
  323. begin
  324. location.resflags:=F_NE;
  325. case right.location.loc of
  326. LOC_REGISTER,
  327. LOC_CREGISTER:
  328. begin
  329. emit_const_reg(A_TEST,S_L,
  330. 1 shl (tordconstnode(left).value and 31),right.location.register);
  331. ungetregister32(right.location.register);
  332. end
  333. else
  334. begin
  335. emit_const_ref(A_TEST,S_L,1 shl (tordconstnode(left).value and 31),
  336. newreference(right.location.reference));
  337. del_reference(right.location.reference);
  338. end;
  339. end;
  340. end
  341. else
  342. begin
  343. case left.location.loc of
  344. LOC_REGISTER,
  345. LOC_CREGISTER:
  346. begin
  347. hr:=left.location.register;
  348. emit_to_reg32(hr);
  349. end;
  350. else
  351. begin
  352. { the set element isn't never samller than a byte }
  353. { and because it's a small set we need only 5 bits }
  354. { but 8 bits are easier to load }
  355. getexplicitregister32(R_EDI);
  356. emit_ref_reg(A_MOVZX,S_BL,
  357. newreference(left.location.reference),R_EDI);
  358. hr:=R_EDI;
  359. del_reference(left.location.reference);
  360. end;
  361. end;
  362. case right.location.loc of
  363. LOC_REGISTER,
  364. LOC_CREGISTER :
  365. begin
  366. emit_reg_reg(A_BT,S_L,hr,
  367. right.location.register);
  368. ungetregister32(right.location.register);
  369. end
  370. else
  371. begin
  372. del_reference(right.location.reference);
  373. if right.location.reference.is_immediate then
  374. begin
  375. { We have to load the value into a register because
  376. btl does not accept values only refs or regs (PFV) }
  377. hr2:=getregister32;
  378. emit_const_reg(A_MOV,S_L,
  379. right.location.reference.offset,hr2);
  380. emit_reg_reg(A_BT,S_L,hr,hr2);
  381. ungetregister32(hr2);
  382. end
  383. else
  384. emit_reg_ref(A_BT,S_L,hr,
  385. newreference(right.location.reference));
  386. end;
  387. end;
  388. { simply to indicate EDI is deallocated here too (JM) }
  389. ungetregister32(hr);
  390. location.loc:=LOC_FLAGS;
  391. location.resflags:=F_C;
  392. end;
  393. end
  394. else
  395. begin
  396. if right.location.reference.is_immediate then
  397. begin
  398. location.resflags:=F_C;
  399. getlabel(l);
  400. getlabel(l2);
  401. { Is this treated in firstpass ?? }
  402. if left.nodetype=ordconstn then
  403. begin
  404. hr:=getregister32;
  405. left.location.loc:=LOC_REGISTER;
  406. left.location.register:=hr;
  407. emit_const_reg(A_MOV,S_L,
  408. tordconstnode(left).value,hr);
  409. end;
  410. case left.location.loc of
  411. LOC_REGISTER,
  412. LOC_CREGISTER:
  413. begin
  414. hr:=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,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(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(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(left.location.reference);
  451. hr:=getregister32;
  452. emit_ref_reg(A_MOV,S_L,
  453. newreference(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. 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 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 left.nodetype=ordconstn then
  468. begin
  469. location.resflags:=F_NE;
  470. inc(right.location.reference.offset,tordconstnode(left).value shr 3);
  471. emit_const_ref(A_TEST,S_B,1 shl (tordconstnode(left).value and 7),
  472. newreference(right.location.reference));
  473. del_reference(right.location.reference);
  474. end
  475. else
  476. begin
  477. if not(left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  478. begin
  479. pleftreg := getexplicitregister32(R_EDI);
  480. opsize := def2def_opsize(left.resulttype.def,u32bittype.def);
  481. if opsize = S_L then
  482. emit_ref_reg(A_MOV,opsize,newreference(left.location.reference),pleftreg)
  483. else
  484. emit_ref_reg(A_MOVZX,opsize,newreference(left.location.reference),pleftreg);
  485. ungetiftemp(left.location.reference);
  486. del_reference(left.location.reference);
  487. end
  488. else
  489. begin
  490. pleftreg := left.location.register;
  491. opsize := def2def_opsize(left.resulttype.def,u32bittype.def);
  492. if opsize <> S_L then
  493. { this will change left, even if it's a LOC_CREGISTER, but }
  494. { that doesn't matter: if left is an 8 bit def, then the }
  495. { upper 24 bits are undefined, so we can zero them without }
  496. { any problem (JM) }
  497. emit_to_reg32(pleftreg)
  498. end;
  499. emit_reg_ref(A_BT,S_L,pleftreg,newreference(right.location.reference));
  500. ungetregister(pleftreg);
  501. del_reference(right.location.reference);
  502. { ungetiftemp(right.location.reference) happens below }
  503. location.loc:=LOC_FLAGS;
  504. location.resflags:=F_C;
  505. end;
  506. end;
  507. end;
  508. if (right.location.loc in [LOC_MEM,LOC_REFERENCE]) then
  509. ungetiftemp(right.location.reference);
  510. end;
  511. {*****************************************************************************
  512. TI386CASENODE
  513. *****************************************************************************}
  514. procedure ti386casenode.pass_2;
  515. var
  516. with_sign : boolean;
  517. opsize : topsize;
  518. jmp_gt,jmp_le,jmp_lee : tasmcond;
  519. hp : tnode;
  520. { register with case expression }
  521. hregister,hregister2 : tregister;
  522. endlabel,elselabel : tasmlabel;
  523. { true, if we can omit the range check of the jump table }
  524. jumptable_no_range : boolean;
  525. { where to put the jump table }
  526. jumpsegment : TAAsmoutput;
  527. min_label : TConstExprInt;
  528. procedure gentreejmp(p : pcaserecord);
  529. var
  530. lesslabel,greaterlabel : tasmlabel;
  531. begin
  532. emitlab(p^._at);
  533. { calculate labels for left and right }
  534. if (p^.less=nil) then
  535. lesslabel:=elselabel
  536. else
  537. lesslabel:=p^.less^._at;
  538. if (p^.greater=nil) then
  539. greaterlabel:=elselabel
  540. else
  541. greaterlabel:=p^.greater^._at;
  542. { calculate labels for left and right }
  543. { no range label: }
  544. if p^._low=p^._high then
  545. begin
  546. emit_const_reg(A_CMP,opsize,p^._low,hregister);
  547. if greaterlabel=lesslabel then
  548. emitjmp(C_NE,lesslabel)
  549. else
  550. begin
  551. emitjmp(jmp_le,lesslabel);
  552. emitjmp(jmp_gt,greaterlabel);
  553. end;
  554. emitjmp(C_None,p^.statement);
  555. end
  556. else
  557. begin
  558. emit_const_reg(A_CMP,opsize,p^._low,hregister);
  559. emitjmp(jmp_le,lesslabel);
  560. emit_const_reg(A_CMP,opsize,p^._high,hregister);
  561. emitjmp(jmp_gt,greaterlabel);
  562. emitjmp(C_None,p^.statement);
  563. end;
  564. if assigned(p^.less) then
  565. gentreejmp(p^.less);
  566. if assigned(p^.greater) then
  567. gentreejmp(p^.greater);
  568. end;
  569. procedure genlinearcmplist(hp : pcaserecord);
  570. var
  571. first : boolean;
  572. last : TConstExprInt;
  573. procedure genitem(t : pcaserecord);
  574. var
  575. l1 : tasmlabel;
  576. begin
  577. if assigned(t^.less) then
  578. genitem(t^.less);
  579. if t^._low=t^._high then
  580. begin
  581. if opsize=S_Q then
  582. begin
  583. getlabel(l1);
  584. emit_const_reg(A_CMP,S_L,longint(hi(int64(t^._low))),hregister2);
  585. emitjmp(C_NZ,l1);
  586. emit_const_reg(A_CMP,S_L,longint(lo(int64(t^._low))),hregister);
  587. emitjmp(C_Z,t^.statement);
  588. emitlab(l1);
  589. end
  590. else
  591. begin
  592. emit_const_reg(A_CMP,opsize,longint(t^._low),hregister);
  593. emitjmp(C_Z,t^.statement);
  594. last:=t^._low;
  595. end;
  596. end
  597. else
  598. begin
  599. { if there is no unused label between the last and the }
  600. { present label then the lower limit can be checked }
  601. { immediately. else check the range in between: }
  602. if first or (t^._low-last>1) then
  603. begin
  604. if opsize=S_Q then
  605. begin
  606. getlabel(l1);
  607. emit_const_reg(A_CMP,S_L,longint(hi(int64(t^._low))),hregister2);
  608. emitjmp(jmp_le,elselabel);
  609. emitjmp(jmp_gt,l1);
  610. emit_const_reg(A_CMP,S_L,longint(lo(int64(t^._low))),hregister);
  611. { the comparisation of the low dword must be always unsigned! }
  612. emitjmp(C_B,elselabel);
  613. emitlab(l1);
  614. end
  615. else
  616. begin
  617. emit_const_reg(A_CMP,opsize,longint(t^._low),hregister);
  618. emitjmp(jmp_le,elselabel);
  619. end;
  620. end;
  621. if opsize=S_Q then
  622. begin
  623. getlabel(l1);
  624. emit_const_reg(A_CMP,S_L,longint(hi(int64(t^._high))),hregister2);
  625. emitjmp(jmp_le,t^.statement);
  626. emitjmp(jmp_gt,l1);
  627. emit_const_reg(A_CMP,S_L,longint(lo(int64(t^._high))),hregister);
  628. { the comparisation of the low dword must be always unsigned! }
  629. emitjmp(C_BE,t^.statement);
  630. emitlab(l1);
  631. end
  632. else
  633. begin
  634. emit_const_reg(A_CMP,opsize,longint(t^._high),hregister);
  635. emitjmp(jmp_lee,t^.statement);
  636. end;
  637. last:=t^._high;
  638. end;
  639. first:=false;
  640. if assigned(t^.greater) then
  641. genitem(t^.greater);
  642. end;
  643. begin
  644. last:=0;
  645. first:=true;
  646. genitem(hp);
  647. emitjmp(C_None,elselabel);
  648. end;
  649. procedure genlinearlist(hp : pcaserecord);
  650. var
  651. first : boolean;
  652. last : TConstExprInt;
  653. {helplabel : longint;}
  654. procedure genitem(t : pcaserecord);
  655. procedure gensub(value:longint);
  656. begin
  657. if value=1 then
  658. emit_reg(A_DEC,opsize,hregister)
  659. else
  660. emit_const_reg(A_SUB,opsize,value,hregister);
  661. end;
  662. begin
  663. if assigned(t^.less) then
  664. genitem(t^.less);
  665. { need we to test the first value }
  666. if first and (t^._low>get_min_value(left.resulttype.def)) then
  667. begin
  668. emit_const_reg(A_CMP,opsize,longint(t^._low),hregister);
  669. emitjmp(jmp_le,elselabel);
  670. end;
  671. if t^._low=t^._high then
  672. begin
  673. if t^._low-last=0 then
  674. emit_reg_reg(A_OR,opsize,hregister,hregister)
  675. else
  676. gensub(longint(t^._low-last));
  677. last:=t^._low;
  678. emitjmp(C_Z,t^.statement);
  679. end
  680. else
  681. begin
  682. { it begins with the smallest label, if the value }
  683. { is even smaller then jump immediately to the }
  684. { ELSE-label }
  685. if first then
  686. begin
  687. { have we to ajust the first value ? }
  688. if (t^._low>get_min_value(left.resulttype.def)) then
  689. gensub(t^._low);
  690. end
  691. else
  692. begin
  693. { if there is no unused label between the last and the }
  694. { present label then the lower limit can be checked }
  695. { immediately. else check the range in between: }
  696. { note: you can't use gensub() here because dec doesn't }
  697. { change the carry flag (needed for jmp_lxx) (JM) }
  698. emit_const_reg(A_SUB,opsize,longint(t^._low-last),hregister);
  699. emitjmp(jmp_le,elselabel);
  700. end;
  701. emit_const_reg(A_SUB,opsize,longint(t^._high-t^._low),hregister);
  702. emitjmp(jmp_lee,t^.statement);
  703. last:=t^._high;
  704. end;
  705. first:=false;
  706. if assigned(t^.greater) then
  707. genitem(t^.greater);
  708. end;
  709. begin
  710. { do we need to generate cmps? }
  711. if (with_sign and (min_label<0)) then
  712. genlinearcmplist(hp)
  713. else
  714. begin
  715. last:=0;
  716. first:=true;
  717. genitem(hp);
  718. emitjmp(C_None,elselabel);
  719. end;
  720. end;
  721. procedure genjumptable(hp : pcaserecord;min_,max_ : longint);
  722. var
  723. table : tasmlabel;
  724. last : TConstExprInt;
  725. hr : preference;
  726. procedure genitem(t : pcaserecord);
  727. var
  728. i : longint;
  729. begin
  730. if assigned(t^.less) then
  731. genitem(t^.less);
  732. { fill possible hole }
  733. for i:=last+1 to t^._low-1 do
  734. jumpSegment.concat(Tai_const_symbol.Create(elselabel));
  735. for i:=t^._low to t^._high do
  736. jumpSegment.concat(Tai_const_symbol.Create(t^.statement));
  737. last:=t^._high;
  738. if assigned(t^.greater) then
  739. genitem(t^.greater);
  740. end;
  741. begin
  742. if not(jumptable_no_range) then
  743. begin
  744. emit_const_reg(A_CMP,opsize,longint(min_),hregister);
  745. { case expr less than min_ => goto elselabel }
  746. emitjmp(jmp_le,elselabel);
  747. emit_const_reg(A_CMP,opsize,longint(max_),hregister);
  748. emitjmp(jmp_gt,elselabel);
  749. end;
  750. getlabel(table);
  751. { extend with sign }
  752. if opsize=S_W then
  753. begin
  754. if with_sign then
  755. emit_reg_reg(A_MOVSX,S_WL,hregister,
  756. reg16toreg32(hregister))
  757. else
  758. emit_reg_reg(A_MOVZX,S_WL,hregister,
  759. reg16toreg32(hregister));
  760. hregister:=reg16toreg32(hregister);
  761. end
  762. else if opsize=S_B then
  763. begin
  764. if with_sign then
  765. emit_reg_reg(A_MOVSX,S_BL,hregister,
  766. reg8toreg32(hregister))
  767. else
  768. emit_reg_reg(A_MOVZX,S_BL,hregister,
  769. reg8toreg32(hregister));
  770. hregister:=reg8toreg32(hregister);
  771. end;
  772. new(hr);
  773. reset_reference(hr^);
  774. hr^.symbol:=table;
  775. hr^.offset:=(-longint(min_))*4;
  776. hr^.index:=hregister;
  777. hr^.scalefactor:=4;
  778. emit_ref(A_JMP,S_NO,hr);
  779. { !!!!! generate tables
  780. if not(cs_littlesize in aktlocalswitches) then
  781. jumpSegment.concat(Taicpu.Op_const(A_ALIGN,S_NO,4));
  782. }
  783. jumpSegment.concat(Tai_label.Create(table));
  784. last:=min_;
  785. genitem(hp);
  786. { !!!!!!!
  787. if not(cs_littlesize in aktlocalswitches) then
  788. emit_const(A_ALIGN,S_NO,4);
  789. }
  790. end;
  791. var
  792. lv,hv,
  793. max_label: tconstexprint;
  794. labels : longint;
  795. max_linear_list : longint;
  796. otl, ofl: tasmlabel;
  797. {$ifdef Delphi}
  798. dist : cardinal;
  799. {$else Delphi}
  800. dist : dword;
  801. {$endif Delphi}
  802. hr : preference;
  803. begin
  804. getlabel(endlabel);
  805. getlabel(elselabel);
  806. if (cs_create_smart in aktmoduleswitches) then
  807. jumpsegment:=procinfo^.aktlocaldata
  808. else
  809. jumpsegment:=datasegment;
  810. with_sign:=is_signed(left.resulttype.def);
  811. if with_sign then
  812. begin
  813. jmp_gt:=C_G;
  814. jmp_le:=C_L;
  815. jmp_lee:=C_LE;
  816. end
  817. else
  818. begin
  819. jmp_gt:=C_A;
  820. jmp_le:=C_B;
  821. jmp_lee:=C_BE;
  822. end;
  823. cleartempgen;
  824. { save current truelabel and falselabel (they are restored in }
  825. { locjump2reg) (JM) }
  826. if left.location.loc=LOC_JUMP then
  827. begin
  828. otl:=truelabel;
  829. getlabel(truelabel);
  830. ofl:=falselabel;
  831. getlabel(falselabel);
  832. end;
  833. secondpass(left);
  834. { determines the size of the operand }
  835. opsize:=bytes2Sxx[left.resulttype.def.size];
  836. { copy the case expression to a register }
  837. case left.location.loc of
  838. LOC_REGISTER:
  839. begin
  840. if opsize=S_Q then
  841. begin
  842. hregister:=left.location.registerlow;
  843. hregister2:=left.location.registerhigh;
  844. end
  845. else
  846. hregister:=left.location.register;
  847. end;
  848. LOC_FLAGS :
  849. begin
  850. locflags2reg(left.location,opsize);
  851. hregister := left.location.register;
  852. end;
  853. LOC_JUMP:
  854. begin
  855. locjump2reg(left.location,opsize,otl,ofl);
  856. hregister := left.location.register;
  857. end;
  858. LOC_CREGISTER:
  859. begin
  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_reg_reg(A_MOV,S_L,left.location.registerlow,hregister);
  872. hr:=newreference(left.location.reference);
  873. inc(hr^.offset,4);
  874. emit_reg_reg(A_MOV,S_L,left.location.registerhigh,hregister2);
  875. end
  876. else
  877. emit_reg_reg(A_MOV,opsize,
  878. left.location.register,hregister);
  879. end;
  880. LOC_MEM,LOC_REFERENCE:
  881. begin
  882. del_reference(left.location.reference);
  883. hregister:=getregister32;
  884. case opsize of
  885. S_B:
  886. hregister:=reg32toreg8(hregister);
  887. S_W:
  888. hregister:=reg32toreg16(hregister);
  889. S_Q:
  890. hregister2:=R_EDI;
  891. end;
  892. if opsize=S_Q then
  893. begin
  894. emit_ref_reg(A_MOV,S_L,newreference(
  895. left.location.reference),hregister);
  896. hr:=newreference(left.location.reference);
  897. inc(hr^.offset,4);
  898. emit_ref_reg(A_MOV,S_L,hr,hregister2);
  899. end
  900. else
  901. emit_ref_reg(A_MOV,opsize,newreference(
  902. left.location.reference),hregister);
  903. end;
  904. else internalerror(2002);
  905. end;
  906. { we need the min_label always to choose between }
  907. { cmps and subs/decs }
  908. min_label:=case_get_min(nodes);
  909. load_all_regvars(exprasmlist);
  910. { now generate the jumps }
  911. if opsize=S_Q then
  912. genlinearcmplist(nodes)
  913. else
  914. begin
  915. if cs_optimize in aktglobalswitches then
  916. begin
  917. { procedures are empirically passed on }
  918. { consumption can also be calculated }
  919. { but does it pay on the different }
  920. { processors? }
  921. { moreover can the size only be appro- }
  922. { ximated as it is not known if rel8, }
  923. { rel16 or rel32 jumps are used }
  924. max_label:=case_get_max(nodes);
  925. labels:=case_count_labels(nodes);
  926. { can we omit the range check of the jump table ? }
  927. getrange(left.resulttype.def,lv,hv);
  928. jumptable_no_range:=(lv=min_label) and (hv=max_label);
  929. { hack a little bit, because the range can be greater }
  930. { than the positive range of a longint }
  931. if (min_label<0) and (max_label>0) then
  932. begin
  933. {$ifdef Delphi}
  934. if min_label=longint($80000000) then
  935. dist:=Cardinal(max_label)+Cardinal($80000000)
  936. else
  937. dist:=Cardinal(max_label)+Cardinal(-min_label)
  938. {$else Delphi}
  939. if min_label=$80000000 then
  940. dist:=dword(max_label)+dword($80000000)
  941. else
  942. dist:=dword(max_label)+dword(-min_label)
  943. {$endif Delphi}
  944. end
  945. else
  946. dist:=max_label-min_label;
  947. { optimize for size ? }
  948. if cs_littlesize in aktglobalswitches then
  949. begin
  950. if (labels<=2) or
  951. ((max_label-min_label)<0) or
  952. ((max_label-min_label)>3*labels) then
  953. { a linear list is always smaller than a jump tree }
  954. genlinearlist(nodes)
  955. else
  956. { if the labels less or more a continuum then }
  957. genjumptable(nodes,min_label,max_label);
  958. end
  959. else
  960. begin
  961. if jumptable_no_range then
  962. max_linear_list:=4
  963. else
  964. max_linear_list:=2;
  965. { a jump table crashes the pipeline! }
  966. if aktoptprocessor=Class386 then
  967. inc(max_linear_list,3);
  968. if aktoptprocessor=ClassP5 then
  969. inc(max_linear_list,6);
  970. if aktoptprocessor>=ClassP6 then
  971. inc(max_linear_list,9);
  972. if (labels<=max_linear_list) then
  973. genlinearlist(nodes)
  974. else
  975. begin
  976. if (dist>4*cardinal(labels)) then
  977. begin
  978. if labels>16 then
  979. gentreejmp(nodes)
  980. else
  981. genlinearlist(nodes);
  982. end
  983. else
  984. genjumptable(nodes,min_label,max_label);
  985. end;
  986. end;
  987. end
  988. else
  989. { it's always not bad }
  990. genlinearlist(nodes);
  991. end;
  992. ungetregister(hregister);
  993. { now generate the instructions }
  994. hp:=right;
  995. while assigned(hp) do
  996. begin
  997. cleartempgen;
  998. secondpass(tbinarynode(hp).right);
  999. { don't come back to case line }
  1000. aktfilepos:=exprasmList.getlasttaifilepos^;
  1001. load_all_regvars(exprasmlist);
  1002. emitjmp(C_None,endlabel);
  1003. hp:=tbinarynode(hp).left;
  1004. end;
  1005. emitlab(elselabel);
  1006. { ...and the else block }
  1007. if assigned(elseblock) then
  1008. begin
  1009. cleartempgen;
  1010. secondpass(elseblock);
  1011. load_all_regvars(exprasmlist);
  1012. end;
  1013. emitlab(endlabel);
  1014. end;
  1015. begin
  1016. csetelementnode:=ti386setelementnode;
  1017. cinnode:=ti386innode;
  1018. ccasenode:=ti386casenode;
  1019. end.
  1020. {
  1021. $Log$
  1022. Revision 1.18 2001-12-03 21:48:43 peter
  1023. * freemem change to value parameter
  1024. * torddef low/high range changed to int64
  1025. Revision 1.17 2001/09/04 11:38:55 jonas
  1026. + searchsystype() and searchsystype() functions in symtable
  1027. * changed ninl and nadd to use these functions
  1028. * i386 set comparison functions now return their results in al instead
  1029. of in the flags so that they can be sued as compilerprocs
  1030. - removed all processor specific code from n386add.pas that has to do
  1031. with set handling, it's now all done in nadd.pas
  1032. * fixed fpc_set_contains_sets in genset.inc
  1033. * fpc_set_in_byte is now coded inline in n386set.pas and doesn't use a
  1034. helper anymore
  1035. * some small fixes in compproc.inc/set.inc regarding the declaration of
  1036. internal helper types (fpc_small_set and fpc_normal_set)
  1037. Revision 1.16 2001/08/26 13:37:00 florian
  1038. * some cg reorganisation
  1039. * some PPC updates
  1040. Revision 1.15 2001/05/06 17:12:14 jonas
  1041. * fixed an IE10 and another bug with [var1..var2] construct
  1042. Revision 1.14 2001/04/13 01:22:19 peter
  1043. * symtable change to classes
  1044. * range check generation and errors fixed, make cycle DEBUG=1 works
  1045. * memory leaks fixed
  1046. Revision 1.13 2001/04/06 14:09:34 jonas
  1047. * fixed bug in ti386innode.pass_2 code and made it simpler/faster
  1048. Revision 1.12 2001/04/02 21:20:38 peter
  1049. * resulttype rewrite
  1050. Revision 1.11 2001/02/11 12:14:56 jonas
  1051. * simplified and optimized code generated for in-statements
  1052. Revision 1.10 2000/12/25 00:07:33 peter
  1053. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  1054. tlinkedlist objects)
  1055. Revision 1.9 2000/12/18 17:45:32 jonas
  1056. * int64 case fixes
  1057. * explicit longint type casts for constants used in assembler code
  1058. generation s,ice they can be cardinals too (or even int64's in case of
  1059. range check errors)
  1060. Revision 1.8 2000/12/16 15:58:18 jonas
  1061. * removed warnings about possible range check errors
  1062. Revision 1.7 2000/12/05 11:44:34 jonas
  1063. + new integer regvar handling, should be much more efficient
  1064. Revision 1.6 2000/11/29 00:30:49 florian
  1065. * unused units removed from uses clause
  1066. * some changes for widestrings
  1067. Revision 1.5 2000/11/17 14:09:00 jonas
  1068. * fixed webbug 1222 ("merged")
  1069. Revision 1.4 2000/11/13 14:44:36 jonas
  1070. * fixes so no more range errors with improved range checking code
  1071. Revision 1.3 2000/10/31 22:02:57 peter
  1072. * symtable splitted, no real code changes
  1073. Revision 1.2 2000/10/26 15:53:27 jonas
  1074. * fixed web bug1192 (changed an ungetregister32 to ungetregister)
  1075. ("merged" from fixes)
  1076. Revision 1.1 2000/10/15 09:33:32 peter
  1077. * moved n386*.pas to i386/ cpu_target dir
  1078. Revision 1.4 2000/10/14 10:14:49 peter
  1079. * moehrendorf oct 2000 rewrite
  1080. Revision 1.3 2000/09/30 16:08:45 peter
  1081. * more cg11 updates
  1082. Revision 1.2 2000/09/24 20:17:44 florian
  1083. * more conversion work done
  1084. Revision 1.1 2000/09/24 19:38:39 florian
  1085. * initial implementation
  1086. }