ncgset.pas 36 KB

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