ncgset.pas 36 KB

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