n386set.pas 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 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 fpcdefs.inc}
  20. interface
  21. uses
  22. node,nset,pass_1;
  23. type
  24. ti386innode = class(tinnode)
  25. procedure pass_2;override;
  26. function pass_1 : tnode;override;
  27. end;
  28. ti386casenode = class(tcasenode)
  29. procedure pass_2;override;
  30. end;
  31. implementation
  32. uses
  33. globtype,systems,
  34. verbose,globals,
  35. symconst,symdef,defbase,
  36. aasmbase,aasmtai,aasmcpu,
  37. cginfo,cgbase,pass_2,
  38. ncon,
  39. cpubase,
  40. cga,cgobj,tgobj,ncgutil,regvars,rgobj;
  41. const
  42. bytes2Sxx:array[1..8] of Topsize=(S_B,S_W,S_NO,S_L,S_NO,S_NO,S_NO,S_Q);
  43. {*****************************************************************************
  44. TI386INNODE
  45. *****************************************************************************}
  46. function ti386innode.pass_1 : tnode;
  47. begin
  48. result:=nil;
  49. { this is the only difference from the generic version }
  50. location.loc:=LOC_FLAGS;
  51. firstpass(right);
  52. firstpass(left);
  53. if codegenerror then
  54. exit;
  55. left_right_max;
  56. { this is not allways true due to optimization }
  57. { but if we don't set this we get problems with optimizing self code }
  58. if tsetdef(right.resulttype.def).settype<>smallset then
  59. procinfo^.flags:=procinfo^.flags or pi_do_call
  60. else
  61. begin
  62. { a smallset needs maybe an misc. register }
  63. if (left.nodetype<>ordconstn) and
  64. not(right.location.loc in [LOC_CREGISTER,LOC_REGISTER]) and
  65. (right.registers32<1) then
  66. inc(registers32);
  67. end;
  68. end;
  69. procedure ti386innode.pass_2;
  70. type
  71. Tsetpart=record
  72. range : boolean; {Part is a range.}
  73. start,stop : byte; {Start/stop when range; Stop=element when an element.}
  74. end;
  75. var
  76. genjumps,
  77. use_small,
  78. ranges : boolean;
  79. hr,hr2,
  80. pleftreg : tregister;
  81. href : treference;
  82. opsize : topsize;
  83. setparts : array[1..8] of Tsetpart;
  84. i,numparts : byte;
  85. adjustment : longint;
  86. pushedregs : tmaybesave;
  87. l,l2 : tasmlabel;
  88. {$ifdef CORRECT_SET_IN_FPC}
  89. AM : tasmop;
  90. {$endif CORRECT_SET_IN_FPC}
  91. function analizeset(Aset:pconstset;is_small:boolean):boolean;
  92. type
  93. byteset=set of byte;
  94. var
  95. compares,maxcompares:word;
  96. i:byte;
  97. begin
  98. analizeset:=false;
  99. ranges:=false;
  100. numparts:=0;
  101. compares:=0;
  102. { Lots of comparisions take a lot of time, so do not allow
  103. too much comparisions. 8 comparisions are, however, still
  104. smalller than emitting the set }
  105. if cs_littlesize in aktglobalswitches then
  106. maxcompares:=8
  107. else
  108. maxcompares:=5;
  109. { when smallset is possible allow only 3 compares the smallset
  110. code is for littlesize also smaller when more compares are used }
  111. if is_small then
  112. maxcompares:=3;
  113. for i:=0 to 255 do
  114. if i in byteset(Aset^) then
  115. begin
  116. if (numparts=0) or (i<>setparts[numparts].stop+1) then
  117. begin
  118. {Set element is a separate element.}
  119. inc(compares);
  120. if compares>maxcompares then
  121. exit;
  122. inc(numparts);
  123. setparts[numparts].range:=false;
  124. setparts[numparts].stop:=i;
  125. end
  126. else
  127. {Set element is part of a range.}
  128. if not setparts[numparts].range then
  129. begin
  130. {Transform an element into a range.}
  131. setparts[numparts].range:=true;
  132. setparts[numparts].start:=setparts[numparts].stop;
  133. setparts[numparts].stop:=i;
  134. ranges := true;
  135. { there's only one compare per range anymore. Only a }
  136. { sub is added, but that's much faster than a }
  137. { cmp/jcc combo so neglect its effect }
  138. { inc(compares);
  139. if compares>maxcompares then
  140. exit; }
  141. end
  142. else
  143. begin
  144. {Extend a range.}
  145. setparts[numparts].stop:=i;
  146. end;
  147. end;
  148. analizeset:=true;
  149. end;
  150. begin
  151. { We check first if we can generate jumps, this can be done
  152. because the resulttype.def is already set in firstpass }
  153. { check if we can use smallset operation using btl which is limited
  154. to 32 bits, the left side may also not contain higher values !! }
  155. use_small:=(tsetdef(right.resulttype.def).settype=smallset) and
  156. ((left.resulttype.def.deftype=orddef) and (torddef(left.resulttype.def).high<=32) or
  157. (left.resulttype.def.deftype=enumdef) and (tenumdef(left.resulttype.def).max<=32));
  158. { Can we generate jumps? Possible for all types of sets }
  159. genjumps:=(right.nodetype=setconstn) and
  160. analizeset(tsetconstnode(right).value_set,use_small);
  161. { calculate both operators }
  162. { the complex one first }
  163. firstcomplex(self);
  164. secondpass(left);
  165. { Only process the right if we are not generating jumps }
  166. if not genjumps then
  167. begin
  168. maybe_save(exprasmlist,right.registers32,left.location,pushedregs);
  169. secondpass(right);
  170. maybe_restore(exprasmlist,left.location,pushedregs);
  171. end;
  172. if codegenerror then
  173. exit;
  174. { ofcourse not commutative }
  175. if nf_swaped in flags then
  176. swapleftright;
  177. if genjumps then
  178. begin
  179. { It gives us advantage to check for the set elements
  180. separately instead of using the SET_IN_BYTE procedure.
  181. To do: Build in support for LOC_JUMP }
  182. opsize := def_opsize(left.resulttype.def);
  183. { If register is used, use only lower 8 bits }
  184. if left.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  185. begin
  186. { for ranges we always need a 32bit register, because then we }
  187. { use the register as base in a reference (JM) }
  188. if ranges then
  189. begin
  190. pleftreg:=rg.makeregsize(left.location.register,OS_INT);
  191. cg.a_load_reg_reg(exprasmlist,left.location.size,left.location.register,pleftreg);
  192. if opsize <> S_L then
  193. emit_const_reg(A_AND,S_L,255,pleftreg);
  194. opsize := S_L;
  195. end
  196. else
  197. { otherwise simply use the lower 8 bits (no "and" }
  198. { necessary this way) (JM) }
  199. begin
  200. pleftreg:=rg.makeregsize(left.location.register,OS_8);
  201. opsize := S_B;
  202. end;
  203. end
  204. else
  205. begin
  206. { load the value in a register }
  207. pleftreg := rg.getexplicitregisterint(exprasmlist,R_EDI);
  208. opsize := S_L;
  209. emit_ref_reg(A_MOVZX,S_BL,left.location.reference,pleftreg);
  210. end;
  211. { Get a label to jump to the end }
  212. location_reset(location,LOC_FLAGS,OS_NO);
  213. { It's better to use the zero flag when there are
  214. no ranges }
  215. if ranges then
  216. location.resflags:=F_C
  217. else
  218. location.resflags:=F_E;
  219. getlabel(l);
  220. { how much have we already substracted from the x in the }
  221. { "x in [y..z]" expression }
  222. adjustment := 0;
  223. for i:=1 to numparts do
  224. if setparts[i].range then
  225. { use fact that a <= x <= b <=> cardinal(x-a) <= cardinal(b-a) }
  226. begin
  227. { is the range different from all legal values? }
  228. if (setparts[i].stop-setparts[i].start <> 255) then
  229. begin
  230. { yes, is the lower bound <> 0? }
  231. if (setparts[i].start <> 0) then
  232. { we're going to substract from the left register, }
  233. { so in case of a LOC_CREGISTER first move the value }
  234. { to edi (not done before because now we can do the }
  235. { move and substract in one instruction with LEA) }
  236. if (pleftreg <> R_EDI) and
  237. (left.location.loc = LOC_CREGISTER) then
  238. begin
  239. rg.ungetregister(exprasmlist,pleftreg);
  240. rg.getexplicitregisterint(exprasmlist,R_EDI);
  241. reference_reset_base(href,pleftreg,-setparts[i].start);
  242. emit_ref_reg(A_LEA,S_L,href,R_EDI);
  243. { only now change pleftreg since previous value is }
  244. { still used in previous instruction }
  245. pleftreg := R_EDI;
  246. opsize := S_L;
  247. end
  248. else
  249. begin
  250. { otherwise, the value is already in a register }
  251. { that can be modified }
  252. if setparts[i].start-adjustment <> 1 then
  253. emit_const_reg(A_SUB,opsize,
  254. setparts[i].start-adjustment,pleftreg)
  255. else emit_reg(A_DEC,opsize,pleftreg);
  256. end;
  257. { new total value substracted from x: }
  258. { adjustment + (setparts[i].start - adjustment) }
  259. adjustment := setparts[i].start;
  260. { check if result < b-a+1 (not "result <= b-a", since }
  261. { we need a carry in case the element is in the range }
  262. { (this will never overflow since we check at the }
  263. { beginning whether stop-start <> 255) }
  264. emit_const_reg(A_CMP,opsize,
  265. setparts[i].stop-setparts[i].start+1,pleftreg);
  266. { use C_C instead of C_B: the meaning is the same, but }
  267. { then the optimizer can easier trace the jump to its }
  268. { final destination since the resultflag of this node }
  269. { is set to the carryflag }
  270. emitjmp(C_C,l);
  271. end
  272. else
  273. { if setparts[i].start = 0 and setparts[i].stop = 255, }
  274. { it's always true since "in" is only allowed for bytes }
  275. begin
  276. emit_none(A_STC,S_NO);
  277. cg.a_jmp_always(exprasmlist,l);
  278. end;
  279. end
  280. else
  281. begin
  282. { Emit code to check if left is an element }
  283. emit_const_reg(A_CMP,opsize,setparts[i].stop-adjustment,
  284. pleftreg);
  285. { Result should be in carry flag when ranges are used }
  286. if ranges then
  287. emit_none(A_STC,S_NO);
  288. { If found, jump to end }
  289. emitjmp(C_E,l);
  290. end;
  291. if ranges and
  292. { if the last one was a range, the carry flag is already }
  293. { set appropriately }
  294. not(setparts[numparts].range) then
  295. emit_none(A_CLC,S_NO);
  296. { To compensate for not doing a second pass }
  297. right.location.reference.symbol:=nil;
  298. { Now place the end label }
  299. cg.a_label(exprasmlist,l);
  300. case left.location.loc of
  301. LOC_REGISTER,
  302. LOC_CREGISTER :
  303. rg.ungetregister(exprasmlist,pleftreg);
  304. else
  305. begin
  306. reference_release(exprasmlist,left.location.reference);
  307. rg.ungetregister(exprasmlist,R_EDI);
  308. end;
  309. end;
  310. end
  311. else
  312. begin
  313. location_reset(location,LOC_FLAGS,OS_NO);
  314. { We will now generated code to check the set itself, no jmps,
  315. handle smallsets separate, because it allows faster checks }
  316. if use_small then
  317. begin
  318. if left.nodetype=ordconstn then
  319. begin
  320. location.resflags:=F_NE;
  321. case right.location.loc of
  322. LOC_REGISTER,
  323. LOC_CREGISTER:
  324. begin
  325. emit_const_reg(A_TEST,S_L,
  326. 1 shl (tordconstnode(left).value and 31),right.location.register);
  327. end;
  328. LOC_REFERENCE,
  329. LOC_CREFERENCE :
  330. begin
  331. emit_const_ref(A_TEST,S_L,1 shl (tordconstnode(left).value and 31),
  332. right.location.reference);
  333. end;
  334. else
  335. internalerror(200203312);
  336. end;
  337. location_release(exprasmlist,right.location);
  338. end
  339. else
  340. begin
  341. case left.location.loc of
  342. LOC_REGISTER,
  343. LOC_CREGISTER:
  344. begin
  345. hr:=rg.makeregsize(left.location.register,OS_INT);
  346. cg.a_load_reg_reg(exprasmlist,left.location.size,left.location.register,hr);
  347. end;
  348. else
  349. begin
  350. { the set element isn't never samller than a byte }
  351. { and because it's a small set we need only 5 bits }
  352. { but 8 bits are easier to load }
  353. rg.getexplicitregisterint(exprasmlist,R_EDI);
  354. emit_ref_reg(A_MOVZX,S_BL,left.location.reference,R_EDI);
  355. hr:=R_EDI;
  356. location_release(exprasmlist,left.location);
  357. end;
  358. end;
  359. case right.location.loc of
  360. LOC_REGISTER,
  361. LOC_CREGISTER :
  362. begin
  363. emit_reg_reg(A_BT,S_L,hr,
  364. right.location.register);
  365. rg.ungetregisterint(exprasmlist,right.location.register);
  366. end;
  367. LOC_CONSTANT :
  368. begin
  369. { We have to load the value into a register because
  370. btl does not accept values only refs or regs (PFV) }
  371. hr2:=rg.getregisterint(exprasmlist);
  372. emit_const_reg(A_MOV,S_L,
  373. right.location.value,hr2);
  374. emit_reg_reg(A_BT,S_L,hr,hr2);
  375. rg.ungetregisterint(exprasmlist,hr2);
  376. end;
  377. LOC_CREFERENCE,
  378. LOC_REFERENCE :
  379. begin
  380. location_release(exprasmlist,right.location);
  381. emit_reg_ref(A_BT,S_L,hr,right.location.reference);
  382. end;
  383. else
  384. internalerror(2002032210);
  385. end;
  386. { simply to indicate EDI is deallocated here too (JM) }
  387. rg.ungetregisterint(exprasmlist,hr);
  388. location.loc:=LOC_FLAGS;
  389. location.resflags:=F_C;
  390. end;
  391. end
  392. else
  393. begin
  394. if right.location.loc=LOC_CONSTANT then
  395. begin
  396. location.resflags:=F_C;
  397. getlabel(l);
  398. getlabel(l2);
  399. { Is this treated in firstpass ?? }
  400. if left.nodetype=ordconstn then
  401. begin
  402. hr:=rg.getregisterint(exprasmlist);
  403. left.location.loc:=LOC_REGISTER;
  404. left.location.register:=hr;
  405. emit_const_reg(A_MOV,S_L,
  406. tordconstnode(left).value,hr);
  407. end;
  408. case left.location.loc of
  409. LOC_REGISTER,
  410. LOC_CREGISTER:
  411. begin
  412. hr:=rg.makeregsize(left.location.register,OS_INT);
  413. cg.a_load_reg_reg(exprasmlist,left.location.size,left.location.register,hr);
  414. emit_const_reg(A_CMP,S_L,31,hr);
  415. emitjmp(C_NA,l);
  416. { reset carry flag }
  417. emit_none(A_CLC,S_NO);
  418. cg.a_jmp_always(exprasmlist,l2);
  419. cg.a_label(exprasmlist,l);
  420. { We have to load the value into a register because
  421. btl does not accept values only refs or regs (PFV) }
  422. hr2:=rg.getregisterint(exprasmlist);
  423. emit_const_reg(A_MOV,S_L,right.location.value,hr2);
  424. emit_reg_reg(A_BT,S_L,hr,hr2);
  425. rg.ungetregisterint(exprasmlist,hr2);
  426. end;
  427. else
  428. begin
  429. {$ifdef CORRECT_SET_IN_FPC}
  430. if m_tp in aktmodeswitches then
  431. begin
  432. {***WARNING only correct if
  433. reference is 32 bits (PM) *****}
  434. emit_const_ref(A_CMP,S_L,31,reference_copy(left.location.reference));
  435. end
  436. else
  437. {$endif CORRECT_SET_IN_FPC}
  438. begin
  439. emit_const_ref(A_CMP,S_B,31,left.location.reference);
  440. end;
  441. emitjmp(C_NA,l);
  442. { reset carry flag }
  443. emit_none(A_CLC,S_NO);
  444. cg.a_jmp_always(exprasmlist,l2);
  445. cg.a_label(exprasmlist,l);
  446. location_release(exprasmlist,left.location);
  447. hr:=rg.getregisterint(exprasmlist);
  448. emit_ref_reg(A_MOV,S_L,left.location.reference,hr);
  449. { We have to load the value into a register because
  450. btl does not accept values only refs or regs (PFV) }
  451. hr2:=rg.getregisterint(exprasmlist);
  452. emit_const_reg(A_MOV,S_L,
  453. right.location.value,hr2);
  454. emit_reg_reg(A_BT,S_L,hr,hr2);
  455. rg.ungetregisterint(exprasmlist,hr2);
  456. end;
  457. end;
  458. cg.a_label(exprasmlist,l2);
  459. end { of right.location.loc=LOC_CONSTANT }
  460. { do search in a normal set which could have >32 elementsm
  461. but also used if the left side contains higher values > 32 }
  462. else if left.nodetype=ordconstn then
  463. begin
  464. location.resflags:=F_NE;
  465. inc(right.location.reference.offset,tordconstnode(left).value shr 3);
  466. emit_const_ref(A_TEST,S_B,1 shl (tordconstnode(left).value and 7),right.location.reference);
  467. location_release(exprasmlist,right.location);
  468. end
  469. else
  470. begin
  471. if (left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  472. pleftreg:=rg.makeregsize(left.location.register,OS_INT)
  473. else
  474. pleftreg:=rg.getexplicitregisterint(exprasmlist,R_EDI);
  475. cg.a_load_loc_reg(exprasmlist,left.location,pleftreg);
  476. location_freetemp(exprasmlist,left.location);
  477. location_release(exprasmlist,left.location);
  478. emit_reg_ref(A_BT,S_L,pleftreg,right.location.reference);
  479. rg.ungetregister(exprasmlist,pleftreg);
  480. location_release(exprasmlist,right.location);
  481. { tg.ungetiftemp(exprasmlist,right.location.reference) happens below }
  482. location.resflags:=F_C;
  483. end;
  484. end;
  485. end;
  486. location_freetemp(exprasmlist,right.location);
  487. end;
  488. {*****************************************************************************
  489. TI386CASENODE
  490. *****************************************************************************}
  491. procedure ti386casenode.pass_2;
  492. var
  493. with_sign : boolean;
  494. opsize : topsize;
  495. jmp_gt,jmp_le,jmp_lee : tasmcond;
  496. hp : tnode;
  497. { register with case expression }
  498. hregister,hregister2 : tregister;
  499. endlabel,elselabel : tasmlabel;
  500. { true, if we can omit the range check of the jump table }
  501. jumptable_no_range : boolean;
  502. { where to put the jump table }
  503. jumpsegment : TAAsmoutput;
  504. min_label : TConstExprInt;
  505. procedure gentreejmp(p : pcaserecord);
  506. var
  507. lesslabel,greaterlabel : tasmlabel;
  508. begin
  509. cg.a_label(exprasmlist,p^._at);
  510. { calculate labels for left and right }
  511. if (p^.less=nil) then
  512. lesslabel:=elselabel
  513. else
  514. lesslabel:=p^.less^._at;
  515. if (p^.greater=nil) then
  516. greaterlabel:=elselabel
  517. else
  518. greaterlabel:=p^.greater^._at;
  519. { calculate labels for left and right }
  520. { no range label: }
  521. if p^._low=p^._high then
  522. begin
  523. emit_const_reg(A_CMP,opsize,p^._low,hregister);
  524. if greaterlabel=lesslabel then
  525. emitjmp(C_NE,lesslabel)
  526. else
  527. begin
  528. emitjmp(jmp_le,lesslabel);
  529. emitjmp(jmp_gt,greaterlabel);
  530. end;
  531. cg.a_jmp_always(exprasmlist,p^.statement);
  532. end
  533. else
  534. begin
  535. emit_const_reg(A_CMP,opsize,p^._low,hregister);
  536. emitjmp(jmp_le,lesslabel);
  537. emit_const_reg(A_CMP,opsize,p^._high,hregister);
  538. emitjmp(jmp_gt,greaterlabel);
  539. cg.a_jmp_always(exprasmlist,p^.statement);
  540. end;
  541. if assigned(p^.less) then
  542. gentreejmp(p^.less);
  543. if assigned(p^.greater) then
  544. gentreejmp(p^.greater);
  545. end;
  546. procedure genlinearcmplist(hp : pcaserecord);
  547. var
  548. first : boolean;
  549. last : TConstExprInt;
  550. procedure genitem(t : pcaserecord);
  551. var
  552. l1 : tasmlabel;
  553. begin
  554. if assigned(t^.less) then
  555. genitem(t^.less);
  556. if t^._low=t^._high then
  557. begin
  558. if opsize=S_Q then
  559. begin
  560. getlabel(l1);
  561. emit_const_reg(A_CMP,S_L,longint(hi(int64(t^._low))),hregister2);
  562. emitjmp(C_NZ,l1);
  563. emit_const_reg(A_CMP,S_L,longint(lo(int64(t^._low))),hregister);
  564. emitjmp(C_Z,t^.statement);
  565. cg.a_label(exprasmlist,l1);
  566. end
  567. else
  568. begin
  569. emit_const_reg(A_CMP,opsize,longint(t^._low),hregister);
  570. emitjmp(C_Z,t^.statement);
  571. last:=t^._low;
  572. end;
  573. end
  574. else
  575. begin
  576. { if there is no unused label between the last and the }
  577. { present label then the lower limit can be checked }
  578. { immediately. else check the range in between: }
  579. if first or (t^._low-last>1) 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(jmp_le,elselabel);
  586. emitjmp(jmp_gt,l1);
  587. emit_const_reg(A_CMP,S_L,longint(lo(int64(t^._low))),hregister);
  588. { the comparisation of the low dword must be always unsigned! }
  589. emitjmp(C_B,elselabel);
  590. cg.a_label(exprasmlist,l1);
  591. end
  592. else
  593. begin
  594. emit_const_reg(A_CMP,opsize,longint(t^._low),hregister);
  595. emitjmp(jmp_le,elselabel);
  596. end;
  597. end;
  598. if opsize=S_Q then
  599. begin
  600. getlabel(l1);
  601. emit_const_reg(A_CMP,S_L,longint(hi(int64(t^._high))),hregister2);
  602. emitjmp(jmp_le,t^.statement);
  603. emitjmp(jmp_gt,l1);
  604. emit_const_reg(A_CMP,S_L,longint(lo(int64(t^._high))),hregister);
  605. { the comparisation of the low dword must be always unsigned! }
  606. emitjmp(C_BE,t^.statement);
  607. cg.a_label(exprasmlist,l1);
  608. end
  609. else
  610. begin
  611. emit_const_reg(A_CMP,opsize,longint(t^._high),hregister);
  612. emitjmp(jmp_lee,t^.statement);
  613. end;
  614. last:=t^._high;
  615. end;
  616. first:=false;
  617. if assigned(t^.greater) then
  618. genitem(t^.greater);
  619. end;
  620. begin
  621. last:=0;
  622. first:=true;
  623. genitem(hp);
  624. cg.a_jmp_always(exprasmlist,elselabel);
  625. end;
  626. procedure genlinearlist(hp : pcaserecord);
  627. var
  628. first : boolean;
  629. last : TConstExprInt;
  630. {helplabel : longint;}
  631. procedure genitem(t : pcaserecord);
  632. procedure gensub(value:longint);
  633. begin
  634. if value=1 then
  635. emit_reg(A_DEC,opsize,hregister)
  636. else
  637. emit_const_reg(A_SUB,opsize,value,hregister);
  638. end;
  639. begin
  640. if assigned(t^.less) then
  641. genitem(t^.less);
  642. { need we to test the first value }
  643. if first and (t^._low>get_min_value(left.resulttype.def)) then
  644. begin
  645. emit_const_reg(A_CMP,opsize,longint(t^._low),hregister);
  646. emitjmp(jmp_le,elselabel);
  647. end;
  648. if t^._low=t^._high then
  649. begin
  650. if t^._low-last=0 then
  651. emit_reg_reg(A_OR,opsize,hregister,hregister)
  652. else
  653. gensub(longint(t^._low-last));
  654. last:=t^._low;
  655. emitjmp(C_Z,t^.statement);
  656. end
  657. else
  658. begin
  659. { it begins with the smallest label, if the value }
  660. { is even smaller then jump immediately to the }
  661. { ELSE-label }
  662. if first then
  663. begin
  664. { have we to ajust the first value ? }
  665. if (t^._low>get_min_value(left.resulttype.def)) then
  666. gensub(longint(t^._low));
  667. end
  668. else
  669. begin
  670. { if there is no unused label between the last and the }
  671. { present label then the lower limit can be checked }
  672. { immediately. else check the range in between: }
  673. { note: you can't use gensub() here because dec doesn't }
  674. { change the carry flag (needed for jmp_lxx) (JM) }
  675. emit_const_reg(A_SUB,opsize,longint(t^._low-last),hregister);
  676. emitjmp(jmp_le,elselabel);
  677. end;
  678. emit_const_reg(A_SUB,opsize,longint(t^._high-t^._low),hregister);
  679. emitjmp(jmp_lee,t^.statement);
  680. last:=t^._high;
  681. end;
  682. first:=false;
  683. if assigned(t^.greater) then
  684. genitem(t^.greater);
  685. end;
  686. begin
  687. { do we need to generate cmps? }
  688. if (with_sign and (min_label<0)) then
  689. genlinearcmplist(hp)
  690. else
  691. begin
  692. last:=0;
  693. first:=true;
  694. genitem(hp);
  695. cg.a_jmp_always(exprasmlist,elselabel);
  696. end;
  697. end;
  698. procedure genjumptable(hp : pcaserecord;min_,max_ : longint);
  699. var
  700. table : tasmlabel;
  701. last : TConstExprInt;
  702. href : treference;
  703. procedure genitem(t : pcaserecord);
  704. var
  705. i : longint;
  706. begin
  707. if assigned(t^.less) then
  708. genitem(t^.less);
  709. { fill possible hole }
  710. for i:=last+1 to t^._low-1 do
  711. jumpSegment.concat(Tai_const_symbol.Create(elselabel));
  712. for i:=t^._low to t^._high do
  713. jumpSegment.concat(Tai_const_symbol.Create(t^.statement));
  714. last:=t^._high;
  715. if assigned(t^.greater) then
  716. genitem(t^.greater);
  717. end;
  718. begin
  719. if not(jumptable_no_range) then
  720. begin
  721. emit_const_reg(A_CMP,opsize,longint(min_),hregister);
  722. { case expr less than min_ => goto elselabel }
  723. emitjmp(jmp_le,elselabel);
  724. emit_const_reg(A_CMP,opsize,longint(max_),hregister);
  725. emitjmp(jmp_gt,elselabel);
  726. end;
  727. getlabel(table);
  728. { extend with sign }
  729. if opsize=S_W then
  730. begin
  731. if with_sign then
  732. emit_reg_reg(A_MOVSX,S_WL,hregister,
  733. rg.makeregsize(hregister,OS_INT))
  734. else
  735. emit_reg_reg(A_MOVZX,S_WL,hregister,
  736. rg.makeregsize(hregister,OS_INT));
  737. hregister:=rg.makeregsize(hregister,OS_INT);
  738. end
  739. else if opsize=S_B then
  740. begin
  741. if with_sign then
  742. emit_reg_reg(A_MOVSX,S_BL,hregister,
  743. rg.makeregsize(hregister,OS_INT))
  744. else
  745. emit_reg_reg(A_MOVZX,S_BL,hregister,
  746. rg.makeregsize(hregister,OS_INT));
  747. hregister:=rg.makeregsize(hregister,OS_INT);
  748. end;
  749. reference_reset_symbol(href,table,0);
  750. href.offset:=(-longint(min_))*4;
  751. href.index:=hregister;
  752. href.scalefactor:=4;
  753. emit_ref(A_JMP,S_NO,href);
  754. { !!!!! generate tables
  755. if not(cs_littlesize in aktlocalswitches) then
  756. jumpSegment.concat(Taicpu.Op_const(A_ALIGN,S_NO,4));
  757. }
  758. jumpSegment.concat(Tai_label.Create(table));
  759. last:=min_;
  760. genitem(hp);
  761. { !!!!!!!
  762. if not(cs_littlesize in aktlocalswitches) then
  763. emit_const(A_ALIGN,S_NO,4);
  764. }
  765. end;
  766. var
  767. lv,hv,
  768. max_label: tconstexprint;
  769. labels : longint;
  770. max_linear_list : longint;
  771. otl, ofl: tasmlabel;
  772. isjump : boolean;
  773. {$ifdef Delphi}
  774. dist : cardinal;
  775. {$else Delphi}
  776. dist : dword;
  777. {$endif Delphi}
  778. begin
  779. getlabel(endlabel);
  780. getlabel(elselabel);
  781. if (cs_create_smart in aktmoduleswitches) then
  782. jumpsegment:=procinfo^.aktlocaldata
  783. else
  784. jumpsegment:=datasegment;
  785. with_sign:=is_signed(left.resulttype.def);
  786. if with_sign then
  787. begin
  788. jmp_gt:=C_G;
  789. jmp_le:=C_L;
  790. jmp_lee:=C_LE;
  791. end
  792. else
  793. begin
  794. jmp_gt:=C_A;
  795. jmp_le:=C_B;
  796. jmp_lee:=C_BE;
  797. end;
  798. rg.cleartempgen;
  799. { save current truelabel and falselabel }
  800. isjump:=false;
  801. if left.location.loc=LOC_JUMP then
  802. begin
  803. otl:=truelabel;
  804. getlabel(truelabel);
  805. ofl:=falselabel;
  806. getlabel(falselabel);
  807. isjump:=true;
  808. end;
  809. secondpass(left);
  810. { determines the size of the operand }
  811. opsize:=bytes2Sxx[left.resulttype.def.size];
  812. { copy the case expression to a register }
  813. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),false);
  814. if opsize=S_Q then
  815. begin
  816. hregister:=left.location.registerlow;
  817. hregister2:=left.location.registerhigh;
  818. end
  819. else
  820. hregister:=left.location.register;
  821. if isjump then
  822. begin
  823. truelabel:=otl;
  824. falselabel:=ofl;
  825. end;
  826. { we need the min_label always to choose between }
  827. { cmps and subs/decs }
  828. min_label:=case_get_min(nodes);
  829. load_all_regvars(exprasmlist);
  830. { now generate the jumps }
  831. if opsize=S_Q then
  832. genlinearcmplist(nodes)
  833. else
  834. begin
  835. if cs_optimize in aktglobalswitches then
  836. begin
  837. { procedures are empirically passed on }
  838. { consumption can also be calculated }
  839. { but does it pay on the different }
  840. { processors? }
  841. { moreover can the size only be appro- }
  842. { ximated as it is not known if rel8, }
  843. { rel16 or rel32 jumps are used }
  844. max_label:=case_get_max(nodes);
  845. labels:=case_count_labels(nodes);
  846. { can we omit the range check of the jump table ? }
  847. getrange(left.resulttype.def,lv,hv);
  848. jumptable_no_range:=(lv=min_label) and (hv=max_label);
  849. { hack a little bit, because the range can be greater }
  850. { than the positive range of a longint }
  851. if (min_label<0) and (max_label>0) then
  852. begin
  853. {$ifdef Delphi}
  854. if min_label=longint($80000000) then
  855. dist:=Cardinal(max_label)+Cardinal($80000000)
  856. else
  857. dist:=Cardinal(max_label)+Cardinal(-min_label)
  858. {$else Delphi}
  859. if min_label=$80000000 then
  860. dist:=dword(max_label)+dword($80000000)
  861. else
  862. dist:=dword(max_label)+dword(-min_label)
  863. {$endif Delphi}
  864. end
  865. else
  866. dist:=max_label-min_label;
  867. { optimize for size ? }
  868. if cs_littlesize in aktglobalswitches then
  869. begin
  870. if (labels<=2) or
  871. ((max_label-min_label)<0) or
  872. ((max_label-min_label)>3*labels) then
  873. { a linear list is always smaller than a jump tree }
  874. genlinearlist(nodes)
  875. else
  876. { if the labels less or more a continuum then }
  877. genjumptable(nodes,min_label,max_label);
  878. end
  879. else
  880. begin
  881. if jumptable_no_range then
  882. max_linear_list:=4
  883. else
  884. max_linear_list:=2;
  885. { a jump table crashes the pipeline! }
  886. if aktoptprocessor=Class386 then
  887. inc(max_linear_list,3);
  888. if aktoptprocessor=ClassP5 then
  889. inc(max_linear_list,6);
  890. if aktoptprocessor>=ClassP6 then
  891. inc(max_linear_list,9);
  892. if (labels<=max_linear_list) then
  893. genlinearlist(nodes)
  894. else
  895. begin
  896. if (dist>4*cardinal(labels)) then
  897. begin
  898. if labels>16 then
  899. gentreejmp(nodes)
  900. else
  901. genlinearlist(nodes);
  902. end
  903. else
  904. genjumptable(nodes,min_label,max_label);
  905. end;
  906. end;
  907. end
  908. else
  909. { it's always not bad }
  910. genlinearlist(nodes);
  911. end;
  912. rg.ungetregister(exprasmlist,hregister);
  913. { now generate the instructions }
  914. hp:=right;
  915. while assigned(hp) do
  916. begin
  917. rg.cleartempgen;
  918. secondpass(tbinarynode(hp).right);
  919. { don't come back to case line }
  920. aktfilepos:=exprasmList.getlasttaifilepos^;
  921. load_all_regvars(exprasmlist);
  922. cg.a_jmp_always(exprasmlist,endlabel);
  923. hp:=tbinarynode(hp).left;
  924. end;
  925. cg.a_label(exprasmlist,elselabel);
  926. { ...and the else block }
  927. if assigned(elseblock) then
  928. begin
  929. rg.cleartempgen;
  930. secondpass(elseblock);
  931. load_all_regvars(exprasmlist);
  932. end;
  933. cg.a_label(exprasmlist,endlabel);
  934. end;
  935. begin
  936. {$ifndef TEST_GENERIC}
  937. cinnode:=ti386innode;
  938. {$endif}
  939. ccasenode:=ti386casenode;
  940. end.
  941. {
  942. $Log$
  943. Revision 1.35 2002-07-20 11:58:04 florian
  944. * types.pas renamed to defbase.pas because D6 contains a types
  945. unit so this would conflicts if D6 programms are compiled
  946. + Willamette/SSE2 instructions to assembler added
  947. Revision 1.34 2002/07/11 14:41:34 florian
  948. * start of the new generic parameter handling
  949. Revision 1.33 2002/07/06 20:27:26 carl
  950. + generic set handling
  951. Revision 1.32 2002/07/01 18:46:33 peter
  952. * internal linker
  953. * reorganized aasm layer
  954. Revision 1.31 2002/05/18 13:34:25 peter
  955. * readded missing revisions
  956. Revision 1.30 2002/05/16 19:46:52 carl
  957. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  958. + try to fix temp allocation (still in ifdef)
  959. + generic constructor calls
  960. + start of tassembler / tmodulebase class cleanup
  961. Revision 1.28 2002/05/13 19:54:38 peter
  962. * removed n386ld and n386util units
  963. * maybe_save/maybe_restore added instead of the old maybe_push
  964. Revision 1.27 2002/05/12 16:53:17 peter
  965. * moved entry and exitcode to ncgutil and cgobj
  966. * foreach gets extra argument for passing local data to the
  967. iterator function
  968. * -CR checks also class typecasts at runtime by changing them
  969. into as
  970. * fixed compiler to cycle with the -CR option
  971. * fixed stabs with elf writer, finally the global variables can
  972. be watched
  973. * removed a lot of routines from cga unit and replaced them by
  974. calls to cgobj
  975. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  976. u32bit then the other is typecasted also to u32bit without giving
  977. a rangecheck warning/error.
  978. * fixed pascal calling method with reversing also the high tree in
  979. the parast, detected by tcalcst3 test
  980. Revision 1.26 2002/04/25 20:16:40 peter
  981. * moved more routines from cga/n386util
  982. Revision 1.25 2002/04/21 19:02:07 peter
  983. * removed newn and disposen nodes, the code is now directly
  984. inlined from pexpr
  985. * -an option that will write the secondpass nodes to the .s file, this
  986. requires EXTDEBUG define to actually write the info
  987. * fixed various internal errors and crashes due recent code changes
  988. Revision 1.24 2002/04/21 15:37:26 carl
  989. * changeregsize -> rg.makeregsize
  990. Revision 1.23 2002/04/19 15:39:35 peter
  991. * removed some more routines from cga
  992. * moved location_force_reg/mem to ncgutil
  993. * moved arrayconstructnode secondpass to ncgld
  994. Revision 1.22 2002/04/15 19:44:21 peter
  995. * fixed stackcheck that would be called recursively when a stack
  996. error was found
  997. * generic changeregsize(reg,size) for i386 register resizing
  998. * removed some more routines from cga unit
  999. * fixed returnvalue handling
  1000. * fixed default stacksize of linux and go32v2, 8kb was a bit small :-)
  1001. Revision 1.21 2002/04/02 17:11:36 peter
  1002. * tlocation,treference update
  1003. * LOC_CONSTANT added for better constant handling
  1004. * secondadd splitted in multiple routines
  1005. * location_force_reg added for loading a location to a register
  1006. of a specified size
  1007. * secondassignment parses now first the right and then the left node
  1008. (this is compatible with Kylix). This saves a lot of push/pop especially
  1009. with string operations
  1010. * adapted some routines to use the new cg methods
  1011. Revision 1.20 2002/03/31 20:26:39 jonas
  1012. + a_loadfpu_* and a_loadmm_* methods in tcg
  1013. * register allocation is now handled by a class and is mostly processor
  1014. independent (+rgobj.pas and i386/rgcpu.pas)
  1015. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  1016. * some small improvements and fixes to the optimizer
  1017. * some register allocation fixes
  1018. * some fpuvaroffset fixes in the unary minus node
  1019. * push/popusedregisters is now called rg.save/restoreusedregisters and
  1020. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  1021. also better optimizable)
  1022. * fixed and optimized register saving/restoring for new/dispose nodes
  1023. * LOC_FPU locations now also require their "register" field to be set to
  1024. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  1025. - list field removed of the tnode class because it's not used currently
  1026. and can cause hard-to-find bugs
  1027. }