ncgset.pas 57 KB

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