ncgset.pas 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  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. begin
  479. cg.a_cmp_const_reg_label(exprasmlist,opsize,jmp_lt,t^._low,hregister,elselabel);
  480. end;
  481. if t^._low=t^._high then
  482. begin
  483. if t^._low-last=0 then
  484. cg.a_cmp_const_reg_label(exprasmlist, opsize, OC_EQ,0,hregister,t^.statement)
  485. else
  486. begin
  487. gensub(aint(t^._low-last));
  488. cg.a_cmp_const_reg_label(exprasmlist, opsize, OC_EQ,aint(t^._low-last),scratch_reg,t^.statement);
  489. end;
  490. last:=t^._low;
  491. end
  492. else
  493. begin
  494. { it begins with the smallest label, if the value }
  495. { is even smaller then jump immediately to the }
  496. { ELSE-label }
  497. if first then
  498. begin
  499. { have we to ajust the first value ? }
  500. if (t^._low>get_min_value(left.resulttype.def)) then
  501. gensub(aint(t^._low));
  502. end
  503. else
  504. begin
  505. { if there is no unused label between the last and the }
  506. { present label then the lower limit can be checked }
  507. { immediately. else check the range in between: }
  508. gensub(aint(t^._low-last));
  509. cg.a_cmp_const_reg_label(exprasmlist, opsize,jmp_lt,aint(t^._low-last),scratch_reg,elselabel);
  510. end;
  511. gensub(aint(t^._high-t^._low));
  512. cg.a_cmp_const_reg_label(exprasmlist, opsize,jmp_le,aint(t^._high-t^._low),scratch_reg,t^.statement);
  513. last:=t^._high;
  514. end;
  515. first:=false;
  516. if assigned(t^.greater) then
  517. genitem(t^.greater);
  518. end;
  519. begin
  520. { do we need to generate cmps? }
  521. if (with_sign and (min_label<0)) then
  522. genlinearcmplist(hp)
  523. else
  524. begin
  525. last:=0;
  526. first:=true;
  527. scratch_reg:=cg.getintregister(exprasmlist,opsize);
  528. genitem(hp);
  529. cg.ungetregister(exprasmlist,scratch_reg);
  530. cg.a_jmp_always(exprasmlist,elselabel);
  531. end;
  532. end;
  533. procedure tcgcasenode.genlinearcmplist(hp : pcaserecord);
  534. var
  535. first : boolean;
  536. last : TConstExprInt;
  537. procedure genitem(t : pcaserecord);
  538. {$ifndef cpu64bit}
  539. var
  540. l1 : tasmlabel;
  541. {$endif cpu64bit}
  542. begin
  543. if assigned(t^.less) then
  544. genitem(t^.less);
  545. if t^._low=t^._high then
  546. begin
  547. {$ifndef cpu64bit}
  548. if opsize in [OS_S64,OS_64] then
  549. begin
  550. objectlibrary.getlabel(l1);
  551. cg.a_cmp_const_reg_label(exprasmlist, OS_32, OC_NE, aint(hi(int64(t^._low))),hregister2,l1);
  552. cg.a_cmp_const_reg_label(exprasmlist, OS_32, OC_EQ, aint(lo(int64(t^._low))),hregister, t^.statement);
  553. cg.a_label(exprasmlist,l1);
  554. end
  555. else
  556. {$endif cpu64bit}
  557. begin
  558. cg.a_cmp_const_reg_label(exprasmlist, opsize, OC_EQ, aint(t^._low),hregister, t^.statement);
  559. end;
  560. { Reset last here, because we've only checked for one value and need to compare
  561. for the next range both the lower and upper bound }
  562. last:=0;
  563. end
  564. else
  565. begin
  566. { it begins with the smallest label, if the value }
  567. { is even smaller then jump immediately to the }
  568. { ELSE-label }
  569. if first or (t^._low-last>1) then
  570. begin
  571. {$ifndef cpu64bit}
  572. if opsize in [OS_64,OS_S64] then
  573. begin
  574. objectlibrary.getlabel(l1);
  575. cg.a_cmp_const_reg_label(exprasmlist, OS_32, jmp_lt, aint(hi(int64(t^._low))),
  576. hregister2, elselabel);
  577. cg.a_cmp_const_reg_label(exprasmlist, OS_32, jmp_gt, aint(hi(int64(t^._low))),
  578. hregister2, l1);
  579. { the comparisation of the low dword must be always unsigned! }
  580. cg.a_cmp_const_reg_label(exprasmlist, OS_32, OC_B, aint(lo(int64(t^._low))), hregister, elselabel);
  581. cg.a_label(exprasmlist,l1);
  582. end
  583. else
  584. {$endif cpu64bit}
  585. begin
  586. cg.a_cmp_const_reg_label(exprasmlist, opsize, jmp_lt, aint(t^._low), hregister,
  587. elselabel);
  588. end;
  589. end;
  590. {$ifndef cpu64bit}
  591. if opsize in [OS_S64,OS_64] then
  592. begin
  593. objectlibrary.getlabel(l1);
  594. cg.a_cmp_const_reg_label(exprasmlist, OS_32, jmp_lt, aint(hi(int64(t^._high))), hregister2,
  595. t^.statement);
  596. cg.a_cmp_const_reg_label(exprasmlist, OS_32, jmp_gt, aint(hi(int64(t^._high))), hregister2,
  597. l1);
  598. cg.a_cmp_const_reg_label(exprasmlist, OS_32, OC_BE, aint(lo(int64(t^._high))), hregister, t^.statement);
  599. cg.a_label(exprasmlist,l1);
  600. end
  601. else
  602. {$endif cpu64bit}
  603. begin
  604. cg.a_cmp_const_reg_label(exprasmlist, opsize, jmp_le, aint(t^._high), hregister, t^.statement);
  605. end;
  606. last:=t^._high;
  607. end;
  608. first:=false;
  609. if assigned(t^.greater) then
  610. genitem(t^.greater);
  611. end;
  612. begin
  613. last:=0;
  614. first:=true;
  615. genitem(hp);
  616. cg.a_jmp_always(exprasmlist,elselabel);
  617. end;
  618. procedure tcgcasenode.gentreejmp(p : pcaserecord);
  619. var
  620. lesslabel,greaterlabel : tasmlabel;
  621. begin
  622. cg.a_label(exprasmlist,p^._at);
  623. { calculate labels for left and right }
  624. if (p^.less=nil) then
  625. lesslabel:=elselabel
  626. else
  627. lesslabel:=p^.less^._at;
  628. if (p^.greater=nil) then
  629. greaterlabel:=elselabel
  630. else
  631. greaterlabel:=p^.greater^._at;
  632. { calculate labels for left and right }
  633. { no range label: }
  634. if p^._low=p^._high then
  635. begin
  636. if greaterlabel=lesslabel then
  637. begin
  638. cg.a_cmp_const_reg_label(exprasmlist, opsize, OC_NE,p^._low,hregister, lesslabel);
  639. end
  640. else
  641. begin
  642. cg.a_cmp_const_reg_label(exprasmlist,opsize, jmp_lt,p^._low,hregister, lesslabel);
  643. cg.a_cmp_const_reg_label(exprasmlist,opsize, jmp_gt,p^._low,hregister, greaterlabel);
  644. end;
  645. cg.a_jmp_always(exprasmlist,p^.statement);
  646. end
  647. else
  648. begin
  649. cg.a_cmp_const_reg_label(exprasmlist,opsize,jmp_lt,p^._low, hregister, lesslabel);
  650. cg.a_cmp_const_reg_label(exprasmlist,opsize,jmp_gt,p^._high,hregister, greaterlabel);
  651. cg.a_jmp_always(exprasmlist,p^.statement);
  652. end;
  653. if assigned(p^.less) then
  654. gentreejmp(p^.less);
  655. if assigned(p^.greater) then
  656. gentreejmp(p^.greater);
  657. end;
  658. procedure ReLabel(var p:tasmsymbol);
  659. begin
  660. if p.defbind = AB_LOCAL then
  661. begin
  662. if not assigned(p.altsymbol) then
  663. objectlibrary.GenerateAltSymbol(p);
  664. p:=p.altsymbol;
  665. p.increfs;
  666. end;
  667. end;
  668. procedure relabelcaserecord(p : pcaserecord);
  669. begin
  670. Relabel(p^.statement);
  671. Relabel(p^._at);
  672. if assigned(p^.greater) then
  673. relabelcaserecord(p^.greater);
  674. if assigned(p^.less) then
  675. relabelcaserecord(p^.less);
  676. end;
  677. procedure tcgcasenode.pass_2;
  678. var
  679. lv,hv,
  680. max_label: tconstexprint;
  681. labels : aint;
  682. max_linear_list : aint;
  683. otl, ofl: tasmlabel;
  684. isjump : boolean;
  685. max_dist,
  686. dist : aword;
  687. hp : tstatementnode;
  688. relabeling: boolean;
  689. begin
  690. location_reset(location,LOC_VOID,OS_NO);
  691. { Relabel for inlining? }
  692. relabeling := false;
  693. if assigned(nodes) and
  694. (nodes^.statement.getrefs <> 0) then
  695. begin
  696. objectlibrary.CreateUsedAsmSymbolList;
  697. relabelcaserecord(nodes);
  698. relabeling := true;
  699. end;
  700. objectlibrary.getlabel(endlabel);
  701. objectlibrary.getlabel(elselabel);
  702. with_sign:=is_signed(left.resulttype.def);
  703. if with_sign then
  704. begin
  705. jmp_gt:=OC_GT;
  706. jmp_lt:=OC_LT;
  707. jmp_le:=OC_LTE;
  708. end
  709. else
  710. begin
  711. jmp_gt:=OC_A;
  712. jmp_lt:=OC_B;
  713. jmp_le:=OC_BE;
  714. end;
  715. { save current truelabel and falselabel }
  716. isjump:=false;
  717. if left.location.loc=LOC_JUMP then
  718. begin
  719. otl:=truelabel;
  720. objectlibrary.getlabel(truelabel);
  721. ofl:=falselabel;
  722. objectlibrary.getlabel(falselabel);
  723. isjump:=true;
  724. end;
  725. secondpass(left);
  726. { determines the size of the operand }
  727. opsize:=def_cgsize(left.resulttype.def);
  728. { copy the case expression to a register }
  729. location_force_reg(exprasmlist,left.location,opsize,false);
  730. {$ifndef cpu64bit}
  731. if opsize in [OS_S64,OS_64] then
  732. begin
  733. hregister:=left.location.registerlow;
  734. hregister2:=left.location.registerhigh;
  735. end
  736. else
  737. {$endif cpu64bit}
  738. hregister:=left.location.register;
  739. if isjump then
  740. begin
  741. truelabel:=otl;
  742. falselabel:=ofl;
  743. end;
  744. { we need the min_label always to choose between }
  745. { cmps and subs/decs }
  746. min_label:=case_get_min(nodes);
  747. {$ifdef OLDREGVARS}
  748. load_all_regvars(exprasmlist);
  749. {$endif OLDREGVARS}
  750. { now generate the jumps }
  751. {$ifndef cpu64bit}
  752. if opsize in [OS_64,OS_S64] then
  753. genlinearcmplist(nodes)
  754. else
  755. {$endif cpu64bit}
  756. begin
  757. if cs_optimize in aktglobalswitches then
  758. begin
  759. { procedures are empirically passed on }
  760. { consumption can also be calculated }
  761. { but does it pay on the different }
  762. { processors? }
  763. { moreover can the size only be appro- }
  764. { ximated as it is not known if rel8, }
  765. { rel16 or rel32 jumps are used }
  766. max_label:=case_get_max(nodes);
  767. labels:=case_count_labels(nodes);
  768. { can we omit the range check of the jump table ? }
  769. getrange(left.resulttype.def,lv,hv);
  770. jumptable_no_range:=(lv=min_label) and (hv=max_label);
  771. { hack a little bit, because the range can be greater }
  772. { than the positive range of a aint }
  773. if (min_label<0) and (max_label>0) then
  774. begin
  775. if min_label=TConstExprInt(low(aint)) then
  776. dist:=aword(max_label)+aword(low(aint))
  777. else
  778. dist:=aword(max_label)+aword(-min_label)
  779. end
  780. else
  781. dist:=max_label-min_label;
  782. { optimize for size ? }
  783. if cs_littlesize in aktglobalswitches then
  784. begin
  785. if (has_jumptable) and
  786. not((labels<=2) or
  787. ((max_label-min_label)<0) or
  788. ((max_label-min_label)>3*labels)) then
  789. begin
  790. { if the labels less or more a continuum then }
  791. genjumptable(nodes,min_label,max_label);
  792. end
  793. else
  794. begin
  795. { a linear list is always smaller than a jump tree }
  796. genlinearlist(nodes);
  797. end;
  798. end
  799. else
  800. begin
  801. max_dist:=4*aword(labels);
  802. if jumptable_no_range then
  803. max_linear_list:=4
  804. else
  805. max_linear_list:=2;
  806. { allow processor specific values }
  807. optimizevalues(max_linear_list,max_dist);
  808. if (labels<=max_linear_list) then
  809. genlinearlist(nodes)
  810. else
  811. begin
  812. if (has_jumptable) and
  813. (dist<max_dist) then
  814. genjumptable(nodes,min_label,max_label)
  815. else
  816. begin
  817. {
  818. This one expects that the case labels are a
  819. perfectly balanced tree, which is not the case
  820. very often -> generates really bad code (JM)
  821. if labels>16 then
  822. gentreejmp(nodes)
  823. else
  824. }
  825. genlinearlist(nodes);
  826. end;
  827. end;
  828. end;
  829. end
  830. else
  831. { it's always not bad }
  832. genlinearlist(nodes);
  833. end;
  834. cg.ungetregister(exprasmlist,hregister);
  835. { now generate the instructions }
  836. hp:=tstatementnode(right);
  837. while assigned(hp) do
  838. begin
  839. { relabel when inlining }
  840. if relabeling then
  841. begin
  842. if hp.left.nodetype<>labeln then
  843. internalerror(200211261);
  844. Relabel(tlabelnode(hp.left).labelnr);
  845. end;
  846. secondpass(hp.left);
  847. { don't come back to case line }
  848. aktfilepos:=exprasmList.getlasttaifilepos^;
  849. {$ifdef OLDREGVARS}
  850. load_all_regvars(exprasmlist);
  851. {$endif OLDREGVARS}
  852. cg.a_jmp_always(exprasmlist,endlabel);
  853. hp:=tstatementnode(hp.right);
  854. end;
  855. cg.a_label(exprasmlist,elselabel);
  856. { ...and the else block }
  857. if assigned(elseblock) then
  858. begin
  859. secondpass(elseblock);
  860. {$ifdef OLDREGVARS}
  861. load_all_regvars(exprasmlist);
  862. {$endif OLDREGVARS}
  863. end;
  864. cg.a_label(exprasmlist,endlabel);
  865. { Remove relabels for inlining }
  866. if relabeling and
  867. assigned(nodes) then
  868. begin
  869. { restore used symbols }
  870. objectlibrary.UsedAsmSymbolListResetAltSym;
  871. objectlibrary.DestroyUsedAsmSymbolList;
  872. end;
  873. end;
  874. begin
  875. csetelementnode:=tcgsetelementnode;
  876. cinnode:=tcginnode;
  877. ccasenode:=tcgcasenode;
  878. end.
  879. {
  880. $Log$
  881. Revision 1.65 2004-07-22 10:07:09 jonas
  882. * fixed relabeling (nextaltnr was never increased)
  883. * fixed inlining of case statements at the node level
  884. Revision 1.64 2004/07/04 12:38:55 jonas
  885. * fixed regvar bug in tcginnode.pass_2
  886. Revision 1.63 2004/06/20 08:55:29 florian
  887. * logs truncated
  888. Revision 1.62 2004/06/16 20:07:08 florian
  889. * dwarf branch merged
  890. Revision 1.61 2004/05/30 21:18:22 jonas
  891. * some optimizations and associated fixes for better regvar code
  892. Revision 1.60.2.5 2004/05/02 16:49:12 peter
  893. * 64 bit fixes
  894. Revision 1.60.2.4 2004/05/02 14:09:54 peter
  895. * fix case 64bit issues
  896. Revision 1.60.2.3 2004/05/01 16:02:09 peter
  897. * POINTER_SIZE replaced with sizeof(aint)
  898. * aint,aword,tconst*int moved to globtype
  899. }