ncgset.pas 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  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. end
  310. else
  311. {*****************************************************************}
  312. { NO JUMP TABLE GENERATION }
  313. {*****************************************************************}
  314. begin
  315. { We will now generated code to check the set itself, no jmps,
  316. handle smallsets separate, because it allows faster checks }
  317. if use_small then
  318. begin
  319. {**************************** SMALL SET **********************}
  320. if left.nodetype=ordconstn then
  321. begin
  322. location_force_reg(exprasmlist,right.location,opsize,true);
  323. location.register:=cg.getintregister(exprasmlist,location.size);
  324. { first SHR the register }
  325. cg.a_op_const_reg_reg(exprasmlist,OP_SHR,opsize,tordconstnode(left).value and 31,right.location.register,location.register);
  326. { then extract the lowest bit }
  327. cg.a_op_const_reg(exprasmlist,OP_AND,opsize,1,location.register);
  328. end
  329. else
  330. begin
  331. location_force_reg(exprasmlist,left.location,opsize,false);
  332. location_force_reg(exprasmlist,right.location,opsize,true);
  333. { allocate a register for the result }
  334. location.register:=cg.getintregister(exprasmlist,location.size);
  335. { emit bit test operation }
  336. emit_bit_test_reg_reg(exprasmlist,left.location.size,left.location.register,
  337. right.location.register,location.size,location.register);
  338. end;
  339. end
  340. else
  341. {************************** NOT SMALL SET ********************}
  342. begin
  343. if right.location.loc=LOC_CONSTANT then
  344. begin
  345. { can it actually occur currently? CEC }
  346. { yes: "if bytevar in [1,3,5,7,9,11,13,15]" (JM) }
  347. { note: this code assumes that left in [0..255], which is a valid }
  348. { assumption (other cases will be caught by range checking) (JM) }
  349. { load left in register }
  350. location_force_reg(exprasmlist,left.location,opsize,true);
  351. if left.location.loc = LOC_CREGISTER then
  352. hr := cg.getintregister(exprasmlist,opsize)
  353. else
  354. hr := left.location.register;
  355. { load right in register }
  356. hr2:=cg.getintregister(exprasmlist,opsize);
  357. cg.a_load_const_reg(exprasmlist,opsize,right.location.value,hr2);
  358. { emit bit test operation }
  359. emit_bit_test_reg_reg(exprasmlist,left.location.size,left.location.register,hr2,opsize,hr2);
  360. { if left > 31 then hr := 0 else hr := $ffffffff }
  361. cg.a_op_const_reg_reg(exprasmlist,OP_SUB,opsize,32,left.location.register,hr);
  362. cg.a_op_const_reg(exprasmlist,OP_SAR,opsize,31,hr);
  363. { if left > 31, then result := 0 else result := result of bit test }
  364. cg.a_op_reg_reg(exprasmlist,OP_AND,opsize,hr,hr2);
  365. { allocate a register for the result }
  366. location.register := cg.getintregister(exprasmlist,location.size);
  367. cg.a_load_reg_reg(exprasmlist,opsize,location.size,hr2,location.register);
  368. end { of right.location.loc=LOC_CONSTANT }
  369. { do search in a normal set which could have >32 elementsm
  370. but also used if the left side contains higher values > 32 }
  371. else if left.nodetype=ordconstn then
  372. begin
  373. { use location.register as scratch register here }
  374. if (target_info.endian = endian_little) then
  375. inc(right.location.reference.offset,tordconstnode(left).value shr 3)
  376. else
  377. { adjust for endianess differences }
  378. inc(right.location.reference.offset,(tordconstnode(left).value shr 3) xor 3);
  379. { allocate a register for the result }
  380. location.register := cg.getintregister(exprasmlist,location.size);
  381. cg.a_load_ref_reg(exprasmlist,OS_8,location.size,right.location.reference, location.register);
  382. cg.a_op_const_reg(exprasmlist,OP_SHR,location.size,tordconstnode(left).value and 7,
  383. location.register);
  384. cg.a_op_const_reg(exprasmlist,OP_AND,location.size,1,location.register);
  385. end
  386. else
  387. begin
  388. location_force_reg(exprasmlist,left.location,OS_INT,true);
  389. pleftreg := left.location.register;
  390. location_freetemp(exprasmlist,left.location);
  391. hr := cg.getaddressregister(exprasmlist);
  392. cg.a_op_const_reg_reg(exprasmlist,OP_SHR,OS_INT,5,pleftreg,hr);
  393. cg.a_op_const_reg(exprasmlist,OP_SHL,OS_INT,2,hr);
  394. href := right.location.reference;
  395. if (href.base = NR_NO) then
  396. href.base := hr
  397. else if (right.location.reference.index = NR_NO) then
  398. href.index := hr
  399. else
  400. begin
  401. hr2 := cg.getaddressregister(exprasmlist);
  402. cg.a_loadaddr_ref_reg(exprasmlist,href, hr2);
  403. reference_reset_base(href,hr2,0);
  404. href.index := hr;
  405. end;
  406. { allocate a register for the result }
  407. location.register := cg.getintregister(exprasmlist,opsize);
  408. cg.a_load_ref_reg(exprasmlist,opsize,opsize,href,location.register);
  409. hr := cg.getintregister(exprasmlist,opsize);
  410. cg.a_op_const_reg_reg(exprasmlist,OP_AND,opsize,31,pleftreg,hr);
  411. cg.a_op_reg_reg(exprasmlist,OP_SHR,opsize,hr,location.register);
  412. cg.a_op_const_reg(exprasmlist,OP_AND,opsize,1,location.register);
  413. end;
  414. end;
  415. end;
  416. location_freetemp(exprasmlist,right.location);
  417. end;
  418. {*****************************************************************************
  419. TCGCASENODE
  420. *****************************************************************************}
  421. procedure tcgcasenode.optimizevalues(var max_linear_list:aint;var max_dist:aword);
  422. begin
  423. { no changes by default }
  424. end;
  425. function tcgcasenode.has_jumptable : boolean;
  426. begin
  427. { No jumptable support in the default implementation }
  428. has_jumptable:=false;
  429. end;
  430. procedure tcgcasenode.genjumptable(hp : pcaserecord;min_,max_ : aint);
  431. begin
  432. internalerror(200209161);
  433. end;
  434. procedure tcgcasenode.genlinearlist(hp : pcaserecord);
  435. var
  436. first : boolean;
  437. last : TConstExprInt;
  438. scratch_reg: tregister;
  439. procedure genitem(t : pcaserecord);
  440. procedure gensub(value:aint);
  441. begin
  442. { here, since the sub and cmp are separate we need
  443. to move the result before subtract to a help
  444. register.
  445. }
  446. cg.a_load_reg_reg(exprasmlist, opsize, opsize, hregister, scratch_reg);
  447. cg.a_op_const_reg(exprasmlist, OP_SUB, opsize, value, hregister);
  448. end;
  449. begin
  450. if assigned(t^.less) then
  451. genitem(t^.less);
  452. { need we to test the first value }
  453. if first and (t^._low>get_min_value(left.resulttype.def)) then
  454. cg.a_cmp_const_reg_label(exprasmlist,opsize,jmp_lt,aint(t^._low),hregister,elselabel);
  455. if t^._low=t^._high then
  456. begin
  457. if t^._low-last=0 then
  458. cg.a_cmp_const_reg_label(exprasmlist, opsize, OC_EQ,0,hregister,t^.statement)
  459. else
  460. begin
  461. gensub(aint(t^._low-last));
  462. cg.a_cmp_const_reg_label(exprasmlist, opsize, OC_EQ,aint(t^._low-last),scratch_reg,t^.statement);
  463. end;
  464. last:=t^._low;
  465. end
  466. else
  467. begin
  468. { it begins with the smallest label, if the value }
  469. { is even smaller then jump immediately to the }
  470. { ELSE-label }
  471. if first then
  472. begin
  473. { have we to ajust the first value ? }
  474. if (t^._low>get_min_value(left.resulttype.def)) then
  475. gensub(aint(t^._low));
  476. end
  477. else
  478. begin
  479. { if there is no unused label between the last and the }
  480. { present label then the lower limit can be checked }
  481. { immediately. else check the range in between: }
  482. gensub(aint(t^._low-last));
  483. cg.a_cmp_const_reg_label(exprasmlist, opsize,jmp_lt,aint(t^._low-last),scratch_reg,elselabel);
  484. end;
  485. gensub(aint(t^._high-t^._low));
  486. cg.a_cmp_const_reg_label(exprasmlist, opsize,jmp_le,aint(t^._high-t^._low),scratch_reg,t^.statement);
  487. last:=t^._high;
  488. end;
  489. first:=false;
  490. if assigned(t^.greater) then
  491. genitem(t^.greater);
  492. end;
  493. begin
  494. { do we need to generate cmps? }
  495. if (with_sign and (min_label<0)) then
  496. genlinearcmplist(hp)
  497. else
  498. begin
  499. last:=0;
  500. first:=true;
  501. scratch_reg:=cg.getintregister(exprasmlist,opsize);
  502. genitem(hp);
  503. cg.a_jmp_always(exprasmlist,elselabel);
  504. end;
  505. end;
  506. procedure tcgcasenode.genlinearcmplist(hp : pcaserecord);
  507. var
  508. last : TConstExprInt;
  509. lastwasrange: boolean;
  510. procedure genitem(t : pcaserecord);
  511. {$ifndef cpu64bit}
  512. var
  513. l1 : tasmlabel;
  514. {$endif cpu64bit}
  515. begin
  516. if assigned(t^.less) then
  517. genitem(t^.less);
  518. if t^._low=t^._high then
  519. begin
  520. {$ifndef cpu64bit}
  521. if opsize in [OS_S64,OS_64] then
  522. begin
  523. objectlibrary.getlabel(l1);
  524. cg.a_cmp_const_reg_label(exprasmlist, OS_32, OC_NE, aint(hi(int64(t^._low))),hregister2,l1);
  525. cg.a_cmp_const_reg_label(exprasmlist, OS_32, OC_EQ, aint(lo(int64(t^._low))),hregister, t^.statement);
  526. cg.a_label(exprasmlist,l1);
  527. end
  528. else
  529. {$endif cpu64bit}
  530. begin
  531. cg.a_cmp_const_reg_label(exprasmlist, opsize, OC_EQ, aint(t^._low),hregister, t^.statement);
  532. end;
  533. { Reset last here, because we've only checked for one value and need to compare
  534. for the next range both the lower and upper bound }
  535. lastwasrange := false;
  536. end
  537. else
  538. begin
  539. { it begins with the smallest label, if the value }
  540. { is even smaller then jump immediately to the }
  541. { ELSE-label }
  542. if not lastwasrange or (t^._low-last>1) then
  543. begin
  544. {$ifndef cpu64bit}
  545. if opsize in [OS_64,OS_S64] then
  546. begin
  547. objectlibrary.getlabel(l1);
  548. cg.a_cmp_const_reg_label(exprasmlist, OS_32, jmp_lt, aint(hi(int64(t^._low))),
  549. hregister2, elselabel);
  550. cg.a_cmp_const_reg_label(exprasmlist, OS_32, jmp_gt, aint(hi(int64(t^._low))),
  551. hregister2, l1);
  552. { the comparisation of the low dword must be always unsigned! }
  553. cg.a_cmp_const_reg_label(exprasmlist, OS_32, OC_B, aint(lo(int64(t^._low))), hregister, elselabel);
  554. cg.a_label(exprasmlist,l1);
  555. end
  556. else
  557. {$endif cpu64bit}
  558. begin
  559. cg.a_cmp_const_reg_label(exprasmlist, opsize, jmp_lt, aint(t^._low), hregister,
  560. elselabel);
  561. end;
  562. end;
  563. {$ifndef cpu64bit}
  564. if opsize in [OS_S64,OS_64] then
  565. begin
  566. objectlibrary.getlabel(l1);
  567. cg.a_cmp_const_reg_label(exprasmlist, OS_32, jmp_lt, aint(hi(int64(t^._high))), hregister2,
  568. t^.statement);
  569. cg.a_cmp_const_reg_label(exprasmlist, OS_32, jmp_gt, aint(hi(int64(t^._high))), hregister2,
  570. l1);
  571. cg.a_cmp_const_reg_label(exprasmlist, OS_32, OC_BE, aint(lo(int64(t^._high))), hregister, t^.statement);
  572. cg.a_label(exprasmlist,l1);
  573. end
  574. else
  575. {$endif cpu64bit}
  576. begin
  577. cg.a_cmp_const_reg_label(exprasmlist, opsize, jmp_le, aint(t^._high), hregister, t^.statement);
  578. end;
  579. last:=t^._high;
  580. lastwasrange := true;
  581. end;
  582. if assigned(t^.greater) then
  583. genitem(t^.greater);
  584. end;
  585. begin
  586. last:=0;
  587. lastwasrange:=false;
  588. genitem(hp);
  589. cg.a_jmp_always(exprasmlist,elselabel);
  590. end;
  591. procedure tcgcasenode.gentreejmp(p : pcaserecord);
  592. var
  593. lesslabel,greaterlabel : tasmlabel;
  594. begin
  595. cg.a_label(exprasmlist,p^._at);
  596. { calculate labels for left and right }
  597. if (p^.less=nil) then
  598. lesslabel:=elselabel
  599. else
  600. lesslabel:=p^.less^._at;
  601. if (p^.greater=nil) then
  602. greaterlabel:=elselabel
  603. else
  604. greaterlabel:=p^.greater^._at;
  605. { calculate labels for left and right }
  606. { no range label: }
  607. if p^._low=p^._high then
  608. begin
  609. if greaterlabel=lesslabel then
  610. begin
  611. cg.a_cmp_const_reg_label(exprasmlist, opsize, OC_NE,p^._low,hregister, lesslabel);
  612. end
  613. else
  614. begin
  615. cg.a_cmp_const_reg_label(exprasmlist,opsize, jmp_lt,p^._low,hregister, lesslabel);
  616. cg.a_cmp_const_reg_label(exprasmlist,opsize, jmp_gt,p^._low,hregister, greaterlabel);
  617. end;
  618. cg.a_jmp_always(exprasmlist,p^.statement);
  619. end
  620. else
  621. begin
  622. cg.a_cmp_const_reg_label(exprasmlist,opsize,jmp_lt,p^._low, hregister, lesslabel);
  623. cg.a_cmp_const_reg_label(exprasmlist,opsize,jmp_gt,p^._high,hregister, greaterlabel);
  624. cg.a_jmp_always(exprasmlist,p^.statement);
  625. end;
  626. if assigned(p^.less) then
  627. gentreejmp(p^.less);
  628. if assigned(p^.greater) then
  629. gentreejmp(p^.greater);
  630. end;
  631. procedure ReLabel(var p:tasmsymbol);
  632. begin
  633. if p.defbind = AB_LOCAL then
  634. begin
  635. if not assigned(p.altsymbol) then
  636. objectlibrary.GenerateAltSymbol(p);
  637. p:=p.altsymbol;
  638. p.increfs;
  639. end;
  640. end;
  641. procedure relabelcaserecord(p : pcaserecord);
  642. begin
  643. Relabel(p^.statement);
  644. Relabel(p^._at);
  645. if assigned(p^.greater) then
  646. relabelcaserecord(p^.greater);
  647. if assigned(p^.less) then
  648. relabelcaserecord(p^.less);
  649. end;
  650. procedure tcgcasenode.pass_2;
  651. var
  652. lv,hv,
  653. max_label: tconstexprint;
  654. labels : aint;
  655. max_linear_list : aint;
  656. otl, ofl: tasmlabel;
  657. isjump : boolean;
  658. max_dist,
  659. dist : aword;
  660. hp : tstatementnode;
  661. relabeling: boolean;
  662. begin
  663. location_reset(location,LOC_VOID,OS_NO);
  664. { Relabel for inlining? }
  665. relabeling := false;
  666. if assigned(nodes) and
  667. (nodes^.statement.getrefs <> 0) then
  668. begin
  669. objectlibrary.CreateUsedAsmSymbolList;
  670. relabelcaserecord(nodes);
  671. relabeling := true;
  672. end;
  673. objectlibrary.getlabel(endlabel);
  674. objectlibrary.getlabel(elselabel);
  675. with_sign:=is_signed(left.resulttype.def);
  676. if with_sign then
  677. begin
  678. jmp_gt:=OC_GT;
  679. jmp_lt:=OC_LT;
  680. jmp_le:=OC_LTE;
  681. end
  682. else
  683. begin
  684. jmp_gt:=OC_A;
  685. jmp_lt:=OC_B;
  686. jmp_le:=OC_BE;
  687. end;
  688. { save current truelabel and falselabel }
  689. isjump:=false;
  690. if left.location.loc=LOC_JUMP then
  691. begin
  692. otl:=truelabel;
  693. objectlibrary.getlabel(truelabel);
  694. ofl:=falselabel;
  695. objectlibrary.getlabel(falselabel);
  696. isjump:=true;
  697. end;
  698. secondpass(left);
  699. { determines the size of the operand }
  700. opsize:=def_cgsize(left.resulttype.def);
  701. { copy the case expression to a register }
  702. location_force_reg(exprasmlist,left.location,opsize,false);
  703. {$ifndef cpu64bit}
  704. if opsize in [OS_S64,OS_64] then
  705. begin
  706. hregister:=left.location.registerlow;
  707. hregister2:=left.location.registerhigh;
  708. end
  709. else
  710. {$endif cpu64bit}
  711. hregister:=left.location.register;
  712. if isjump then
  713. begin
  714. truelabel:=otl;
  715. falselabel:=ofl;
  716. end;
  717. { we need the min_label always to choose between }
  718. { cmps and subs/decs }
  719. min_label:=case_get_min(nodes);
  720. {$ifdef OLDREGVARS}
  721. load_all_regvars(exprasmlist);
  722. {$endif OLDREGVARS}
  723. { now generate the jumps }
  724. {$ifndef cpu64bit}
  725. if opsize in [OS_64,OS_S64] then
  726. genlinearcmplist(nodes)
  727. else
  728. {$endif cpu64bit}
  729. begin
  730. if cs_optimize in aktglobalswitches then
  731. begin
  732. { procedures are empirically passed on }
  733. { consumption can also be calculated }
  734. { but does it pay on the different }
  735. { processors? }
  736. { moreover can the size only be appro- }
  737. { ximated as it is not known if rel8, }
  738. { rel16 or rel32 jumps are used }
  739. max_label:=case_get_max(nodes);
  740. labels:=case_count_labels(nodes);
  741. { can we omit the range check of the jump table ? }
  742. getrange(left.resulttype.def,lv,hv);
  743. jumptable_no_range:=(lv=min_label) and (hv=max_label);
  744. { hack a little bit, because the range can be greater }
  745. { than the positive range of a aint }
  746. if (min_label<0) and (max_label>0) then
  747. begin
  748. if min_label=TConstExprInt(low(aint)) then
  749. dist:=aword(max_label)+aword(low(aint))
  750. else
  751. dist:=aword(max_label)+aword(-min_label)
  752. end
  753. else
  754. dist:=max_label-min_label;
  755. { optimize for size ? }
  756. if cs_littlesize in aktglobalswitches then
  757. begin
  758. if has_jumptable and
  759. not((labels<=2) or
  760. ((max_label-min_label)<0) or
  761. ((max_label-min_label)>3*labels)) then
  762. begin
  763. { if the labels less or more a continuum then }
  764. genjumptable(nodes,min_label,max_label);
  765. end
  766. else
  767. begin
  768. { a linear list is always smaller than a jump tree }
  769. genlinearlist(nodes);
  770. end;
  771. end
  772. else
  773. begin
  774. max_dist:=4*aword(labels);
  775. if jumptable_no_range then
  776. max_linear_list:=4
  777. else
  778. max_linear_list:=2;
  779. { allow processor specific values }
  780. optimizevalues(max_linear_list,max_dist);
  781. if (labels<=max_linear_list) then
  782. genlinearlist(nodes)
  783. else
  784. begin
  785. if (has_jumptable) and
  786. (dist<max_dist) then
  787. genjumptable(nodes,min_label,max_label)
  788. else
  789. begin
  790. {
  791. This one expects that the case labels are a
  792. perfectly balanced tree, which is not the case
  793. very often -> generates really bad code (JM)
  794. if labels>16 then
  795. gentreejmp(nodes)
  796. else
  797. }
  798. genlinearlist(nodes);
  799. end;
  800. end;
  801. end;
  802. end
  803. else
  804. { it's always not bad }
  805. genlinearlist(nodes);
  806. end;
  807. { now generate the instructions }
  808. hp:=tstatementnode(right);
  809. while assigned(hp) do
  810. begin
  811. { relabel when inlining }
  812. if relabeling then
  813. begin
  814. if hp.left.nodetype<>labeln then
  815. internalerror(200211261);
  816. Relabel(tlabelnode(hp.left).labelnr);
  817. end;
  818. secondpass(hp.left);
  819. { don't come back to case line }
  820. aktfilepos:=exprasmList.getlasttaifilepos^;
  821. {$ifdef OLDREGVARS}
  822. load_all_regvars(exprasmlist);
  823. {$endif OLDREGVARS}
  824. cg.a_jmp_always(exprasmlist,endlabel);
  825. hp:=tstatementnode(hp.right);
  826. end;
  827. cg.a_label(exprasmlist,elselabel);
  828. { ...and the else block }
  829. if assigned(elseblock) then
  830. begin
  831. secondpass(elseblock);
  832. {$ifdef OLDREGVARS}
  833. load_all_regvars(exprasmlist);
  834. {$endif OLDREGVARS}
  835. end;
  836. cg.a_label(exprasmlist,endlabel);
  837. { Remove relabels for inlining }
  838. if relabeling and
  839. assigned(nodes) then
  840. begin
  841. { restore used symbols }
  842. objectlibrary.UsedAsmSymbolListResetAltSym;
  843. objectlibrary.DestroyUsedAsmSymbolList;
  844. end;
  845. end;
  846. begin
  847. csetelementnode:=tcgsetelementnode;
  848. cinnode:=tcginnode;
  849. ccasenode:=tcgcasenode;
  850. end.
  851. {
  852. $Log$
  853. Revision 1.69 2004-10-30 22:01:11 florian
  854. * jmp table code generation for case statement on sparc
  855. Revision 1.68 2004/09/25 14:23:54 peter
  856. * ungetregister is now only used for cpuregisters, renamed to
  857. ungetcpuregister
  858. * renamed (get|unget)explicitregister(s) to ..cpuregister
  859. * removed location-release/reference_release
  860. Revision 1.67 2004/08/25 11:51:31 jonas
  861. * fixed rare case bug (see tests/test/tb0478.pp)
  862. Revision 1.66 2004/08/16 21:00:15 peter
  863. * range checks fixed
  864. Revision 1.65 2004/07/22 10:07:09 jonas
  865. * fixed relabeling (nextaltnr was never increased)
  866. * fixed inlining of case statements at the node level
  867. Revision 1.64 2004/07/04 12:38:55 jonas
  868. * fixed regvar bug in tcginnode.pass_2
  869. Revision 1.63 2004/06/20 08:55:29 florian
  870. * logs truncated
  871. Revision 1.62 2004/06/16 20:07:08 florian
  872. * dwarf branch merged
  873. Revision 1.61 2004/05/30 21:18:22 jonas
  874. * some optimizations and associated fixes for better regvar code
  875. Revision 1.60.2.5 2004/05/02 16:49:12 peter
  876. * 64 bit fixes
  877. Revision 1.60.2.4 2004/05/02 14:09:54 peter
  878. * fix case 64bit issues
  879. Revision 1.60.2.3 2004/05/01 16:02:09 peter
  880. * POINTER_SIZE replaced with sizeof(aint)
  881. * aint,aword,tconst*int moved to globtype
  882. }