ncgset.pas 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  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. begin
  689. location_reset(location,LOC_VOID,OS_NO);
  690. { Relabel for inlining? }
  691. if inlining_procedure and assigned(nodes) then
  692. begin
  693. objectlibrary.CreateUsedAsmSymbolList;
  694. relabelcaserecord(nodes);
  695. end;
  696. objectlibrary.getlabel(endlabel);
  697. objectlibrary.getlabel(elselabel);
  698. with_sign:=is_signed(left.resulttype.def);
  699. if with_sign then
  700. begin
  701. jmp_gt:=OC_GT;
  702. jmp_lt:=OC_LT;
  703. jmp_le:=OC_LTE;
  704. end
  705. else
  706. begin
  707. jmp_gt:=OC_A;
  708. jmp_lt:=OC_B;
  709. jmp_le:=OC_BE;
  710. end;
  711. { save current truelabel and falselabel }
  712. isjump:=false;
  713. if left.location.loc=LOC_JUMP then
  714. begin
  715. otl:=truelabel;
  716. objectlibrary.getlabel(truelabel);
  717. ofl:=falselabel;
  718. objectlibrary.getlabel(falselabel);
  719. isjump:=true;
  720. end;
  721. secondpass(left);
  722. { determines the size of the operand }
  723. opsize:=def_cgsize(left.resulttype.def);
  724. { copy the case expression to a register }
  725. location_force_reg(exprasmlist,left.location,opsize,false);
  726. {$ifndef cpu64bit}
  727. if opsize in [OS_S64,OS_64] then
  728. begin
  729. hregister:=left.location.registerlow;
  730. hregister2:=left.location.registerhigh;
  731. end
  732. else
  733. {$endif cpu64bit}
  734. hregister:=left.location.register;
  735. if isjump then
  736. begin
  737. truelabel:=otl;
  738. falselabel:=ofl;
  739. end;
  740. { we need the min_label always to choose between }
  741. { cmps and subs/decs }
  742. min_label:=case_get_min(nodes);
  743. {$ifdef OLDREGVARS}
  744. load_all_regvars(exprasmlist);
  745. {$endif OLDREGVARS}
  746. { now generate the jumps }
  747. {$ifndef cpu64bit}
  748. if opsize in [OS_64,OS_S64] then
  749. genlinearcmplist(nodes)
  750. else
  751. {$endif cpu64bit}
  752. begin
  753. if cs_optimize in aktglobalswitches then
  754. begin
  755. { procedures are empirically passed on }
  756. { consumption can also be calculated }
  757. { but does it pay on the different }
  758. { processors? }
  759. { moreover can the size only be appro- }
  760. { ximated as it is not known if rel8, }
  761. { rel16 or rel32 jumps are used }
  762. max_label:=case_get_max(nodes);
  763. labels:=case_count_labels(nodes);
  764. { can we omit the range check of the jump table ? }
  765. getrange(left.resulttype.def,lv,hv);
  766. jumptable_no_range:=(lv=min_label) and (hv=max_label);
  767. { hack a little bit, because the range can be greater }
  768. { than the positive range of a aint }
  769. if (min_label<0) and (max_label>0) then
  770. begin
  771. if min_label=TConstExprInt(low(aint)) then
  772. dist:=aword(max_label)+aword(low(aint))
  773. else
  774. dist:=aword(max_label)+aword(-min_label)
  775. end
  776. else
  777. dist:=max_label-min_label;
  778. { optimize for size ? }
  779. if cs_littlesize in aktglobalswitches then
  780. begin
  781. if (has_jumptable) and
  782. not((labels<=2) or
  783. ((max_label-min_label)<0) or
  784. ((max_label-min_label)>3*labels)) then
  785. begin
  786. { if the labels less or more a continuum then }
  787. genjumptable(nodes,min_label,max_label);
  788. end
  789. else
  790. begin
  791. { a linear list is always smaller than a jump tree }
  792. genlinearlist(nodes);
  793. end;
  794. end
  795. else
  796. begin
  797. max_dist:=4*aword(labels);
  798. if jumptable_no_range then
  799. max_linear_list:=4
  800. else
  801. max_linear_list:=2;
  802. { allow processor specific values }
  803. optimizevalues(max_linear_list,max_dist);
  804. if (labels<=max_linear_list) then
  805. genlinearlist(nodes)
  806. else
  807. begin
  808. if (has_jumptable) and
  809. (dist<max_dist) then
  810. genjumptable(nodes,min_label,max_label)
  811. else
  812. begin
  813. {
  814. This one expects that the case labels are a
  815. perfectly balanced tree, which is not the case
  816. very often -> generates really bad code (JM)
  817. if labels>16 then
  818. gentreejmp(nodes)
  819. else
  820. }
  821. genlinearlist(nodes);
  822. end;
  823. end;
  824. end;
  825. end
  826. else
  827. { it's always not bad }
  828. genlinearlist(nodes);
  829. end;
  830. cg.ungetregister(exprasmlist,hregister);
  831. { now generate the instructions }
  832. hp:=tstatementnode(right);
  833. while assigned(hp) do
  834. begin
  835. { relabel when inlining }
  836. if inlining_procedure then
  837. begin
  838. if hp.left.nodetype<>labeln then
  839. internalerror(200211261);
  840. Relabel(tlabelnode(hp.left).labelnr);
  841. end;
  842. secondpass(hp.left);
  843. { don't come back to case line }
  844. aktfilepos:=exprasmList.getlasttaifilepos^;
  845. {$ifdef OLDREGVARS}
  846. load_all_regvars(exprasmlist);
  847. {$endif OLDREGVARS}
  848. cg.a_jmp_always(exprasmlist,endlabel);
  849. hp:=tstatementnode(hp.right);
  850. end;
  851. cg.a_label(exprasmlist,elselabel);
  852. { ...and the else block }
  853. if assigned(elseblock) then
  854. begin
  855. secondpass(elseblock);
  856. {$ifdef OLDREGVARS}
  857. load_all_regvars(exprasmlist);
  858. {$endif OLDREGVARS}
  859. end;
  860. cg.a_label(exprasmlist,endlabel);
  861. { Remove relabels for inlining }
  862. if inlining_procedure and
  863. assigned(nodes) then
  864. begin
  865. { restore used symbols }
  866. objectlibrary.UsedAsmSymbolListResetAltSym;
  867. objectlibrary.DestroyUsedAsmSymbolList;
  868. end;
  869. end;
  870. begin
  871. csetelementnode:=tcgsetelementnode;
  872. cinnode:=tcginnode;
  873. ccasenode:=tcgcasenode;
  874. end.
  875. {
  876. $Log$
  877. Revision 1.64 2004-07-04 12:38:55 jonas
  878. * fixed regvar bug in tcginnode.pass_2
  879. Revision 1.63 2004/06/20 08:55:29 florian
  880. * logs truncated
  881. Revision 1.62 2004/06/16 20:07:08 florian
  882. * dwarf branch merged
  883. Revision 1.61 2004/05/30 21:18:22 jonas
  884. * some optimizations and associated fixes for better regvar code
  885. Revision 1.60.2.5 2004/05/02 16:49:12 peter
  886. * 64 bit fixes
  887. Revision 1.60.2.4 2004/05/02 14:09:54 peter
  888. * fix case 64bit issues
  889. Revision 1.60.2.3 2004/05/01 16:02:09 peter
  890. * POINTER_SIZE replaced with sizeof(aint)
  891. * aint,aword,tconst*int moved to globtype
  892. }