ncgset.pas 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl and Carl Eric Codere
  4. Generate generic 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 ncgset;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. globtype,globals,
  23. node,nset,cpubase,cgbase,cgobj,aasmbase,aasmtai;
  24. type
  25. tcgsetelementnode = class(tsetelementnode)
  26. procedure pass_2;override;
  27. end;
  28. tcginnode = class(tinnode)
  29. procedure pass_2;override;
  30. protected
  31. {# Routine to test bitnumber in bitnumber register on value
  32. in value register. The __result register should be set
  33. to one if the bit is set, otherwise __result register
  34. should be set to zero.
  35. Should be overriden on processors which have specific
  36. instructions to do bit tests.
  37. }
  38. procedure emit_bit_test_reg_reg(list : taasmoutput;
  39. bitsize: tcgsize; bitnumber,value : tregister;
  40. ressize: tcgsize; res :tregister);virtual;
  41. end;
  42. tcgcasenode = class(tcasenode)
  43. {
  44. Emits the case node statement. Contrary to the intel
  45. 80x86 version, this version does not emit jump tables,
  46. because of portability problems.
  47. }
  48. procedure pass_2;override;
  49. protected
  50. with_sign : boolean;
  51. opsize : tcgsize;
  52. jmp_gt,jmp_lt,jmp_le : topcmp;
  53. { register with case expression }
  54. hregister,hregister2 : tregister;
  55. endlabel,elselabel : tasmlabel;
  56. { true, if we can omit the range check of the jump table }
  57. jumptable_no_range : boolean;
  58. { has the implementation jumptable support }
  59. min_label : tconstexprint;
  60. procedure optimizevalues(var max_linear_list:aint;var max_dist:aword);virtual;
  61. function has_jumptable : boolean;virtual;
  62. procedure genjumptable(hp : pcaserecord;min_,max_ : aint); virtual;
  63. procedure genlinearlist(hp : pcaserecord); virtual;
  64. procedure genlinearcmplist(hp : pcaserecord); virtual;
  65. procedure gentreejmp(p : pcaserecord);
  66. end;
  67. implementation
  68. uses
  69. systems,
  70. verbose,
  71. symconst,symdef,defutil,
  72. paramgr,
  73. pass_2,
  74. nbas,ncon,nflw,
  75. ncgutil,regvars,cpuinfo,
  76. cgutils;
  77. {*****************************************************************************
  78. TCGSETELEMENTNODE
  79. *****************************************************************************}
  80. procedure tcgsetelementnode.pass_2;
  81. begin
  82. { load first value in 32bit register }
  83. secondpass(left);
  84. if left.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  85. location_force_reg(exprasmlist,left.location,OS_32,false);
  86. { also a second value ? }
  87. if assigned(right) then
  88. begin
  89. secondpass(right);
  90. if codegenerror then
  91. exit;
  92. if right.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  93. location_force_reg(exprasmlist,right.location,OS_32,false);
  94. end;
  95. { we doesn't modify the left side, we check only the type }
  96. location_copy(location,left.location);
  97. end;
  98. {*****************************************************************************
  99. *****************************************************************************}
  100. {**********************************************************************}
  101. { Description: Emit operation to do a bit test, where the bitnumber }
  102. { to test is in the bitnumber register. The value to test against is }
  103. { located in the value register. }
  104. { WARNING: Bitnumber register value is DESTROYED! }
  105. { __Result register is set to 1, if the bit is set otherwise, __Result}
  106. { is set to zero. __RESULT register is also used as scratch. }
  107. {**********************************************************************}
  108. procedure tcginnode.emit_bit_test_reg_reg(list : taasmoutput;
  109. bitsize: tcgsize; bitnumber,value : tregister;
  110. ressize: tcgsize; res :tregister);
  111. var
  112. newres: tregister;
  113. begin
  114. { first make sure that the bit number is modulo 32 }
  115. { not necessary, since if it's > 31, we have a range error -> will }
  116. { be caught when range checking is on! (JM) }
  117. { cg.a_op_const_reg(list,OP_AND,31,bitnumber); }
  118. if bitsize<>ressize then
  119. begin
  120. { FIX ME! We're not allowed to modify the value register here! }
  121. { shift value register "bitnumber" bits to the right }
  122. cg.a_op_reg_reg(list,OP_SHR,bitsize,bitnumber,value);
  123. { extract the bit we want }
  124. cg.a_op_const_reg(list,OP_AND,bitsize,1,value);
  125. cg.a_load_reg_reg(list,bitsize,ressize,value,res);
  126. end
  127. else
  128. begin
  129. { rotate value register "bitnumber" bits to the right }
  130. cg.a_op_reg_reg_reg(list,OP_SHR,bitsize,bitnumber,value,res);
  131. { extract the bit we want }
  132. cg.a_op_const_reg(list,OP_AND,bitsize,1,res);
  133. end;
  134. end;
  135. procedure tcginnode.pass_2;
  136. type
  137. Tsetpart=record
  138. range : boolean; {Part is a range.}
  139. start,stop : byte; {Start/stop when range; Stop=element when an element.}
  140. end;
  141. var
  142. l,l3 : tasmlabel;
  143. adjustment : aint;
  144. href : treference;
  145. hr,hr2,
  146. pleftreg : tregister;
  147. setparts : array[1..8] of Tsetpart;
  148. opsize : tcgsize;
  149. genjumps,
  150. use_small : boolean;
  151. i,numparts : byte;
  152. function analizeset(const Aset:Tconstset;is_small:boolean):boolean;
  153. var
  154. compares,maxcompares:word;
  155. i:byte;
  156. begin
  157. analizeset:=false;
  158. numparts:=0;
  159. compares:=0;
  160. { Lots of comparisions take a lot of time, so do not allow
  161. too much comparisions. 8 comparisions are, however, still
  162. smalller than emitting the set }
  163. if cs_littlesize in aktglobalswitches then
  164. maxcompares:=8
  165. else
  166. maxcompares:=5;
  167. { when smallset is possible allow only 3 compares the smallset
  168. code is for littlesize also smaller when more compares are used }
  169. if is_small then
  170. maxcompares:=3;
  171. for i:=0 to 255 do
  172. if i in Aset then
  173. begin
  174. if (numparts=0) or (i<>setparts[numparts].stop+1) then
  175. begin
  176. {Set element is a separate element.}
  177. inc(compares);
  178. if compares>maxcompares then
  179. exit;
  180. inc(numparts);
  181. setparts[numparts].range:=false;
  182. setparts[numparts].stop:=i;
  183. end
  184. else
  185. {Set element is part of a range.}
  186. if not setparts[numparts].range then
  187. begin
  188. {Transform an element into a range.}
  189. setparts[numparts].range:=true;
  190. setparts[numparts].start:=setparts[numparts].stop;
  191. setparts[numparts].stop:=i;
  192. { there's only one compare per range anymore. Only a }
  193. { sub is added, but that's much faster than a }
  194. { cmp/jcc combo so neglect its effect }
  195. { inc(compares);
  196. if compares>maxcompares then
  197. exit; }
  198. end
  199. else
  200. begin
  201. {Extend a range.}
  202. setparts[numparts].stop:=i;
  203. end;
  204. end;
  205. analizeset:=true;
  206. end;
  207. begin
  208. { We check first if we can generate jumps, this can be done
  209. because the resulttype.def is already set in firstpass }
  210. { check if we can use smallset operation using btl which is limited
  211. to 32 bits, the left side may also not contain higher values !! }
  212. use_small:=(tsetdef(right.resulttype.def).settype=smallset) and
  213. ((left.resulttype.def.deftype=orddef) and (torddef(left.resulttype.def).high<=32) or
  214. (left.resulttype.def.deftype=enumdef) and (tenumdef(left.resulttype.def).max<=32));
  215. { Can we generate jumps? Possible for all types of sets }
  216. genjumps:=(right.nodetype=setconstn) and
  217. analizeset(Tsetconstnode(right).value_set^,use_small);
  218. opsize:=OS_32;
  219. { calculate both operators }
  220. { the complex one first }
  221. firstcomplex(self);
  222. secondpass(left);
  223. { Only process the right if we are not generating jumps }
  224. if not genjumps then
  225. secondpass(right);
  226. if codegenerror then
  227. exit;
  228. { ofcourse not commutative }
  229. if nf_swaped in flags then
  230. swapleftright;
  231. { location is always LOC_JUMP }
  232. location_reset(location,LOC_REGISTER,def_cgsize(resulttype.def));
  233. if genjumps then
  234. begin
  235. { allocate a register for the result }
  236. location.register := cg.getintregister(exprasmlist,location.size);
  237. { Get a label to jump to the end }
  238. objectlibrary.getlabel(l);
  239. { clear the register value, indicating result is FALSE }
  240. cg.a_load_const_reg(exprasmlist,location.size,0,location.register);
  241. { If register is used, use only lower 8 bits }
  242. location_force_reg(exprasmlist,left.location,opsize,false);
  243. pleftreg := left.location.register;
  244. { how much have we already substracted from the x in the }
  245. { "x in [y..z]" expression }
  246. adjustment := 0;
  247. hr:=NR_NO;
  248. for i:=1 to numparts do
  249. if setparts[i].range then
  250. { use fact that a <= x <= b <=> aword(x-a) <= aword(b-a) }
  251. begin
  252. { is the range different from all legal values? }
  253. if (setparts[i].stop-setparts[i].start <> 255) then
  254. begin
  255. { yes, is the lower bound <> 0? }
  256. if (setparts[i].start <> 0) then
  257. { we're going to substract from the left register, }
  258. { so in case of a LOC_CREGISTER first move the value }
  259. { to edi (not done before because now we can do the }
  260. { move and substract in one instruction with LEA) }
  261. if (left.location.loc = LOC_CREGISTER) and
  262. (hr<>pleftreg) then
  263. begin
  264. cg.a_op_const_reg(exprasmlist,OP_SUB,opsize,setparts[i].start,pleftreg);
  265. hr:=cg.getintregister(exprasmlist,opsize);
  266. cg.a_load_reg_reg(exprasmlist,opsize,opsize,pleftreg,hr);
  267. pleftreg:=hr;
  268. end
  269. else
  270. begin
  271. { otherwise, the value is already in a register }
  272. { that can be modified }
  273. cg.a_op_const_reg(exprasmlist,OP_SUB,opsize,
  274. setparts[i].start-adjustment,pleftreg)
  275. end;
  276. { new total value substracted from x: }
  277. { adjustment + (setparts[i].start - adjustment) }
  278. adjustment := setparts[i].start;
  279. { check if result < b-a+1 (not "result <= b-a", since }
  280. { we need a carry in case the element is in the range }
  281. { (this will never overflow since we check at the }
  282. { beginning whether stop-start <> 255) }
  283. cg.a_cmp_const_reg_label(exprasmlist, opsize, OC_B,
  284. setparts[i].stop-setparts[i].start+1,pleftreg,l);
  285. end
  286. else
  287. { if setparts[i].start = 0 and setparts[i].stop = 255, }
  288. { it's always true since "in" is only allowed for bytes }
  289. begin
  290. cg.a_jmp_always(exprasmlist,l);
  291. end;
  292. end
  293. else
  294. begin
  295. { Emit code to check if left is an element }
  296. cg.a_cmp_const_reg_label(exprasmlist, opsize, OC_EQ,
  297. setparts[i].stop-adjustment,pleftreg,l);
  298. end;
  299. { To compensate for not doing a second pass }
  300. right.location.reference.symbol:=nil;
  301. objectlibrary.getlabel(l3);
  302. cg.a_jmp_always(exprasmlist,l3);
  303. { Now place the end label if IN success }
  304. cg.a_label(exprasmlist,l);
  305. { result register is 1 }
  306. cg.a_load_const_reg(exprasmlist,location.size,1,location.register);
  307. { in case value is not found }
  308. cg.a_label(exprasmlist,l3);
  309. case left.location.loc of
  310. LOC_CREGISTER :
  311. cg.ungetregister(exprasmlist,pleftreg);
  312. LOC_REGISTER :
  313. cg.ungetregister(exprasmlist,pleftreg);
  314. else
  315. begin
  316. reference_release(exprasmlist,left.location.reference);
  317. cg.ungetregister(exprasmlist,pleftreg);
  318. end;
  319. end;
  320. end
  321. else
  322. {*****************************************************************}
  323. { NO JUMP TABLE GENERATION }
  324. {*****************************************************************}
  325. begin
  326. { We will now generated code to check the set itself, no jmps,
  327. handle smallsets separate, because it allows faster checks }
  328. if use_small then
  329. begin
  330. {**************************** SMALL SET **********************}
  331. if left.nodetype=ordconstn then
  332. begin
  333. location_force_reg(exprasmlist,right.location,opsize,true);
  334. location.register:=cg.getintregister(exprasmlist,location.size);
  335. { first SHR the register }
  336. cg.a_op_const_reg_reg(exprasmlist,OP_SHR,opsize,tordconstnode(left).value and 31,right.location.register,location.register);
  337. { then extract the lowest bit }
  338. cg.a_op_const_reg(exprasmlist,OP_AND,opsize,1,location.register);
  339. end
  340. else
  341. begin
  342. location_force_reg(exprasmlist,left.location,opsize,false);
  343. location_force_reg(exprasmlist,right.location,opsize,true);
  344. { allocate a register for the result }
  345. location.register:=cg.getintregister(exprasmlist,location.size);
  346. { emit bit test operation }
  347. emit_bit_test_reg_reg(exprasmlist,left.location.size,left.location.register,
  348. right.location.register,location.size,location.register);
  349. end;
  350. location_release(exprasmlist,left.location);
  351. location_release(exprasmlist,right.location);
  352. end
  353. else
  354. {************************** NOT SMALL SET ********************}
  355. begin
  356. if right.location.loc=LOC_CONSTANT then
  357. begin
  358. { can it actually occur currently? CEC }
  359. { yes: "if bytevar in [1,3,5,7,9,11,13,15]" (JM) }
  360. { note: this code assumes that left in [0..255], which is a valid }
  361. { assumption (other cases will be caught by range checking) (JM) }
  362. { load left in register }
  363. location_force_reg(exprasmlist,left.location,opsize,true);
  364. if left.location.loc = LOC_CREGISTER then
  365. hr := cg.getintregister(exprasmlist,opsize)
  366. else
  367. hr := left.location.register;
  368. { load right in register }
  369. hr2:=cg.getintregister(exprasmlist,opsize);
  370. cg.a_load_const_reg(exprasmlist,opsize,right.location.value,hr2);
  371. { emit bit test operation }
  372. emit_bit_test_reg_reg(exprasmlist,left.location.size,left.location.register,hr2,opsize,hr2);
  373. { if left > 31 then hr := 0 else hr := $ffffffff }
  374. cg.a_op_const_reg_reg(exprasmlist,OP_SUB,opsize,32,left.location.register,hr);
  375. cg.a_op_const_reg(exprasmlist,OP_SAR,opsize,31,hr);
  376. { free registers }
  377. cg.ungetregister(exprasmlist,hr2);
  378. if (left.location.loc in [LOC_CREGISTER]) then
  379. cg.ungetregister(exprasmlist,hr)
  380. else
  381. cg.ungetregister(exprasmlist,left.location.register);
  382. { if left > 31, then result := 0 else result := result of bit test }
  383. cg.a_op_reg_reg(exprasmlist,OP_AND,opsize,hr,hr2);
  384. { allocate a register for the result }
  385. location.register := cg.getintregister(exprasmlist,location.size);
  386. cg.a_load_reg_reg(exprasmlist,opsize,location.size,hr2,location.register);
  387. end { of right.location.loc=LOC_CONSTANT }
  388. { do search in a normal set which could have >32 elementsm
  389. but also used if the left side contains higher values > 32 }
  390. else if left.nodetype=ordconstn then
  391. begin
  392. { use location.register as scratch register here }
  393. if (target_info.endian = endian_little) then
  394. inc(right.location.reference.offset,tordconstnode(left).value shr 3)
  395. else
  396. { adjust for endianess differences }
  397. inc(right.location.reference.offset,(tordconstnode(left).value shr 3) xor 3);
  398. { allocate a register for the result }
  399. location.register := cg.getintregister(exprasmlist,location.size);
  400. cg.a_load_ref_reg(exprasmlist,OS_8,location.size,right.location.reference, location.register);
  401. location_release(exprasmlist,right.location);
  402. cg.a_op_const_reg(exprasmlist,OP_SHR,location.size,tordconstnode(left).value and 7,
  403. location.register);
  404. cg.a_op_const_reg(exprasmlist,OP_AND,location.size,1,location.register);
  405. end
  406. else
  407. begin
  408. location_force_reg(exprasmlist,left.location,OS_INT,true);
  409. pleftreg := left.location.register;
  410. location_freetemp(exprasmlist,left.location);
  411. hr := cg.getaddressregister(exprasmlist);
  412. cg.a_op_const_reg_reg(exprasmlist,OP_SHR,OS_INT,5,pleftreg,hr);
  413. cg.a_op_const_reg(exprasmlist,OP_SHL,OS_INT,2,hr);
  414. href := right.location.reference;
  415. if (href.base = NR_NO) then
  416. href.base := hr
  417. else if (right.location.reference.index = NR_NO) then
  418. href.index := hr
  419. else
  420. begin
  421. reference_release(exprasmlist,href);
  422. hr2 := cg.getaddressregister(exprasmlist);
  423. cg.a_loadaddr_ref_reg(exprasmlist,href, hr2);
  424. reference_reset_base(href,hr2,0);
  425. href.index := hr;
  426. end;
  427. reference_release(exprasmlist,href);
  428. { allocate a register for the result }
  429. location.register := cg.getintregister(exprasmlist,opsize);
  430. cg.a_load_ref_reg(exprasmlist,opsize,opsize,href,location.register);
  431. cg.ungetregister(exprasmlist,pleftreg);
  432. hr := cg.getintregister(exprasmlist,opsize);
  433. cg.a_op_const_reg_reg(exprasmlist,OP_AND,opsize,31,pleftreg,hr);
  434. cg.a_op_reg_reg(exprasmlist,OP_SHR,opsize,hr,location.register);
  435. cg.ungetregister(exprasmlist,hr);
  436. cg.a_op_const_reg(exprasmlist,OP_AND,opsize,1,location.register);
  437. end;
  438. end;
  439. end;
  440. location_freetemp(exprasmlist,right.location);
  441. end;
  442. {*****************************************************************************
  443. TCGCASENODE
  444. *****************************************************************************}
  445. procedure tcgcasenode.optimizevalues(var max_linear_list:aint;var max_dist:aword);
  446. begin
  447. { no changes by default }
  448. end;
  449. function tcgcasenode.has_jumptable : boolean;
  450. begin
  451. { No jumptable support in the default implementation }
  452. has_jumptable:=false;
  453. end;
  454. procedure tcgcasenode.genjumptable(hp : pcaserecord;min_,max_ : aint);
  455. begin
  456. internalerror(200209161);
  457. end;
  458. procedure tcgcasenode.genlinearlist(hp : pcaserecord);
  459. var
  460. first : boolean;
  461. last : TConstExprInt;
  462. scratch_reg: tregister;
  463. procedure genitem(t : pcaserecord);
  464. procedure gensub(value:aint);
  465. begin
  466. { here, since the sub and cmp are separate we need
  467. to move the result before subtract to a help
  468. register.
  469. }
  470. cg.a_load_reg_reg(exprasmlist, opsize, opsize, hregister, scratch_reg);
  471. cg.a_op_const_reg(exprasmlist, OP_SUB, opsize, value, hregister);
  472. end;
  473. begin
  474. if assigned(t^.less) then
  475. genitem(t^.less);
  476. { need we to test the first value }
  477. if first and (t^._low>get_min_value(left.resulttype.def)) then
  478. cg.a_cmp_const_reg_label(exprasmlist,opsize,jmp_lt,aint(t^._low),hregister,elselabel);
  479. if t^._low=t^._high then
  480. begin
  481. if t^._low-last=0 then
  482. cg.a_cmp_const_reg_label(exprasmlist, opsize, OC_EQ,0,hregister,t^.statement)
  483. else
  484. begin
  485. gensub(aint(t^._low-last));
  486. cg.a_cmp_const_reg_label(exprasmlist, opsize, OC_EQ,aint(t^._low-last),scratch_reg,t^.statement);
  487. end;
  488. last:=t^._low;
  489. end
  490. else
  491. begin
  492. { it begins with the smallest label, if the value }
  493. { is even smaller then jump immediately to the }
  494. { ELSE-label }
  495. if first then
  496. begin
  497. { have we to ajust the first value ? }
  498. if (t^._low>get_min_value(left.resulttype.def)) then
  499. gensub(aint(t^._low));
  500. end
  501. else
  502. begin
  503. { if there is no unused label between the last and the }
  504. { present label then the lower limit can be checked }
  505. { immediately. else check the range in between: }
  506. gensub(aint(t^._low-last));
  507. cg.a_cmp_const_reg_label(exprasmlist, opsize,jmp_lt,aint(t^._low-last),scratch_reg,elselabel);
  508. end;
  509. gensub(aint(t^._high-t^._low));
  510. cg.a_cmp_const_reg_label(exprasmlist, opsize,jmp_le,aint(t^._high-t^._low),scratch_reg,t^.statement);
  511. last:=t^._high;
  512. end;
  513. first:=false;
  514. if assigned(t^.greater) then
  515. genitem(t^.greater);
  516. end;
  517. begin
  518. { do we need to generate cmps? }
  519. if (with_sign and (min_label<0)) then
  520. genlinearcmplist(hp)
  521. else
  522. begin
  523. last:=0;
  524. first:=true;
  525. scratch_reg:=cg.getintregister(exprasmlist,opsize);
  526. genitem(hp);
  527. cg.ungetregister(exprasmlist,scratch_reg);
  528. cg.a_jmp_always(exprasmlist,elselabel);
  529. end;
  530. end;
  531. procedure tcgcasenode.genlinearcmplist(hp : pcaserecord);
  532. var
  533. last : TConstExprInt;
  534. lastwasrange: boolean;
  535. procedure genitem(t : pcaserecord);
  536. {$ifndef cpu64bit}
  537. var
  538. l1 : tasmlabel;
  539. {$endif cpu64bit}
  540. begin
  541. if assigned(t^.less) then
  542. genitem(t^.less);
  543. if t^._low=t^._high then
  544. begin
  545. {$ifndef cpu64bit}
  546. if opsize in [OS_S64,OS_64] then
  547. begin
  548. objectlibrary.getlabel(l1);
  549. cg.a_cmp_const_reg_label(exprasmlist, OS_32, OC_NE, aint(hi(int64(t^._low))),hregister2,l1);
  550. cg.a_cmp_const_reg_label(exprasmlist, OS_32, OC_EQ, aint(lo(int64(t^._low))),hregister, t^.statement);
  551. cg.a_label(exprasmlist,l1);
  552. end
  553. else
  554. {$endif cpu64bit}
  555. begin
  556. cg.a_cmp_const_reg_label(exprasmlist, opsize, OC_EQ, aint(t^._low),hregister, t^.statement);
  557. end;
  558. { Reset last here, because we've only checked for one value and need to compare
  559. for the next range both the lower and upper bound }
  560. lastwasrange := false;
  561. end
  562. else
  563. begin
  564. { it begins with the smallest label, if the value }
  565. { is even smaller then jump immediately to the }
  566. { ELSE-label }
  567. if not lastwasrange or (t^._low-last>1) then
  568. begin
  569. {$ifndef cpu64bit}
  570. if opsize in [OS_64,OS_S64] then
  571. begin
  572. objectlibrary.getlabel(l1);
  573. cg.a_cmp_const_reg_label(exprasmlist, OS_32, jmp_lt, aint(hi(int64(t^._low))),
  574. hregister2, elselabel);
  575. cg.a_cmp_const_reg_label(exprasmlist, OS_32, jmp_gt, aint(hi(int64(t^._low))),
  576. hregister2, l1);
  577. { the comparisation of the low dword must be always unsigned! }
  578. cg.a_cmp_const_reg_label(exprasmlist, OS_32, OC_B, aint(lo(int64(t^._low))), hregister, elselabel);
  579. cg.a_label(exprasmlist,l1);
  580. end
  581. else
  582. {$endif cpu64bit}
  583. begin
  584. cg.a_cmp_const_reg_label(exprasmlist, opsize, jmp_lt, aint(t^._low), hregister,
  585. elselabel);
  586. end;
  587. end;
  588. {$ifndef cpu64bit}
  589. if opsize in [OS_S64,OS_64] then
  590. begin
  591. objectlibrary.getlabel(l1);
  592. cg.a_cmp_const_reg_label(exprasmlist, OS_32, jmp_lt, aint(hi(int64(t^._high))), hregister2,
  593. t^.statement);
  594. cg.a_cmp_const_reg_label(exprasmlist, OS_32, jmp_gt, aint(hi(int64(t^._high))), hregister2,
  595. l1);
  596. cg.a_cmp_const_reg_label(exprasmlist, OS_32, OC_BE, aint(lo(int64(t^._high))), hregister, t^.statement);
  597. cg.a_label(exprasmlist,l1);
  598. end
  599. else
  600. {$endif cpu64bit}
  601. begin
  602. cg.a_cmp_const_reg_label(exprasmlist, opsize, jmp_le, aint(t^._high), hregister, t^.statement);
  603. end;
  604. last:=t^._high;
  605. lastwasrange := true;
  606. end;
  607. if assigned(t^.greater) then
  608. genitem(t^.greater);
  609. end;
  610. begin
  611. last:=0;
  612. lastwasrange:=false;
  613. genitem(hp);
  614. cg.a_jmp_always(exprasmlist,elselabel);
  615. end;
  616. procedure tcgcasenode.gentreejmp(p : pcaserecord);
  617. var
  618. lesslabel,greaterlabel : tasmlabel;
  619. begin
  620. cg.a_label(exprasmlist,p^._at);
  621. { calculate labels for left and right }
  622. if (p^.less=nil) then
  623. lesslabel:=elselabel
  624. else
  625. lesslabel:=p^.less^._at;
  626. if (p^.greater=nil) then
  627. greaterlabel:=elselabel
  628. else
  629. greaterlabel:=p^.greater^._at;
  630. { calculate labels for left and right }
  631. { no range label: }
  632. if p^._low=p^._high then
  633. begin
  634. if greaterlabel=lesslabel then
  635. begin
  636. cg.a_cmp_const_reg_label(exprasmlist, opsize, OC_NE,p^._low,hregister, lesslabel);
  637. end
  638. else
  639. begin
  640. cg.a_cmp_const_reg_label(exprasmlist,opsize, jmp_lt,p^._low,hregister, lesslabel);
  641. cg.a_cmp_const_reg_label(exprasmlist,opsize, jmp_gt,p^._low,hregister, greaterlabel);
  642. end;
  643. cg.a_jmp_always(exprasmlist,p^.statement);
  644. end
  645. else
  646. begin
  647. cg.a_cmp_const_reg_label(exprasmlist,opsize,jmp_lt,p^._low, hregister, lesslabel);
  648. cg.a_cmp_const_reg_label(exprasmlist,opsize,jmp_gt,p^._high,hregister, greaterlabel);
  649. cg.a_jmp_always(exprasmlist,p^.statement);
  650. end;
  651. if assigned(p^.less) then
  652. gentreejmp(p^.less);
  653. if assigned(p^.greater) then
  654. gentreejmp(p^.greater);
  655. end;
  656. procedure ReLabel(var p:tasmsymbol);
  657. begin
  658. if p.defbind = AB_LOCAL then
  659. begin
  660. if not assigned(p.altsymbol) then
  661. objectlibrary.GenerateAltSymbol(p);
  662. p:=p.altsymbol;
  663. p.increfs;
  664. end;
  665. end;
  666. procedure relabelcaserecord(p : pcaserecord);
  667. begin
  668. Relabel(p^.statement);
  669. Relabel(p^._at);
  670. if assigned(p^.greater) then
  671. relabelcaserecord(p^.greater);
  672. if assigned(p^.less) then
  673. relabelcaserecord(p^.less);
  674. end;
  675. procedure tcgcasenode.pass_2;
  676. var
  677. lv,hv,
  678. max_label: tconstexprint;
  679. labels : aint;
  680. max_linear_list : aint;
  681. otl, ofl: tasmlabel;
  682. isjump : boolean;
  683. max_dist,
  684. dist : aword;
  685. hp : tstatementnode;
  686. relabeling: boolean;
  687. begin
  688. location_reset(location,LOC_VOID,OS_NO);
  689. { Relabel for inlining? }
  690. relabeling := false;
  691. if assigned(nodes) and
  692. (nodes^.statement.getrefs <> 0) then
  693. begin
  694. objectlibrary.CreateUsedAsmSymbolList;
  695. relabelcaserecord(nodes);
  696. relabeling := true;
  697. end;
  698. objectlibrary.getlabel(endlabel);
  699. objectlibrary.getlabel(elselabel);
  700. with_sign:=is_signed(left.resulttype.def);
  701. if with_sign then
  702. begin
  703. jmp_gt:=OC_GT;
  704. jmp_lt:=OC_LT;
  705. jmp_le:=OC_LTE;
  706. end
  707. else
  708. begin
  709. jmp_gt:=OC_A;
  710. jmp_lt:=OC_B;
  711. jmp_le:=OC_BE;
  712. end;
  713. { save current truelabel and falselabel }
  714. isjump:=false;
  715. if left.location.loc=LOC_JUMP then
  716. begin
  717. otl:=truelabel;
  718. objectlibrary.getlabel(truelabel);
  719. ofl:=falselabel;
  720. objectlibrary.getlabel(falselabel);
  721. isjump:=true;
  722. end;
  723. secondpass(left);
  724. { determines the size of the operand }
  725. opsize:=def_cgsize(left.resulttype.def);
  726. { copy the case expression to a register }
  727. location_force_reg(exprasmlist,left.location,opsize,false);
  728. {$ifndef cpu64bit}
  729. if opsize in [OS_S64,OS_64] then
  730. begin
  731. hregister:=left.location.registerlow;
  732. hregister2:=left.location.registerhigh;
  733. end
  734. else
  735. {$endif cpu64bit}
  736. hregister:=left.location.register;
  737. if isjump then
  738. begin
  739. truelabel:=otl;
  740. falselabel:=ofl;
  741. end;
  742. { we need the min_label always to choose between }
  743. { cmps and subs/decs }
  744. min_label:=case_get_min(nodes);
  745. {$ifdef OLDREGVARS}
  746. load_all_regvars(exprasmlist);
  747. {$endif OLDREGVARS}
  748. { now generate the jumps }
  749. {$ifndef cpu64bit}
  750. if opsize in [OS_64,OS_S64] then
  751. genlinearcmplist(nodes)
  752. else
  753. {$endif cpu64bit}
  754. begin
  755. if cs_optimize in aktglobalswitches then
  756. begin
  757. { procedures are empirically passed on }
  758. { consumption can also be calculated }
  759. { but does it pay on the different }
  760. { processors? }
  761. { moreover can the size only be appro- }
  762. { ximated as it is not known if rel8, }
  763. { rel16 or rel32 jumps are used }
  764. max_label:=case_get_max(nodes);
  765. labels:=case_count_labels(nodes);
  766. { can we omit the range check of the jump table ? }
  767. getrange(left.resulttype.def,lv,hv);
  768. jumptable_no_range:=(lv=min_label) and (hv=max_label);
  769. { hack a little bit, because the range can be greater }
  770. { than the positive range of a aint }
  771. if (min_label<0) and (max_label>0) then
  772. begin
  773. if min_label=TConstExprInt(low(aint)) then
  774. dist:=aword(max_label)+aword(low(aint))
  775. else
  776. dist:=aword(max_label)+aword(-min_label)
  777. end
  778. else
  779. dist:=max_label-min_label;
  780. { optimize for size ? }
  781. if cs_littlesize in aktglobalswitches then
  782. begin
  783. if (has_jumptable) and
  784. not((labels<=2) or
  785. ((max_label-min_label)<0) or
  786. ((max_label-min_label)>3*labels)) then
  787. begin
  788. { if the labels less or more a continuum then }
  789. genjumptable(nodes,min_label,max_label);
  790. end
  791. else
  792. begin
  793. { a linear list is always smaller than a jump tree }
  794. genlinearlist(nodes);
  795. end;
  796. end
  797. else
  798. begin
  799. max_dist:=4*aword(labels);
  800. if jumptable_no_range then
  801. max_linear_list:=4
  802. else
  803. max_linear_list:=2;
  804. { allow processor specific values }
  805. optimizevalues(max_linear_list,max_dist);
  806. if (labels<=max_linear_list) then
  807. genlinearlist(nodes)
  808. else
  809. begin
  810. if (has_jumptable) and
  811. (dist<max_dist) then
  812. genjumptable(nodes,min_label,max_label)
  813. else
  814. begin
  815. {
  816. This one expects that the case labels are a
  817. perfectly balanced tree, which is not the case
  818. very often -> generates really bad code (JM)
  819. if labels>16 then
  820. gentreejmp(nodes)
  821. else
  822. }
  823. genlinearlist(nodes);
  824. end;
  825. end;
  826. end;
  827. end
  828. else
  829. { it's always not bad }
  830. genlinearlist(nodes);
  831. end;
  832. cg.ungetregister(exprasmlist,hregister);
  833. { now generate the instructions }
  834. hp:=tstatementnode(right);
  835. while assigned(hp) do
  836. begin
  837. { relabel when inlining }
  838. if relabeling then
  839. begin
  840. if hp.left.nodetype<>labeln then
  841. internalerror(200211261);
  842. Relabel(tlabelnode(hp.left).labelnr);
  843. end;
  844. secondpass(hp.left);
  845. { don't come back to case line }
  846. aktfilepos:=exprasmList.getlasttaifilepos^;
  847. {$ifdef OLDREGVARS}
  848. load_all_regvars(exprasmlist);
  849. {$endif OLDREGVARS}
  850. cg.a_jmp_always(exprasmlist,endlabel);
  851. hp:=tstatementnode(hp.right);
  852. end;
  853. cg.a_label(exprasmlist,elselabel);
  854. { ...and the else block }
  855. if assigned(elseblock) then
  856. begin
  857. secondpass(elseblock);
  858. {$ifdef OLDREGVARS}
  859. load_all_regvars(exprasmlist);
  860. {$endif OLDREGVARS}
  861. end;
  862. cg.a_label(exprasmlist,endlabel);
  863. { Remove relabels for inlining }
  864. if relabeling and
  865. assigned(nodes) then
  866. begin
  867. { restore used symbols }
  868. objectlibrary.UsedAsmSymbolListResetAltSym;
  869. objectlibrary.DestroyUsedAsmSymbolList;
  870. end;
  871. end;
  872. begin
  873. csetelementnode:=tcgsetelementnode;
  874. cinnode:=tcginnode;
  875. ccasenode:=tcgcasenode;
  876. end.
  877. {
  878. $Log$
  879. Revision 1.67 2004-08-25 11:51:31 jonas
  880. * fixed rare case bug (see tests/test/tb0478.pp)
  881. Revision 1.66 2004/08/16 21:00:15 peter
  882. * range checks fixed
  883. Revision 1.65 2004/07/22 10:07:09 jonas
  884. * fixed relabeling (nextaltnr was never increased)
  885. * fixed inlining of case statements at the node level
  886. Revision 1.64 2004/07/04 12:38:55 jonas
  887. * fixed regvar bug in tcginnode.pass_2
  888. Revision 1.63 2004/06/20 08:55:29 florian
  889. * logs truncated
  890. Revision 1.62 2004/06/16 20:07:08 florian
  891. * dwarf branch merged
  892. Revision 1.61 2004/05/30 21:18:22 jonas
  893. * some optimizations and associated fixes for better regvar code
  894. Revision 1.60.2.5 2004/05/02 16:49:12 peter
  895. * 64 bit fixes
  896. Revision 1.60.2.4 2004/05/02 14:09:54 peter
  897. * fix case 64bit issues
  898. Revision 1.60.2.3 2004/05/01 16:02:09 peter
  899. * POINTER_SIZE replaced with sizeof(aint)
  900. * aint,aword,tconst*int moved to globtype
  901. }