n386set.pas 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  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. cginfo,cgbase,pass_2,
  39. ncon,
  40. cpubase,
  41. cga,cgobj,tgobj,ncgutil,n386util,regvars,rgobj;
  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. location_force_reg(left.location,OS_32,false);
  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. location_force_reg(right.location,OS_32,false);
  66. end;
  67. { we doesn't modify the left side, we check only the type }
  68. location_copy(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. href : treference;
  87. opsize : topsize;
  88. setparts : array[1..8] of Tsetpart;
  89. i,numparts : byte;
  90. adjustment : longint;
  91. {href,href2 : Treference;}
  92. l,l2 : tasmlabel;
  93. {$ifdef CORRECT_SET_IN_FPC}
  94. AM : tasmop;
  95. {$endif CORRECT_SET_IN_FPC}
  96. function analizeset(Aset:pconstset;is_small:boolean):boolean;
  97. type
  98. byteset=set of byte;
  99. var
  100. compares,maxcompares:word;
  101. i:byte;
  102. begin
  103. analizeset:=false;
  104. ranges:=false;
  105. numparts:=0;
  106. compares:=0;
  107. { Lots of comparisions take a lot of time, so do not allow
  108. too much comparisions. 8 comparisions are, however, still
  109. smalller than emitting the set }
  110. if cs_littlesize in aktglobalswitches then
  111. maxcompares:=8
  112. else
  113. maxcompares:=5;
  114. { when smallset is possible allow only 3 compares the smallset
  115. code is for littlesize also smaller when more compares are used }
  116. if is_small then
  117. maxcompares:=3;
  118. for i:=0 to 255 do
  119. if i in byteset(Aset^) then
  120. begin
  121. if (numparts=0) or (i<>setparts[numparts].stop+1) then
  122. begin
  123. {Set element is a separate element.}
  124. inc(compares);
  125. if compares>maxcompares then
  126. exit;
  127. inc(numparts);
  128. setparts[numparts].range:=false;
  129. setparts[numparts].stop:=i;
  130. end
  131. else
  132. {Set element is part of a range.}
  133. if not setparts[numparts].range then
  134. begin
  135. {Transform an element into a range.}
  136. setparts[numparts].range:=true;
  137. setparts[numparts].start:=setparts[numparts].stop;
  138. setparts[numparts].stop:=i;
  139. ranges := true;
  140. { there's only one compare per range anymore. Only a }
  141. { sub is added, but that's much faster than a }
  142. { cmp/jcc combo so neglect its effect }
  143. { inc(compares);
  144. if compares>maxcompares then
  145. exit; }
  146. end
  147. else
  148. begin
  149. {Extend a range.}
  150. setparts[numparts].stop:=i;
  151. end;
  152. end;
  153. analizeset:=true;
  154. end;
  155. begin
  156. { We check first if we can generate jumps, this can be done
  157. because the resulttype.def is already set in firstpass }
  158. { check if we can use smallset operation using btl which is limited
  159. to 32 bits, the left side may also not contain higher values !! }
  160. use_small:=(tsetdef(right.resulttype.def).settype=smallset) and
  161. ((left.resulttype.def.deftype=orddef) and (torddef(left.resulttype.def).high<=32) or
  162. (left.resulttype.def.deftype=enumdef) and (tenumdef(left.resulttype.def).max<=32));
  163. { Can we generate jumps? Possible for all types of sets }
  164. genjumps:=(right.nodetype=setconstn) and
  165. analizeset(tsetconstnode(right).value_set,use_small);
  166. { calculate both operators }
  167. { the complex one first }
  168. firstcomplex(self);
  169. secondpass(left);
  170. { Only process the right if we are not generating jumps }
  171. if not genjumps then
  172. begin
  173. pushed:=maybe_push(right.registers32,left,false);
  174. secondpass(right);
  175. if pushed then
  176. restore(left,false);
  177. end;
  178. if codegenerror then
  179. exit;
  180. { ofcourse not commutative }
  181. if nf_swaped in flags then
  182. swapleftright;
  183. if genjumps then
  184. begin
  185. { It gives us advantage to check for the set elements
  186. separately instead of using the SET_IN_BYTE procedure.
  187. To do: Build in support for LOC_JUMP }
  188. opsize := def_opsize(left.resulttype.def);
  189. { If register is used, use only lower 8 bits }
  190. if left.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  191. begin
  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:=rg.makeregsize(left.location.register,OS_INT);
  197. cg.a_load_reg_reg(exprasmlist,left.location.size,left.location.register,pleftreg);
  198. if opsize <> S_L then
  199. emit_const_reg(A_AND,S_L,255,pleftreg);
  200. opsize := S_L;
  201. end
  202. else
  203. { otherwise simply use the lower 8 bits (no "and" }
  204. { necessary this way) (JM) }
  205. begin
  206. pleftreg:=rg.makeregsize(left.location.register,OS_8);
  207. opsize := S_B;
  208. end;
  209. end
  210. else
  211. begin
  212. { load the value in a register }
  213. pleftreg := rg.getexplicitregisterint(exprasmlist,R_EDI);
  214. opsize := S_L;
  215. emit_ref_reg(A_MOVZX,S_BL,left.location.reference,pleftreg);
  216. end;
  217. { Get a label to jump to the end }
  218. location_reset(location,LOC_FLAGS,OS_NO);
  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. rg.ungetregister(exprasmlist,pleftreg);
  246. rg.getexplicitregisterint(exprasmlist,R_EDI);
  247. reference_reset_base(href,pleftreg,-setparts[i].start);
  248. emit_ref_reg(A_LEA,S_L,href,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 :
  309. rg.ungetregister(exprasmlist,pleftreg);
  310. else
  311. begin
  312. reference_release(exprasmlist,left.location.reference);
  313. rg.ungetregister(exprasmlist,R_EDI);
  314. end;
  315. end;
  316. end
  317. else
  318. begin
  319. location_reset(location,LOC_FLAGS,OS_NO);
  320. { We will now generated code to check the set itself, no jmps,
  321. handle smallsets separate, because it allows faster checks }
  322. if use_small then
  323. begin
  324. if left.nodetype=ordconstn then
  325. begin
  326. location.resflags:=F_NE;
  327. case right.location.loc of
  328. LOC_REGISTER,
  329. LOC_CREGISTER:
  330. begin
  331. emit_const_reg(A_TEST,S_L,
  332. 1 shl (tordconstnode(left).value and 31),right.location.register);
  333. end;
  334. LOC_REFERENCE,
  335. LOC_CREFERENCE :
  336. begin
  337. emit_const_ref(A_TEST,S_L,1 shl (tordconstnode(left).value and 31),
  338. right.location.reference);
  339. end;
  340. else
  341. internalerror(200203312);
  342. end;
  343. location_release(exprasmlist,right.location);
  344. end
  345. else
  346. begin
  347. case left.location.loc of
  348. LOC_REGISTER,
  349. LOC_CREGISTER:
  350. begin
  351. hr:=rg.makeregsize(left.location.register,OS_INT);
  352. cg.a_load_reg_reg(exprasmlist,left.location.size,left.location.register,hr);
  353. end;
  354. else
  355. begin
  356. { the set element isn't never samller than a byte }
  357. { and because it's a small set we need only 5 bits }
  358. { but 8 bits are easier to load }
  359. rg.getexplicitregisterint(exprasmlist,R_EDI);
  360. emit_ref_reg(A_MOVZX,S_BL,left.location.reference,R_EDI);
  361. hr:=R_EDI;
  362. location_release(exprasmlist,left.location);
  363. end;
  364. end;
  365. case right.location.loc of
  366. LOC_REGISTER,
  367. LOC_CREGISTER :
  368. begin
  369. emit_reg_reg(A_BT,S_L,hr,
  370. right.location.register);
  371. rg.ungetregisterint(exprasmlist,right.location.register);
  372. end;
  373. LOC_CONSTANT :
  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:=rg.getregisterint(exprasmlist);
  378. emit_const_reg(A_MOV,S_L,
  379. right.location.value,hr2);
  380. emit_reg_reg(A_BT,S_L,hr,hr2);
  381. rg.ungetregisterint(exprasmlist,hr2);
  382. end;
  383. LOC_CREFERENCE,
  384. LOC_REFERENCE :
  385. begin
  386. location_release(exprasmlist,right.location);
  387. emit_reg_ref(A_BT,S_L,hr,right.location.reference);
  388. end;
  389. else
  390. internalerror(2002032210);
  391. end;
  392. { simply to indicate EDI is deallocated here too (JM) }
  393. rg.ungetregisterint(exprasmlist,hr);
  394. location.loc:=LOC_FLAGS;
  395. location.resflags:=F_C;
  396. end;
  397. end
  398. else
  399. begin
  400. if right.location.loc=LOC_CONSTANT then
  401. begin
  402. location.resflags:=F_C;
  403. getlabel(l);
  404. getlabel(l2);
  405. { Is this treated in firstpass ?? }
  406. if left.nodetype=ordconstn then
  407. begin
  408. hr:=rg.getregisterint(exprasmlist);
  409. left.location.loc:=LOC_REGISTER;
  410. left.location.register:=hr;
  411. emit_const_reg(A_MOV,S_L,
  412. tordconstnode(left).value,hr);
  413. end;
  414. case left.location.loc of
  415. LOC_REGISTER,
  416. LOC_CREGISTER:
  417. begin
  418. hr:=rg.makeregsize(left.location.register,OS_INT);
  419. cg.a_load_reg_reg(exprasmlist,left.location.size,left.location.register,hr);
  420. emit_const_reg(A_CMP,S_L,31,hr);
  421. emitjmp(C_NA,l);
  422. { reset carry flag }
  423. emit_none(A_CLC,S_NO);
  424. emitjmp(C_NONE,l2);
  425. emitlab(l);
  426. { We have to load the value into a register because
  427. btl does not accept values only refs or regs (PFV) }
  428. hr2:=rg.getregisterint(exprasmlist);
  429. emit_const_reg(A_MOV,S_L,right.location.value,hr2);
  430. emit_reg_reg(A_BT,S_L,hr,hr2);
  431. rg.ungetregisterint(exprasmlist,hr2);
  432. end;
  433. else
  434. begin
  435. {$ifdef CORRECT_SET_IN_FPC}
  436. if m_tp in aktmodeswitches then
  437. begin
  438. {***WARNING only correct if
  439. reference is 32 bits (PM) *****}
  440. emit_const_ref(A_CMP,S_L,31,reference_copy(left.location.reference));
  441. end
  442. else
  443. {$endif CORRECT_SET_IN_FPC}
  444. begin
  445. emit_const_ref(A_CMP,S_B,31,left.location.reference);
  446. end;
  447. emitjmp(C_NA,l);
  448. { reset carry flag }
  449. emit_none(A_CLC,S_NO);
  450. emitjmp(C_NONE,l2);
  451. emitlab(l);
  452. location_release(exprasmlist,left.location);
  453. hr:=rg.getregisterint(exprasmlist);
  454. emit_ref_reg(A_MOV,S_L,left.location.reference,hr);
  455. { We have to load the value into a register because
  456. btl does not accept values only refs or regs (PFV) }
  457. hr2:=rg.getregisterint(exprasmlist);
  458. emit_const_reg(A_MOV,S_L,
  459. right.location.value,hr2);
  460. emit_reg_reg(A_BT,S_L,hr,hr2);
  461. rg.ungetregisterint(exprasmlist,hr2);
  462. end;
  463. end;
  464. emitlab(l2);
  465. end { of right.location.loc=LOC_CONSTANT }
  466. { do search in a normal set which could have >32 elementsm
  467. but also used if the left side contains higher values > 32 }
  468. else if left.nodetype=ordconstn then
  469. begin
  470. location.resflags:=F_NE;
  471. inc(right.location.reference.offset,tordconstnode(left).value shr 3);
  472. emit_const_ref(A_TEST,S_B,1 shl (tordconstnode(left).value and 7),right.location.reference);
  473. location_release(exprasmlist,right.location);
  474. end
  475. else
  476. begin
  477. if not(left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  478. begin
  479. pleftreg := rg.getexplicitregisterint(exprasmlist,R_EDI);
  480. opsize := def2def_opsize(left.resulttype.def,u32bittype.def);
  481. if opsize = S_L then
  482. emit_ref_reg(A_MOV,opsize,left.location.reference,pleftreg)
  483. else
  484. emit_ref_reg(A_MOVZX,opsize,left.location.reference,pleftreg);
  485. location_freetemp(exprasmlist,left.location);
  486. location_release(exprasmlist,left.location);
  487. end
  488. else
  489. begin
  490. pleftreg := rg.makeregsize(left.location.register,OS_INT);
  491. opsize := def2def_opsize(left.resulttype.def,u32bittype.def);
  492. if opsize <> S_L then
  493. begin
  494. { this will change left, even if it's a LOC_CREGISTER, but }
  495. { that doesn't matter: if left is an 8 bit def, then the }
  496. { upper 24 bits are undefined, so we can zero them without }
  497. { any problem (JM) }
  498. cg.a_load_reg_reg(exprasmlist,left.location.size,left.location.register,pleftreg);
  499. end;
  500. end;
  501. emit_reg_ref(A_BT,S_L,pleftreg,right.location.reference);
  502. rg.ungetregister(exprasmlist,pleftreg);
  503. location_release(exprasmlist,right.location);
  504. { tg.ungetiftemp(exprasmlist,right.location.reference) happens below }
  505. location.resflags:=F_C;
  506. end;
  507. end;
  508. end;
  509. location_freetemp(exprasmlist,right.location);
  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. href : treference;
  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. rg.makeregsize(hregister,OS_INT))
  757. else
  758. emit_reg_reg(A_MOVZX,S_WL,hregister,
  759. rg.makeregsize(hregister,OS_INT));
  760. hregister:=rg.makeregsize(hregister,OS_INT);
  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. rg.makeregsize(hregister,OS_INT))
  767. else
  768. emit_reg_reg(A_MOVZX,S_BL,hregister,
  769. rg.makeregsize(hregister,OS_INT));
  770. hregister:=rg.makeregsize(hregister,OS_INT);
  771. end;
  772. reference_reset_symbol(href,table,0);
  773. href.offset:=(-longint(min_))*4;
  774. href.index:=hregister;
  775. href.scalefactor:=4;
  776. emit_ref(A_JMP,S_NO,href);
  777. { !!!!! generate tables
  778. if not(cs_littlesize in aktlocalswitches) then
  779. jumpSegment.concat(Taicpu.Op_const(A_ALIGN,S_NO,4));
  780. }
  781. jumpSegment.concat(Tai_label.Create(table));
  782. last:=min_;
  783. genitem(hp);
  784. { !!!!!!!
  785. if not(cs_littlesize in aktlocalswitches) then
  786. emit_const(A_ALIGN,S_NO,4);
  787. }
  788. end;
  789. var
  790. lv,hv,
  791. max_label: tconstexprint;
  792. labels : longint;
  793. max_linear_list : longint;
  794. otl, ofl: tasmlabel;
  795. isjump : boolean;
  796. {$ifdef Delphi}
  797. dist : cardinal;
  798. {$else Delphi}
  799. dist : dword;
  800. {$endif Delphi}
  801. begin
  802. getlabel(endlabel);
  803. getlabel(elselabel);
  804. if (cs_create_smart in aktmoduleswitches) then
  805. jumpsegment:=procinfo^.aktlocaldata
  806. else
  807. jumpsegment:=datasegment;
  808. with_sign:=is_signed(left.resulttype.def);
  809. if with_sign then
  810. begin
  811. jmp_gt:=C_G;
  812. jmp_le:=C_L;
  813. jmp_lee:=C_LE;
  814. end
  815. else
  816. begin
  817. jmp_gt:=C_A;
  818. jmp_le:=C_B;
  819. jmp_lee:=C_BE;
  820. end;
  821. rg.cleartempgen;
  822. { save current truelabel and falselabel }
  823. isjump:=false;
  824. if left.location.loc=LOC_JUMP then
  825. begin
  826. otl:=truelabel;
  827. getlabel(truelabel);
  828. ofl:=falselabel;
  829. getlabel(falselabel);
  830. isjump:=true;
  831. end;
  832. secondpass(left);
  833. { determines the size of the operand }
  834. opsize:=bytes2Sxx[left.resulttype.def.size];
  835. { copy the case expression to a register }
  836. location_force_reg(left.location,def_cgsize(left.resulttype.def),false);
  837. hregister:=left.location.register;
  838. if isjump then
  839. begin
  840. truelabel:=otl;
  841. falselabel:=ofl;
  842. end;
  843. { we need the min_label always to choose between }
  844. { cmps and subs/decs }
  845. min_label:=case_get_min(nodes);
  846. load_all_regvars(exprasmlist);
  847. { now generate the jumps }
  848. if opsize=S_Q then
  849. genlinearcmplist(nodes)
  850. else
  851. begin
  852. if cs_optimize in aktglobalswitches then
  853. begin
  854. { procedures are empirically passed on }
  855. { consumption can also be calculated }
  856. { but does it pay on the different }
  857. { processors? }
  858. { moreover can the size only be appro- }
  859. { ximated as it is not known if rel8, }
  860. { rel16 or rel32 jumps are used }
  861. max_label:=case_get_max(nodes);
  862. labels:=case_count_labels(nodes);
  863. { can we omit the range check of the jump table ? }
  864. getrange(left.resulttype.def,lv,hv);
  865. jumptable_no_range:=(lv=min_label) and (hv=max_label);
  866. { hack a little bit, because the range can be greater }
  867. { than the positive range of a longint }
  868. if (min_label<0) and (max_label>0) then
  869. begin
  870. {$ifdef Delphi}
  871. if min_label=longint($80000000) then
  872. dist:=Cardinal(max_label)+Cardinal($80000000)
  873. else
  874. dist:=Cardinal(max_label)+Cardinal(-min_label)
  875. {$else Delphi}
  876. if min_label=$80000000 then
  877. dist:=dword(max_label)+dword($80000000)
  878. else
  879. dist:=dword(max_label)+dword(-min_label)
  880. {$endif Delphi}
  881. end
  882. else
  883. dist:=max_label-min_label;
  884. { optimize for size ? }
  885. if cs_littlesize in aktglobalswitches then
  886. begin
  887. if (labels<=2) or
  888. ((max_label-min_label)<0) or
  889. ((max_label-min_label)>3*labels) then
  890. { a linear list is always smaller than a jump tree }
  891. genlinearlist(nodes)
  892. else
  893. { if the labels less or more a continuum then }
  894. genjumptable(nodes,min_label,max_label);
  895. end
  896. else
  897. begin
  898. if jumptable_no_range then
  899. max_linear_list:=4
  900. else
  901. max_linear_list:=2;
  902. { a jump table crashes the pipeline! }
  903. if aktoptprocessor=Class386 then
  904. inc(max_linear_list,3);
  905. if aktoptprocessor=ClassP5 then
  906. inc(max_linear_list,6);
  907. if aktoptprocessor>=ClassP6 then
  908. inc(max_linear_list,9);
  909. if (labels<=max_linear_list) then
  910. genlinearlist(nodes)
  911. else
  912. begin
  913. if (dist>4*cardinal(labels)) then
  914. begin
  915. if labels>16 then
  916. gentreejmp(nodes)
  917. else
  918. genlinearlist(nodes);
  919. end
  920. else
  921. genjumptable(nodes,min_label,max_label);
  922. end;
  923. end;
  924. end
  925. else
  926. { it's always not bad }
  927. genlinearlist(nodes);
  928. end;
  929. rg.ungetregister(exprasmlist,hregister);
  930. { now generate the instructions }
  931. hp:=right;
  932. while assigned(hp) do
  933. begin
  934. rg.cleartempgen;
  935. secondpass(tbinarynode(hp).right);
  936. { don't come back to case line }
  937. aktfilepos:=exprasmList.getlasttaifilepos^;
  938. load_all_regvars(exprasmlist);
  939. emitjmp(C_None,endlabel);
  940. hp:=tbinarynode(hp).left;
  941. end;
  942. emitlab(elselabel);
  943. { ...and the else block }
  944. if assigned(elseblock) then
  945. begin
  946. rg.cleartempgen;
  947. secondpass(elseblock);
  948. load_all_regvars(exprasmlist);
  949. end;
  950. emitlab(endlabel);
  951. end;
  952. begin
  953. csetelementnode:=ti386setelementnode;
  954. cinnode:=ti386innode;
  955. ccasenode:=ti386casenode;
  956. end.
  957. {
  958. $Log$
  959. Revision 1.24 2002-04-21 15:37:26 carl
  960. * changeregsize -> rg.makeregsize
  961. Revision 1.23 2002/04/19 15:39:35 peter
  962. * removed some more routines from cga
  963. * moved location_force_reg/mem to ncgutil
  964. * moved arrayconstructnode secondpass to ncgld
  965. Revision 1.22 2002/04/15 19:44:21 peter
  966. * fixed stackcheck that would be called recursively when a stack
  967. error was found
  968. * generic changeregsize(reg,size) for i386 register resizing
  969. * removed some more routines from cga unit
  970. * fixed returnvalue handling
  971. * fixed default stacksize of linux and go32v2, 8kb was a bit small :-)
  972. Revision 1.21 2002/04/02 17:11:36 peter
  973. * tlocation,treference update
  974. * LOC_CONSTANT added for better constant handling
  975. * secondadd splitted in multiple routines
  976. * location_force_reg added for loading a location to a register
  977. of a specified size
  978. * secondassignment parses now first the right and then the left node
  979. (this is compatible with Kylix). This saves a lot of push/pop especially
  980. with string operations
  981. * adapted some routines to use the new cg methods
  982. Revision 1.20 2002/03/31 20:26:39 jonas
  983. + a_loadfpu_* and a_loadmm_* methods in tcg
  984. * register allocation is now handled by a class and is mostly processor
  985. independent (+rgobj.pas and i386/rgcpu.pas)
  986. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  987. * some small improvements and fixes to the optimizer
  988. * some register allocation fixes
  989. * some fpuvaroffset fixes in the unary minus node
  990. * push/popusedregisters is now called rg.save/restoreusedregisters and
  991. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  992. also better optimizable)
  993. * fixed and optimized register saving/restoring for new/dispose nodes
  994. * LOC_FPU locations now also require their "register" field to be set to
  995. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  996. - list field removed of the tnode class because it's not used currently
  997. and can cause hard-to-find bugs
  998. Revision 1.19 2001/12/31 09:53:15 jonas
  999. * changed remaining "getregister32" calls to "getregisterint"
  1000. Revision 1.18 2001/12/03 21:48:43 peter
  1001. * freemem change to value parameter
  1002. * torddef low/high range changed to int64
  1003. Revision 1.17 2001/09/04 11:38:55 jonas
  1004. + searchsystype() and searchsystype() functions in symtable
  1005. * changed ninl and nadd to use these functions
  1006. * i386 set comparison functions now return their results in al instead
  1007. of in the flags so that they can be sued as compilerprocs
  1008. - removed all processor specific code from n386add.pas that has to do
  1009. with set handling, it's now all done in nadd.pas
  1010. * fixed fpc_set_contains_sets in genset.inc
  1011. * fpc_set_in_byte is now coded inline in n386set.pas and doesn't use a
  1012. helper anymore
  1013. * some small fixes in compproc.inc/set.inc regarding the declaration of
  1014. internal helper types (fpc_small_set and fpc_normal_set)
  1015. Revision 1.16 2001/08/26 13:37:00 florian
  1016. * some cg reorganisation
  1017. * some PPC updates
  1018. Revision 1.15 2001/05/06 17:12:14 jonas
  1019. * fixed an IE10 and another bug with [var1..var2] construct
  1020. Revision 1.14 2001/04/13 01:22:19 peter
  1021. * symtable change to classes
  1022. * range check generation and errors fixed, make cycle DEBUG=1 works
  1023. * memory leaks fixed
  1024. Revision 1.13 2001/04/06 14:09:34 jonas
  1025. * fixed bug in ti386innode.pass_2 code and made it simpler/faster
  1026. Revision 1.12 2001/04/02 21:20:38 peter
  1027. * resulttype rewrite
  1028. Revision 1.11 2001/02/11 12:14:56 jonas
  1029. * simplified and optimized code generated for in-statements
  1030. Revision 1.10 2000/12/25 00:07:33 peter
  1031. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  1032. tlinkedlist objects)
  1033. Revision 1.9 2000/12/18 17:45:32 jonas
  1034. * int64 case fixes
  1035. * explicit longint type casts for constants used in assembler code
  1036. generation s,ice they can be cardinals too (or even int64's in case of
  1037. range check errors)
  1038. Revision 1.8 2000/12/16 15:58:18 jonas
  1039. * removed warnings about possible range check errors
  1040. Revision 1.7 2000/12/05 11:44:34 jonas
  1041. + new integer regvar handling, should be much more efficient
  1042. Revision 1.6 2000/11/29 00:30:49 florian
  1043. * unused units removed from uses clause
  1044. * some changes for widestrings
  1045. Revision 1.5 2000/11/17 14:09:00 jonas
  1046. * fixed webbug 1222 ("merged")
  1047. Revision 1.4 2000/11/13 14:44:36 jonas
  1048. * fixes so no more range errors with improved range checking code
  1049. Revision 1.3 2000/10/31 22:02:57 peter
  1050. * symtable splitted, no real code changes
  1051. Revision 1.2 2000/10/26 15:53:27 jonas
  1052. * fixed web bug1192 (changed an ungetregister32 to ungetregister)
  1053. ("merged" from fixes)
  1054. Revision 1.1 2000/10/15 09:33:32 peter
  1055. * moved n386*.pas to i386/ cpu_target dir
  1056. Revision 1.4 2000/10/14 10:14:49 peter
  1057. * moehrendorf oct 2000 rewrite
  1058. Revision 1.3 2000/09/30 16:08:45 peter
  1059. * more cg11 updates
  1060. Revision 1.2 2000/09/24 20:17:44 florian
  1061. * more conversion work done
  1062. Revision 1.1 2000/09/24 19:38:39 florian
  1063. * initial implementation
  1064. }