ncgset.pas 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  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,constexp,symtype,
  22. node,nset,cpubase,cgbase,cgutils,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 }
  33. tcginnode = class(tinnode)
  34. procedure in_smallset(opdef: tdef; setbase: aint); virtual;
  35. function pass_1: tnode;override;
  36. procedure pass_generate_code;override;
  37. protected
  38. function checkgenjumps(out setparts: Tsetparts; out numparts: byte; out use_small: boolean): boolean; virtual;
  39. function analizeset(const Aset:Tconstset;out setparts: Tsetparts; out numparts: byte;is_small:boolean):boolean;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_generate_code;override;
  48. protected
  49. with_sign : boolean;
  50. opsize : tdef;
  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,hlcgobj;
  75. {*****************************************************************************
  76. TCGSETELEMENTNODE
  77. *****************************************************************************}
  78. procedure tcgsetelementnode.pass_generate_code;
  79. begin
  80. { load first value in 32bit register }
  81. secondpass(left);
  82. if left.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  83. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,u32inttype,false);
  84. { also a second value ? }
  85. if assigned(right) then
  86. begin
  87. secondpass(right);
  88. if codegenerror then
  89. exit;
  90. if right.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  91. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,u32inttype,false);
  92. end;
  93. { we doesn't modify the left side, we check only the type }
  94. location_copy(location,left.location);
  95. end;
  96. {*****************************************************************************
  97. *****************************************************************************}
  98. function tcginnode.analizeset(const Aset:Tconstset; out setparts:tsetparts; out numparts: byte; is_small:boolean):boolean;
  99. var
  100. compares,maxcompares:word;
  101. i:byte;
  102. begin
  103. analizeset:=false;
  104. fillchar(setparts,sizeof(setparts),0);
  105. numparts:=0;
  106. compares:=0;
  107. { Lots of comparisions take a lot of time, so do not allow
  108. too much comparisions. 8 comparisions are, however, still
  109. smalller than emitting the set }
  110. if cs_opt_size in current_settings.optimizerswitches then
  111. maxcompares:=8
  112. else
  113. maxcompares:=5;
  114. { when smallset is possible allow only 3 compares the smallset
  115. code is for littlesize also smaller when more compares are used }
  116. if is_small then
  117. maxcompares:=3;
  118. for i:=0 to 255 do
  119. if i in Aset then
  120. begin
  121. if (numparts=0) or (i<>setparts[numparts].stop+1) then
  122. begin
  123. {Set element is a separate element.}
  124. inc(compares);
  125. if compares>maxcompares then
  126. exit;
  127. inc(numparts);
  128. setparts[numparts].range:=false;
  129. setparts[numparts].stop:=i;
  130. end
  131. else
  132. {Set element is part of a range.}
  133. if not setparts[numparts].range then
  134. begin
  135. {Transform an element into a range.}
  136. setparts[numparts].range:=true;
  137. setparts[numparts].start:=setparts[numparts].stop;
  138. setparts[numparts].stop:=i;
  139. { there's only one compare per range anymore. Only a }
  140. { sub is added, but that's much faster than a }
  141. { cmp/jcc combo so neglect its effect }
  142. { inc(compares);
  143. if compares>maxcompares then
  144. exit; }
  145. end
  146. else
  147. begin
  148. {Extend a range.}
  149. setparts[numparts].stop:=i;
  150. end;
  151. end;
  152. analizeset:=true;
  153. end;
  154. procedure tcginnode.in_smallset(opdef: tdef; setbase: aint);
  155. begin
  156. { location is always LOC_REGISTER }
  157. location_reset(location, LOC_REGISTER, def_cgsize(resultdef));
  158. { allocate a register for the result }
  159. location.register := hlcg.getintregister(current_asmdata.CurrAsmList, resultdef);
  160. {**************************** SMALL SET **********************}
  161. if left.location.loc=LOC_CONSTANT then
  162. begin
  163. hlcg.a_bit_test_const_loc_reg(current_asmdata.CurrAsmList,
  164. right.resultdef, resultdef,
  165. left.location.value-setbase, right.location,
  166. location.register);
  167. end
  168. else
  169. begin
  170. hlcg.location_force_reg(current_asmdata.CurrAsmList, left.location,
  171. left.resultdef, opdef, true);
  172. register_maybe_adjust_setbase(current_asmdata.CurrAsmList, left.location,
  173. setbase);
  174. hlcg.a_bit_test_reg_loc_reg(current_asmdata.CurrAsmList, left.resultdef,
  175. right.resultdef, resultdef, left.location.register, right.location,
  176. location.register);
  177. end;
  178. end;
  179. function tcginnode.checkgenjumps(out setparts: Tsetparts; out numparts: byte;out use_small: boolean): boolean;
  180. begin
  181. { check if we can use smallset operation using btl which is limited
  182. to 32 bits, the left side may also not contain higher values !! }
  183. use_small:=is_smallset(right.resultdef) and
  184. not is_signed(left.resultdef) and
  185. ((left.resultdef.typ=orddef) and (torddef(left.resultdef).high<32) or
  186. (left.resultdef.typ=enumdef) and (tenumdef(left.resultdef).max<32));
  187. { Can we generate jumps? Possible for all types of sets }
  188. checkgenjumps:=(right.nodetype=setconstn) and
  189. analizeset(Tsetconstnode(right).value_set^,setparts,numparts,use_small);
  190. end;
  191. function tcginnode.pass_1: tnode;
  192. var
  193. setparts: Tsetparts;
  194. numparts: byte;
  195. use_small: boolean;
  196. begin
  197. result := inherited pass_1;
  198. if not(assigned(result)) and
  199. checkgenjumps(setparts,numparts,use_small) then
  200. expectloc := LOC_JUMP;
  201. end;
  202. procedure tcginnode.pass_generate_code;
  203. var
  204. adjustment,
  205. setbase : aint;
  206. l, l2 : tasmlabel;
  207. otl, ofl : tasmlabel;
  208. hr,
  209. pleftreg : tregister;
  210. setparts : Tsetparts;
  211. opsize : tcgsize;
  212. opdef : tdef;
  213. uopsize : tcgsize;
  214. uopdef : tdef;
  215. orgopsize : tcgsize;
  216. orgopdef : tdef;
  217. genjumps,
  218. use_small,
  219. isjump : boolean;
  220. i,numparts : byte;
  221. needslabel : Boolean;
  222. begin
  223. l2:=nil;
  224. ofl:=nil;
  225. otl:=nil;
  226. { We check first if we can generate jumps, this can be done
  227. because the resultdef is already set in firstpass }
  228. genjumps := checkgenjumps(setparts,numparts,use_small);
  229. orgopsize := def_cgsize(left.resultdef);
  230. orgopdef := left.resultdef;
  231. uopsize := OS_32;
  232. uopdef := u32inttype;
  233. if is_signed(left.resultdef) then
  234. begin
  235. opsize := OS_S32;
  236. opdef := s32inttype;
  237. end
  238. else
  239. begin
  240. opsize := uopsize;
  241. opdef := uopdef;
  242. end;
  243. needslabel := false;
  244. isjump:=false;
  245. if (left.expectloc=LOC_JUMP) then
  246. begin
  247. otl:=current_procinfo.CurrTrueLabel;
  248. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  249. ofl:=current_procinfo.CurrFalseLabel;
  250. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  251. isjump:=true;
  252. end
  253. else if not genjumps then
  254. { calculate both operators }
  255. { the complex one first }
  256. { only if left will not be a LOC_JUMP, to keep complexity in the }
  257. { code generator down. This almost never happens anyway, only in }
  258. { case like "if ((a in someset) in someboolset) then" etc }
  259. { also not in case of genjumps, because then we don't secondpass }
  260. { right at all (so we have to make sure that "right" really is }
  261. { "right" and not "swapped left" in that case) }
  262. firstcomplex(self);
  263. secondpass(left);
  264. if isjump then
  265. begin
  266. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,orgopdef,opdef,true);
  267. left.resultdef:=opdef;
  268. current_procinfo.CurrTrueLabel:=otl;
  269. current_procinfo.CurrFalseLabel:=ofl;
  270. end
  271. else if (left.location.loc=LOC_JUMP) then
  272. internalerror(2007070101);
  273. { Only process the right if we are not generating jumps }
  274. if not genjumps then
  275. secondpass(right);
  276. if codegenerror then
  277. exit;
  278. { ofcourse not commutative }
  279. if nf_swapped in flags then
  280. swapleftright;
  281. setbase:=tsetdef(right.resultdef).setbase;
  282. if genjumps then
  283. begin
  284. { location is always LOC_JUMP }
  285. location_reset(location,LOC_JUMP,OS_NO);
  286. { If register is used, use only lower 8 bits }
  287. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,opdef,false);
  288. pleftreg := left.location.register;
  289. { how much have we already substracted from the x in the }
  290. { "x in [y..z]" expression }
  291. adjustment := 0;
  292. hr:=NR_NO;
  293. for i:=1 to numparts do
  294. if setparts[i].range then
  295. { use fact that a <= x <= b <=> aword(x-a) <= aword(b-a) }
  296. begin
  297. { is the range different from all legal values? }
  298. if (setparts[i].stop-setparts[i].start <> 255) or not (orgopsize = OS_8) then
  299. begin
  300. { yes, is the lower bound <> 0? }
  301. if (setparts[i].start <> 0) then
  302. { we're going to substract from the left register, }
  303. { so in case of a LOC_CREGISTER first move the value }
  304. { to edi (not done before because now we can do the }
  305. { move and substract in one instruction with LEA) }
  306. if (left.location.loc = LOC_CREGISTER) and
  307. (hr<>pleftreg) then
  308. begin
  309. { don't change this back to a_op_const_reg/a_load_reg_reg, since pleftreg must not be modified }
  310. hr:=hlcg.getintregister(current_asmdata.CurrAsmList,opdef);
  311. hlcg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,opdef,setparts[i].start,pleftreg,hr);
  312. pleftreg:=hr;
  313. end
  314. else
  315. begin
  316. { otherwise, the value is already in a register }
  317. { that can be modified }
  318. hlcg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SUB,opdef,
  319. setparts[i].start-adjustment,pleftreg)
  320. end;
  321. { new total value substracted from x: }
  322. { adjustment + (setparts[i].start - adjustment) }
  323. adjustment := setparts[i].start;
  324. { check if result < b-a+1 (not "result <= b-a", since }
  325. { we need a carry in case the element is in the range }
  326. { (this will never overflow since we check at the }
  327. { beginning whether stop-start <> 255) }
  328. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, uopdef, OC_B,
  329. setparts[i].stop-setparts[i].start+1,pleftreg,current_procinfo.CurrTrueLabel);
  330. end
  331. else
  332. { if setparts[i].start = 0 and setparts[i].stop = 255, }
  333. { it's always true since "in" is only allowed for bytes }
  334. begin
  335. hlcg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrTrueLabel);
  336. end;
  337. end
  338. else
  339. begin
  340. { Emit code to check if left is an element }
  341. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, opdef, OC_EQ,
  342. setparts[i].stop-adjustment,pleftreg,current_procinfo.CurrTrueLabel);
  343. end;
  344. { To compensate for not doing a second pass }
  345. right.location.reference.symbol:=nil;
  346. hlcg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  347. end
  348. else
  349. {*****************************************************************}
  350. { NO JUMP TABLE GENERATION }
  351. {*****************************************************************}
  352. begin
  353. { We will now generated code to check the set itself, no jmps,
  354. handle smallsets separate, because it allows faster checks }
  355. if use_small then
  356. begin
  357. in_smallset(opdef, setbase);
  358. end
  359. else
  360. {************************** NOT SMALL SET ********************}
  361. begin
  362. { location is always LOC_REGISTER }
  363. location_reset(location, LOC_REGISTER, uopsize{def_cgsize(resultdef)});
  364. { allocate a register for the result }
  365. location.register := hlcg.getintregister(current_asmdata.CurrAsmList, uopdef);
  366. if right.location.loc=LOC_CONSTANT then
  367. begin
  368. { can it actually occur currently? CEC }
  369. { yes: "if bytevar in [1,3,5,7,9,11,13,15]" (JM) }
  370. { note: this code assumes that left in [0..255], which is a valid }
  371. { assumption (other cases will be caught by range checking) (JM) }
  372. { load left in register }
  373. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,uopdef,true);
  374. register_maybe_adjust_setbase(current_asmdata.CurrAsmList,left.location,setbase);
  375. { emit bit test operation -- warning: do not use
  376. location_force_reg() to force a set into a register, except
  377. to a register of the same size as the set. The reason is
  378. that on big endian systems, this would require moving the
  379. set to the most significant part of the new register,
  380. and location_force_register can't do that (it does not
  381. know the type).
  382. a_bit_test_reg_loc_reg() properly takes into account the
  383. size of the set to adjust the register index to test }
  384. hlcg.a_bit_test_reg_loc_reg(current_asmdata.CurrAsmList,
  385. left.resultdef,right.resultdef,uopdef,
  386. left.location.register,right.location,location.register);
  387. { now zero the result if left > nr_of_bits_in_right_register }
  388. hr := hlcg.getintregister(current_asmdata.CurrAsmList,uopdef);
  389. { if left > tcgsize2size[opsize]*8 then hr := 0 else hr := $ffffffff }
  390. { (left.location.size = location.size at this point) }
  391. hlcg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SUB, uopdef, tcgsize2size[opsize]*8, left.location.register, hr);
  392. hlcg.a_op_const_reg(current_asmdata.CurrAsmList, OP_SAR, uopdef, (tcgsize2size[opsize]*8)-1, hr);
  393. { if left > tcgsize2size[opsize]*8-1, then result := 0 else result := result of bit test }
  394. hlcg.a_op_reg_reg(current_asmdata.CurrAsmList, OP_AND, uopdef, hr, location.register);
  395. end { of right.location.loc=LOC_CONSTANT }
  396. { do search in a normal set which could have >32 elements
  397. but also used if the left side contains higher values > 32 }
  398. else if (left.location.loc=LOC_CONSTANT) then
  399. begin
  400. if (left.location.value < setbase) or (((left.location.value-setbase) shr 3) >= right.resultdef.size) then
  401. {should be caught earlier }
  402. internalerror(2007020402);
  403. hlcg.a_bit_test_const_loc_reg(current_asmdata.CurrAsmList,right.resultdef,uopdef,left.location.value-setbase,
  404. right.location,location.register);
  405. end
  406. else
  407. begin
  408. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,opdef,true);
  409. register_maybe_adjust_setbase(current_asmdata.CurrAsmList,left.location,setbase);
  410. pleftreg := left.location.register;
  411. if (opsize >= OS_S8) or { = if signed }
  412. ((left.resultdef.typ=orddef) and
  413. ((torddef(left.resultdef).low < int64(tsetdef(right.resultdef).setbase)) or
  414. (torddef(left.resultdef).high > int64(tsetdef(right.resultdef).setmax)))) or
  415. ((left.resultdef.typ=enumdef) and
  416. ((tenumdef(left.resultdef).min < aint(tsetdef(right.resultdef).setbase)) or
  417. (tenumdef(left.resultdef).max > aint(tsetdef(right.resultdef).setmax)))) then
  418. begin
  419. current_asmdata.getjumplabel(l);
  420. current_asmdata.getjumplabel(l2);
  421. needslabel := True;
  422. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, left.resultdef, OC_BE, tsetdef(right.resultdef).setmax-tsetdef(right.resultdef).setbase, pleftreg, l);
  423. hlcg.a_load_const_reg(current_asmdata.CurrAsmList, uopdef, 0, location.register);
  424. hlcg.a_jmp_always(current_asmdata.CurrAsmList, l2);
  425. hlcg.a_label(current_asmdata.CurrAsmList, l);
  426. end;
  427. hlcg.a_bit_test_reg_loc_reg(current_asmdata.CurrAsmList,left.resultdef,right.resultdef,uopdef,
  428. pleftreg,right.location,location.register);
  429. if needslabel then
  430. hlcg.a_label(current_asmdata.CurrAsmList, l2);
  431. end;
  432. {$ifndef cpuhighleveltarget}
  433. location.size := def_cgsize(resultdef);
  434. location.register := cg.makeregsize(current_asmdata.CurrAsmList, location.register, location.size);
  435. {$else not cpuhighleveltarget}
  436. hr:=hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  437. hlcg.a_load_reg_reg(current_asmdata.CurrAsmList,uopdef,resultdef,location.register,hr);
  438. location.register:=hr;
  439. location.size := def_cgsize(resultdef);
  440. {$endif not cpuhighleveltarget}
  441. end;
  442. end;
  443. location_freetemp(current_asmdata.CurrAsmList, right.location);
  444. end;
  445. {*****************************************************************************
  446. TCGCASENODE
  447. *****************************************************************************}
  448. function tcgcasenode.blocklabel(id:longint):tasmlabel;
  449. begin
  450. if not assigned(blocks[id]) then
  451. internalerror(200411301);
  452. result:=pcaseblock(blocks[id])^.blocklabel;
  453. end;
  454. procedure tcgcasenode.optimizevalues(var max_linear_list:aint;var max_dist:aword);
  455. begin
  456. { no changes by default }
  457. end;
  458. function tcgcasenode.has_jumptable : boolean;
  459. begin
  460. { No jumptable support in the default implementation }
  461. has_jumptable:=false;
  462. end;
  463. procedure tcgcasenode.genjumptable(hp : pcaselabel;min_,max_ : aint);
  464. begin
  465. internalerror(200209161);
  466. end;
  467. procedure tcgcasenode.genlinearlist(hp : pcaselabel);
  468. var
  469. first : boolean;
  470. last : TConstExprInt;
  471. scratch_reg: tregister;
  472. newsize: tcgsize;
  473. newdef: tdef;
  474. procedure genitem(t : pcaselabel);
  475. procedure gensub(value:tcgint);
  476. begin
  477. { here, since the sub and cmp are separate we need
  478. to move the result before subtract to help
  479. the register allocator
  480. }
  481. hlcg.a_load_reg_reg(current_asmdata.CurrAsmList, opsize, opsize, hregister, scratch_reg);
  482. hlcg.a_op_const_reg(current_asmdata.CurrAsmList, OP_SUB, opsize, value, hregister);
  483. end;
  484. begin
  485. if assigned(t^.less) then
  486. genitem(t^.less);
  487. { do we need to test the first value? }
  488. if first and (t^._low>get_min_value(left.resultdef)) then
  489. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,jmp_lt,tcgint(t^._low.svalue),hregister,elselabel);
  490. if t^._low=t^._high then
  491. begin
  492. if t^._low-last=0 then
  493. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,OC_EQ,0,hregister,blocklabel(t^.blockid))
  494. else
  495. begin
  496. gensub(tcgint(t^._low.svalue-last.svalue));
  497. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,
  498. OC_EQ,tcgint(t^._low.svalue-last.svalue),scratch_reg,blocklabel(t^.blockid));
  499. end;
  500. last:=t^._low;
  501. end
  502. else
  503. begin
  504. { it begins with the smallest label, if the value }
  505. { is even smaller then jump immediately to the }
  506. { ELSE-label }
  507. if first then
  508. begin
  509. { have we to ajust the first value ? }
  510. if (t^._low>get_min_value(left.resultdef)) or (get_min_value(left.resultdef)<>0) then
  511. gensub(tcgint(t^._low.svalue));
  512. end
  513. else
  514. begin
  515. { if there is no unused label between the last and the }
  516. { present label then the lower limit can be checked }
  517. { immediately. else check the range in between: }
  518. gensub(tcgint(t^._low.svalue-last.svalue));
  519. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, opsize,jmp_lt,tcgint(t^._low.svalue-last.svalue),scratch_reg,elselabel);
  520. end;
  521. gensub(tcgint(t^._high.svalue-t^._low.svalue));
  522. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,jmp_le,tcgint(t^._high.svalue-t^._low.svalue),scratch_reg,blocklabel(t^.blockid));
  523. last:=t^._high;
  524. end;
  525. first:=false;
  526. if assigned(t^.greater) then
  527. genitem(t^.greater);
  528. end;
  529. begin
  530. { do we need to generate cmps? }
  531. if (with_sign and (min_label<0)) then
  532. genlinearcmplist(hp)
  533. else
  534. begin
  535. { sign/zero extend the value to a full register before starting to
  536. subtract values, so that on platforms that don't have
  537. subregisters of the same size as the value we don't generate
  538. sign/zero-extensions after every subtraction
  539. make newsize always signed, since we only do this if the size in
  540. bytes of the register is larger than the original opsize, so
  541. the value can always be represented by a larger signed type }
  542. newsize:=tcgsize2signed[reg_cgsize(hregister)];
  543. if tcgsize2size[newsize]>opsize.size then
  544. begin
  545. newdef:=cgsize_orddef(newsize);
  546. scratch_reg:=hlcg.getintregister(current_asmdata.CurrAsmList,newdef);
  547. hlcg.a_load_reg_reg(current_asmdata.CurrAsmList,opsize,newdef,hregister,scratch_reg);
  548. hregister:=scratch_reg;
  549. opsize:=newdef;
  550. end;
  551. last:=0;
  552. first:=true;
  553. scratch_reg:=hlcg.getintregister(current_asmdata.CurrAsmList,opsize);
  554. genitem(hp);
  555. hlcg.a_jmp_always(current_asmdata.CurrAsmList,elselabel);
  556. end;
  557. end;
  558. procedure tcgcasenode.genlinearcmplist(hp : pcaselabel);
  559. var
  560. last : TConstExprInt;
  561. lastwasrange: boolean;
  562. procedure genitem(t : pcaselabel);
  563. {$ifndef cpu64bitalu}
  564. var
  565. l1 : tasmlabel;
  566. {$endif not cpu64bitalu}
  567. begin
  568. if assigned(t^.less) then
  569. genitem(t^.less);
  570. if t^._low=t^._high then
  571. begin
  572. {$if defined(cpu32bitalu)}
  573. if def_cgsize(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, OC_NE, aint(hi(int64(t^._low.svalue))),hregister2,l1);
  577. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_32, OC_EQ, aint(lo(int64(t^._low.svalue))),hregister, blocklabel(t^.blockid));
  578. cg.a_label(current_asmdata.CurrAsmList,l1);
  579. end
  580. else
  581. {$elseif defined(cpu16bitalu)}
  582. if def_cgsize(opsize) in [OS_S64,OS_64] then
  583. begin
  584. current_asmdata.getjumplabel(l1);
  585. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, OC_NE, aint(hi(hi(int64(t^._low.svalue)))),GetNextReg(hregister2),l1);
  586. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, OC_NE, aint(lo(hi(int64(t^._low.svalue)))),hregister2,l1);
  587. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, OC_NE, aint(hi(lo(int64(t^._low.svalue)))),GetNextReg(hregister),l1);
  588. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, OC_EQ, aint(lo(lo(int64(t^._low.svalue)))),hregister, blocklabel(t^.blockid));
  589. cg.a_label(current_asmdata.CurrAsmList,l1);
  590. end
  591. else if def_cgsize(opsize) in [OS_S32,OS_32] then
  592. begin
  593. current_asmdata.getjumplabel(l1);
  594. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, OC_NE, aint(hi(int32(t^._low.svalue))),GetNextReg(hregister),l1);
  595. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, OC_EQ, aint(lo(int32(t^._low.svalue))),hregister, blocklabel(t^.blockid));
  596. cg.a_label(current_asmdata.CurrAsmList,l1);
  597. end
  598. else
  599. {$elseif defined(cpu8bitalu)}
  600. if def_cgsize(opsize) in [OS_S64,OS_64] then
  601. begin
  602. current_asmdata.getjumplabel(l1);
  603. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_8, OC_NE, aint(hi(hi(hi(int64(t^._low.svalue))))),GetNextReg(GetNextReg(GetNextReg(hregister2))),l1);
  604. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_8, OC_NE, aint(lo(hi(hi(int64(t^._low.svalue))))),GetNextReg(GetNextReg(hregister2)),l1);
  605. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_8, OC_NE, aint(hi(lo(hi(int64(t^._low.svalue))))),GetNextReg(hregister2),l1);
  606. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_8, OC_NE, aint(lo(lo(hi(int64(t^._low.svalue))))),hregister2,l1);
  607. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_8, OC_NE, aint(hi(hi(lo(int64(t^._low.svalue))))),GetNextReg(GetNextReg(GetNextReg(hregister))),l1);
  608. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_8, OC_NE, aint(lo(hi(lo(int64(t^._low.svalue))))),GetNextReg(GetNextReg(hregister)),l1);
  609. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_8, OC_NE, aint(hi(lo(lo(int64(t^._low.svalue))))),GetNextReg(hregister),l1);
  610. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_8, OC_EQ, aint(lo(lo(lo(int64(t^._low.svalue))))),hregister,blocklabel(t^.blockid));
  611. cg.a_label(current_asmdata.CurrAsmList,l1);
  612. end
  613. else if def_cgsize(opsize) in [OS_S32,OS_32] then
  614. begin
  615. current_asmdata.getjumplabel(l1);
  616. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_8, OC_NE, aint(hi(hi(int32(t^._low.svalue)))),GetNextReg(GetNextReg(GetNextReg(hregister))),l1);
  617. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_8, OC_NE, aint(lo(hi(int32(t^._low.svalue)))),GetNextReg(GetNextReg(hregister)),l1);
  618. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_8, OC_NE, aint(hi(lo(int32(t^._low.svalue)))),GetNextReg(hregister),l1);
  619. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_8, OC_EQ, aint(lo(lo(int32(t^._low.svalue)))),hregister, blocklabel(t^.blockid));
  620. cg.a_label(current_asmdata.CurrAsmList,l1);
  621. end
  622. else if def_cgsize(opsize) in [OS_S16,OS_16] then
  623. begin
  624. current_asmdata.getjumplabel(l1);
  625. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_8, OC_NE, aint(hi(int16(t^._low.svalue))),GetNextReg(hregister),l1);
  626. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_8, OC_EQ, aint(lo(int16(t^._low.svalue))),hregister, blocklabel(t^.blockid));
  627. cg.a_label(current_asmdata.CurrAsmList,l1);
  628. end
  629. else
  630. {$endif}
  631. begin
  632. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, opsize, OC_EQ, aint(t^._low.svalue),hregister, blocklabel(t^.blockid));
  633. end;
  634. { Reset last here, because we've only checked for one value and need to compare
  635. for the next range both the lower and upper bound }
  636. lastwasrange := false;
  637. end
  638. else
  639. begin
  640. { it begins with the smallest label, if the value }
  641. { is even smaller then jump immediately to the }
  642. { ELSE-label }
  643. if not lastwasrange or (t^._low-last>1) then
  644. begin
  645. {$if defined(cpu32bitalu)}
  646. if def_cgsize(opsize) in [OS_64,OS_S64] then
  647. begin
  648. current_asmdata.getjumplabel(l1);
  649. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_32, jmp_lt, aint(hi(int64(t^._low.svalue))),
  650. hregister2, elselabel);
  651. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_32, jmp_gt, aint(hi(int64(t^._low.svalue))),
  652. hregister2, l1);
  653. { the comparisation of the low dword must be always unsigned! }
  654. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_32, OC_B, aint(lo(int64(t^._low.svalue))), hregister, elselabel);
  655. cg.a_label(current_asmdata.CurrAsmList,l1);
  656. end
  657. else
  658. {$elseif defined(cpu16bitalu)}
  659. if def_cgsize(opsize) in [OS_64,OS_S64] then
  660. begin
  661. current_asmdata.getjumplabel(l1);
  662. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, jmp_lt, aint(hi(hi(int64(t^._low.svalue)))),
  663. GetNextReg(hregister2), elselabel);
  664. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, jmp_gt, aint(hi(hi(int64(t^._low.svalue)))),
  665. GetNextReg(hregister2), l1);
  666. { the comparison of the low words must be always unsigned! }
  667. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, OC_B, aint(lo(hi(int64(t^._low.svalue)))),
  668. hregister2, elselabel);
  669. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, OC_A, aint(lo(hi(int64(t^._low.svalue)))),
  670. hregister2, l1);
  671. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, OC_B, aint(hi(lo(int64(t^._low.svalue)))),
  672. GetNextReg(hregister), elselabel);
  673. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, OC_A, aint(hi(lo(int64(t^._low.svalue)))),
  674. GetNextReg(hregister), l1);
  675. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, OC_B, aint(lo(lo(int64(t^._low.svalue)))), hregister, elselabel);
  676. cg.a_label(current_asmdata.CurrAsmList,l1);
  677. end
  678. else if def_cgsize(opsize) in [OS_32,OS_S32] then
  679. begin
  680. current_asmdata.getjumplabel(l1);
  681. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, jmp_lt, aint(hi(int32(t^._low.svalue))),
  682. GetNextReg(hregister), elselabel);
  683. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, jmp_gt, aint(hi(int32(t^._low.svalue))),
  684. GetNextReg(hregister), l1);
  685. { the comparisation of the low dword must be always unsigned! }
  686. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, OC_B, aint(lo(int32(t^._low.svalue))), hregister, elselabel);
  687. cg.a_label(current_asmdata.CurrAsmList,l1);
  688. end
  689. else
  690. {$elseif defined(cpu8bitalu)}
  691. if def_cgsize(opsize) in [OS_64,OS_S64] then
  692. begin
  693. current_asmdata.getjumplabel(l1);
  694. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,jmp_lt,aint(hi(hi(hi(int64(t^._low.svalue))))),GetNextReg(GetNextReg(GetNextReg(hregister2))),elselabel);
  695. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,jmp_gt,aint(hi(hi(hi(int64(t^._low.svalue))))),GetNextReg(GetNextReg(GetNextReg(hregister2))),l1);
  696. { the comparison of the low words must be always unsigned! }
  697. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_B,aint(lo(hi(hi(int64(t^._low.svalue))))),GetNextReg(GetNextReg(hregister2)),elselabel);
  698. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_A,aint(lo(hi(hi(int64(t^._low.svalue))))),GetNextReg(GetNextReg(hregister2)),l1);
  699. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_B,aint(hi(lo(hi(int64(t^._low.svalue))))),GetNextReg(hregister2),elselabel);
  700. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_A,aint(hi(lo(hi(int64(t^._low.svalue))))),GetNextReg(hregister2),l1);
  701. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_B,aint(lo(lo(hi(int64(t^._low.svalue))))),hregister2,elselabel);
  702. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_A,aint(lo(lo(hi(int64(t^._low.svalue))))),hregister2,l1);
  703. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_B,aint(hi(hi(lo(int64(t^._low.svalue))))),GetNextReg(GetNextReg(GetNextReg(hregister))),elselabel);
  704. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_A,aint(hi(hi(lo(int64(t^._low.svalue))))),GetNextReg(GetNextReg(GetNextReg(hregister))),l1);
  705. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_B,aint(lo(hi(lo(int64(t^._low.svalue))))),GetNextReg(GetNextReg(hregister)),elselabel);
  706. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_A,aint(lo(hi(lo(int64(t^._low.svalue))))),GetNextReg(GetNextReg(hregister)),l1);
  707. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_B,aint(hi(lo(lo(int64(t^._low.svalue))))),GetNextReg(hregister),elselabel);
  708. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_A,aint(hi(lo(lo(int64(t^._low.svalue))))),GetNextReg(hregister),l1);
  709. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_B,aint(lo(lo(lo(int64(t^._low.svalue))))),hregister,elselabel);
  710. cg.a_label(current_asmdata.CurrAsmList,l1);
  711. end
  712. else if def_cgsize(opsize) in [OS_32,OS_S32] then
  713. begin
  714. current_asmdata.getjumplabel(l1);
  715. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,jmp_lt,aint(hi(hi(int32(t^._low.svalue)))),GetNextReg(GetNextReg(GetNextReg(hregister))),elselabel);
  716. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_8,jmp_gt,aint(hi(hi(int32(t^._low.svalue)))),GetNextReg(GetNextReg(GetNextReg(hregister))),l1);
  717. { the comparison of the low words must be always unsigned! }
  718. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_B,aint(lo(hi(int32(t^._low.svalue)))),GetNextReg(GetNextReg(hregister)),elselabel);
  719. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_A,aint(lo(hi(int32(t^._low.svalue)))),GetNextReg(GetNextReg(hregister)),l1);
  720. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_B,aint(hi(lo(int32(t^._low.svalue)))),GetNextReg(hregister),elselabel);
  721. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_A,aint(hi(lo(int32(t^._low.svalue)))),GetNextReg(hregister),l1);
  722. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_B,aint(lo(lo(int32(t^._low.svalue)))),hregister,elselabel);
  723. cg.a_label(current_asmdata.CurrAsmList,l1);
  724. end
  725. else if def_cgsize(opsize) in [OS_16,OS_S16] then
  726. begin
  727. current_asmdata.getjumplabel(l1);
  728. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,jmp_lt,aint(hi(int16(t^._low.svalue))),GetNextReg(hregister),elselabel);
  729. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,jmp_gt,aint(hi(int16(t^._low.svalue))),GetNextReg(hregister),l1);
  730. { the comparisation of the low dword must be always unsigned! }
  731. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_B,aint(lo(int16(t^._low.svalue))),hregister,elselabel);
  732. cg.a_label(current_asmdata.CurrAsmList,l1);
  733. end
  734. else
  735. {$endif}
  736. begin
  737. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, opsize, jmp_lt, aint(t^._low.svalue), hregister,
  738. elselabel);
  739. end;
  740. end;
  741. {$if defined(cpu32bitalu)}
  742. if def_cgsize(opsize) in [OS_S64,OS_64] then
  743. begin
  744. current_asmdata.getjumplabel(l1);
  745. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_32, jmp_lt, aint(hi(int64(t^._high.svalue))), hregister2,
  746. blocklabel(t^.blockid));
  747. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_32, jmp_gt, aint(hi(int64(t^._high.svalue))), hregister2,
  748. l1);
  749. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_32, OC_BE, aint(lo(int64(t^._high.svalue))), hregister, blocklabel(t^.blockid));
  750. cg.a_label(current_asmdata.CurrAsmList,l1);
  751. end
  752. else
  753. {$elseif defined(cpu16bitalu)}
  754. if def_cgsize(opsize) in [OS_S64,OS_64] then
  755. begin
  756. current_asmdata.getjumplabel(l1);
  757. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, jmp_lt, aint(hi(hi(int64(t^._high.svalue)))), GetNextReg(hregister2),
  758. blocklabel(t^.blockid));
  759. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, jmp_gt, aint(hi(hi(int64(t^._high.svalue)))), GetNextReg(hregister2),
  760. l1);
  761. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, OC_B, aint(lo(hi(int64(t^._high.svalue)))), hregister2,
  762. blocklabel(t^.blockid));
  763. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, OC_A, aint(lo(hi(int64(t^._high.svalue)))), hregister2,
  764. l1);
  765. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, OC_B, aint(hi(lo(int64(t^._high.svalue)))), GetNextReg(hregister),
  766. blocklabel(t^.blockid));
  767. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, OC_A, aint(hi(lo(int64(t^._high.svalue)))), GetNextReg(hregister),
  768. l1);
  769. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, OC_BE, aint(lo(lo(int64(t^._high.svalue)))), hregister, blocklabel(t^.blockid));
  770. cg.a_label(current_asmdata.CurrAsmList,l1);
  771. end
  772. else if def_cgsize(opsize) in [OS_S32,OS_32] then
  773. begin
  774. current_asmdata.getjumplabel(l1);
  775. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, jmp_lt, aint(hi(int32(t^._high.svalue))), GetNextReg(hregister),
  776. blocklabel(t^.blockid));
  777. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, jmp_gt, aint(hi(int32(t^._high.svalue))), GetNextReg(hregister),
  778. l1);
  779. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, OC_BE, aint(lo(int32(t^._high.svalue))), hregister, blocklabel(t^.blockid));
  780. cg.a_label(current_asmdata.CurrAsmList,l1);
  781. end
  782. else
  783. {$elseif defined(cpu8bitalu)}
  784. if def_cgsize(opsize) in [OS_S64,OS_64] then
  785. begin
  786. current_asmdata.getjumplabel(l1);
  787. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,jmp_lt,aint(hi(hi(hi(int64(t^._high.svalue))))),GetNextReg(GetNextReg(GetNextReg(hregister2))),blocklabel(t^.blockid));
  788. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,jmp_gt,aint(hi(hi(hi(int64(t^._high.svalue))))),GetNextReg(GetNextReg(GetNextReg(hregister2))),l1);
  789. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_B,aint(lo(hi(hi(int64(t^._high.svalue))))),GetNextReg(GetNextReg(hregister2)),blocklabel(t^.blockid));
  790. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_A,aint(lo(hi(hi(int64(t^._high.svalue))))),GetNextReg(GetNextReg(hregister2)),l1);
  791. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_B,aint(hi(lo(hi(int64(t^._high.svalue))))),GetNextReg(hregister2),blocklabel(t^.blockid));
  792. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_A,aint(hi(lo(hi(int64(t^._high.svalue))))),GetNextReg(hregister2),l1);
  793. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_B,aint(lo(lo(hi(int64(t^._high.svalue))))),hregister2,blocklabel(t^.blockid));
  794. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_A,aint(lo(lo(hi(int64(t^._high.svalue))))),hregister2,l1);
  795. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_B,aint(hi(hi(lo(int64(t^._high.svalue))))),GetNextReg(GetNextReg(GetNextReg(hregister))),blocklabel(t^.blockid));
  796. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_A,aint(hi(hi(lo(int64(t^._high.svalue))))),GetNextReg(GetNextReg(GetNextReg(hregister))),l1);
  797. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_B,aint(lo(hi(lo(int64(t^._high.svalue))))),GetNextReg(GetNextReg(hregister)),blocklabel(t^.blockid));
  798. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_A,aint(lo(hi(lo(int64(t^._high.svalue))))),GetNextReg(GetNextReg(hregister)),l1);
  799. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_B,aint(hi(lo(lo(int64(t^._high.svalue))))),GetNextReg(hregister),blocklabel(t^.blockid));
  800. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_A,aint(hi(lo(lo(int64(t^._high.svalue))))),GetNextReg(hregister),l1);
  801. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_BE,aint(lo(lo(lo(int64(t^._high.svalue))))),hregister,blocklabel(t^.blockid));
  802. cg.a_label(current_asmdata.CurrAsmList,l1);
  803. end
  804. else if def_cgsize(opsize) in [OS_S32,OS_32] then
  805. begin
  806. current_asmdata.getjumplabel(l1);
  807. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,jmp_lt,aint(hi(hi(int32(t^._high.svalue)))),GetNextReg(GetNextReg(GetNextReg(hregister))),blocklabel(t^.blockid));
  808. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,jmp_gt,aint(hi(hi(int32(t^._high.svalue)))),GetNextReg(GetNextReg(GetNextReg(hregister))),l1);
  809. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_B,aint(lo(hi(int32(t^._high.svalue)))),GetNextReg(GetNextReg(hregister)),blocklabel(t^.blockid));
  810. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_A,aint(lo(hi(int32(t^._high.svalue)))),GetNextReg(GetNextReg(hregister)),l1);
  811. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_B,aint(hi(lo(int32(t^._high.svalue)))),GetNextReg(hregister),blocklabel(t^.blockid));
  812. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_A,aint(hi(lo(int32(t^._high.svalue)))),GetNextReg(hregister),l1);
  813. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_BE,aint(lo(lo(int32(t^._high.svalue)))),hregister,blocklabel(t^.blockid));
  814. cg.a_label(current_asmdata.CurrAsmList,l1);
  815. end
  816. else if def_cgsize(opsize) in [OS_S16,OS_16] then
  817. begin
  818. current_asmdata.getjumplabel(l1);
  819. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,jmp_lt,aint(hi(int16(t^._high.svalue))),GetNextReg(hregister),blocklabel(t^.blockid));
  820. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,jmp_gt,aint(hi(int16(t^._high.svalue))),GetNextReg(hregister),l1);
  821. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_BE,aint(lo(int16(t^._high.svalue))),hregister,blocklabel(t^.blockid));
  822. cg.a_label(current_asmdata.CurrAsmList,l1);
  823. end
  824. else
  825. {$endif}
  826. begin
  827. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, opsize, jmp_le, aint(t^._high.svalue), hregister, blocklabel(t^.blockid));
  828. end;
  829. last:=t^._high;
  830. lastwasrange := true;
  831. end;
  832. if assigned(t^.greater) then
  833. genitem(t^.greater);
  834. end;
  835. begin
  836. last:=0;
  837. lastwasrange:=false;
  838. genitem(hp);
  839. hlcg.a_jmp_always(current_asmdata.CurrAsmList,elselabel);
  840. end;
  841. procedure tcgcasenode.pass_generate_code;
  842. var
  843. oldflowcontrol: tflowcontrol;
  844. i : longint;
  845. distv,
  846. lv,hv,
  847. max_label: tconstexprint;
  848. labelcnt : tcgint;
  849. max_linear_list : aint;
  850. otl, ofl: tasmlabel;
  851. isjump : boolean;
  852. max_dist,
  853. dist : aword;
  854. oldexecutionweight : longint;
  855. begin
  856. location_reset(location,LOC_VOID,OS_NO);
  857. ofl:=nil;
  858. otl:=nil;
  859. oldflowcontrol := flowcontrol;
  860. include(flowcontrol,fc_inflowcontrol);
  861. { Allocate labels }
  862. current_asmdata.getjumplabel(endlabel);
  863. current_asmdata.getjumplabel(elselabel);
  864. for i:=0 to blocks.count-1 do
  865. current_asmdata.getjumplabel(pcaseblock(blocks[i])^.blocklabel);
  866. with_sign:=is_signed(left.resultdef);
  867. if with_sign then
  868. begin
  869. jmp_gt:=OC_GT;
  870. jmp_lt:=OC_LT;
  871. jmp_le:=OC_LTE;
  872. end
  873. else
  874. begin
  875. jmp_gt:=OC_A;
  876. jmp_lt:=OC_B;
  877. jmp_le:=OC_BE;
  878. end;
  879. { save current current_procinfo.CurrTrueLabel and current_procinfo.CurrFalseLabel }
  880. isjump:=false;
  881. if left.expectloc=LOC_JUMP then
  882. begin
  883. otl:=current_procinfo.CurrTrueLabel;
  884. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  885. ofl:=current_procinfo.CurrFalseLabel;
  886. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  887. isjump:=true;
  888. end;
  889. secondpass(left);
  890. { determines the size of the operand }
  891. opsize:=left.resultdef;
  892. { copy the case expression to a register }
  893. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,opsize,false);
  894. {$ifndef cpu64bitalu}
  895. if def_cgsize(opsize) in [OS_S64,OS_64] then
  896. begin
  897. hregister:=left.location.register64.reglo;
  898. hregister2:=left.location.register64.reghi;
  899. end
  900. else
  901. {$endif not cpu64bitalu}
  902. hregister:=left.location.register;
  903. if isjump then
  904. begin
  905. current_procinfo.CurrTrueLabel:=otl;
  906. current_procinfo.CurrFalseLabel:=ofl;
  907. end
  908. else
  909. if (left.location.loc=LOC_JUMP) then
  910. internalerror(2006050501);
  911. { we need the min_label always to choose between }
  912. { cmps and subs/decs }
  913. min_label:=case_get_min(labels);
  914. { Generate the jumps }
  915. {$ifdef OLDREGVARS}
  916. load_all_regvars(current_asmdata.CurrAsmList);
  917. {$endif OLDREGVARS}
  918. {$ifndef cpu64bitalu}
  919. if def_cgsize(opsize) in [OS_64,OS_S64] then
  920. genlinearcmplist(labels)
  921. else
  922. {$endif not cpu64bitalu}
  923. begin
  924. if cs_opt_level1 in current_settings.optimizerswitches then
  925. begin
  926. { procedures are empirically passed on }
  927. { consumption can also be calculated }
  928. { but does it pay on the different }
  929. { processors? }
  930. { moreover can the size only be appro- }
  931. { ximated as it is not known if rel8, }
  932. { rel16 or rel32 jumps are used }
  933. max_label:=case_get_max(labels);
  934. labelcnt:=case_count_labels(labels);
  935. { can we omit the range check of the jump table ? }
  936. getrange(left.resultdef,lv,hv);
  937. jumptable_no_range:=(lv=min_label) and (hv=max_label);
  938. { hack a little bit, because the range can be greater }
  939. { than the positive range of a aint }
  940. if (min_label<0) and (max_label>0) then
  941. distv:=max_label+min_label
  942. else
  943. distv:=max_label-min_label;
  944. if (distv>=0) then
  945. dist:=distv.uvalue
  946. else
  947. dist:=-distv.svalue;
  948. { optimize for size ? }
  949. if cs_opt_size in current_settings.optimizerswitches then
  950. begin
  951. if has_jumptable and
  952. (min_label>=int64(low(aint))) and
  953. (max_label<=high(aint)) and
  954. not((labelcnt<=2) or
  955. ((max_label-min_label)<0) or
  956. ((max_label-min_label)>3*labelcnt)) then
  957. begin
  958. { if the labels less or more a continuum then }
  959. genjumptable(labels,min_label.svalue,max_label.svalue);
  960. end
  961. else
  962. begin
  963. { a linear list is always smaller than a jump tree }
  964. genlinearlist(labels);
  965. end;
  966. end
  967. else
  968. begin
  969. max_dist:=4*labelcnt;
  970. if jumptable_no_range then
  971. max_linear_list:=4
  972. else
  973. max_linear_list:=2;
  974. { allow processor specific values }
  975. optimizevalues(max_linear_list,max_dist);
  976. if (labelcnt<=max_linear_list) then
  977. genlinearlist(labels)
  978. else
  979. begin
  980. if (has_jumptable) and
  981. (dist<max_dist) and
  982. (min_label>=int64(low(aint))) and
  983. (max_label<=high(aint)) then
  984. genjumptable(labels,min_label.svalue,max_label.svalue)
  985. else
  986. genlinearlist(labels);
  987. end;
  988. end;
  989. end
  990. else
  991. { it's always not bad }
  992. genlinearlist(labels);
  993. end;
  994. { estimates the repeat of each instruction }
  995. oldexecutionweight:=cg.executionweight;
  996. cg.executionweight:=cg.executionweight div case_count_labels(labels);
  997. if cg.executionweight<1 then
  998. cg.executionweight:=1;
  999. { generate the instruction blocks }
  1000. for i:=0 to blocks.count-1 do
  1001. begin
  1002. current_asmdata.CurrAsmList.concat(cai_align.create(current_settings.alignment.jumpalign));
  1003. cg.a_label(current_asmdata.CurrAsmList,pcaseblock(blocks[i])^.blocklabel);
  1004. secondpass(pcaseblock(blocks[i])^.statement);
  1005. { don't come back to case line }
  1006. current_filepos:=current_asmdata.CurrAsmList.getlasttaifilepos^;
  1007. {$ifdef OLDREGVARS}
  1008. load_all_regvars(current_asmdata.CurrAsmList);
  1009. {$endif OLDREGVARS}
  1010. hlcg.a_jmp_always(current_asmdata.CurrAsmList,endlabel);
  1011. end;
  1012. current_asmdata.CurrAsmList.concat(cai_align.create(current_settings.alignment.jumpalign));
  1013. { ...and the else block }
  1014. hlcg.a_label(current_asmdata.CurrAsmList,elselabel);
  1015. if assigned(elseblock) then
  1016. begin
  1017. secondpass(elseblock);
  1018. {$ifdef OLDREGVARS}
  1019. load_all_regvars(current_asmdata.CurrAsmList);
  1020. {$endif OLDREGVARS}
  1021. end;
  1022. cg.executionweight:=oldexecutionweight;
  1023. current_asmdata.CurrAsmList.concat(cai_align.create(current_settings.alignment.jumpalign));
  1024. hlcg.a_label(current_asmdata.CurrAsmList,endlabel);
  1025. { Reset labels }
  1026. for i:=0 to blocks.count-1 do
  1027. pcaseblock(blocks[i])^.blocklabel:=nil;
  1028. flowcontrol := oldflowcontrol + (flowcontrol - [fc_inflowcontrol]);
  1029. end;
  1030. begin
  1031. csetelementnode:=tcgsetelementnode;
  1032. cinnode:=tcginnode;
  1033. ccasenode:=tcgcasenode;
  1034. end.