ncgset.pas 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134
  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. newsize: tcgsize;
  469. newdef: tdef;
  470. procedure genitem(t : pcaselabel);
  471. procedure gensub(value:aint);
  472. begin
  473. { here, since the sub and cmp are separate we need
  474. to move the result before subtract to help
  475. the register allocator
  476. }
  477. hlcg.a_load_reg_reg(current_asmdata.CurrAsmList, opsize, opsize, hregister, scratch_reg);
  478. hlcg.a_op_const_reg(current_asmdata.CurrAsmList, OP_SUB, opsize, value, hregister);
  479. end;
  480. begin
  481. if assigned(t^.less) then
  482. genitem(t^.less);
  483. { do we need to test the first value? }
  484. if first and (t^._low>get_min_value(left.resultdef)) then
  485. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,jmp_lt,aint(t^._low.svalue),hregister,elselabel);
  486. if t^._low=t^._high then
  487. begin
  488. if t^._low-last=0 then
  489. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,OC_EQ,0,hregister,blocklabel(t^.blockid))
  490. else
  491. begin
  492. gensub(aint(t^._low.svalue-last.svalue));
  493. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,
  494. OC_EQ,aint(t^._low.svalue-last.svalue),scratch_reg,blocklabel(t^.blockid));
  495. end;
  496. last:=t^._low;
  497. end
  498. else
  499. begin
  500. { it begins with the smallest label, if the value }
  501. { is even smaller then jump immediately to the }
  502. { ELSE-label }
  503. if first then
  504. begin
  505. { have we to ajust the first value ? }
  506. if (t^._low>get_min_value(left.resultdef)) or (get_min_value(left.resultdef)<>0) then
  507. gensub(aint(t^._low.svalue));
  508. end
  509. else
  510. begin
  511. { if there is no unused label between the last and the }
  512. { present label then the lower limit can be checked }
  513. { immediately. else check the range in between: }
  514. gensub(aint(t^._low.svalue-last.svalue));
  515. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, opsize,jmp_lt,aint(t^._low.svalue-last.svalue),scratch_reg,elselabel);
  516. end;
  517. gensub(aint(t^._high.svalue-t^._low.svalue));
  518. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,jmp_le,aint(t^._high.svalue-t^._low.svalue),scratch_reg,blocklabel(t^.blockid));
  519. last:=t^._high;
  520. end;
  521. first:=false;
  522. if assigned(t^.greater) then
  523. genitem(t^.greater);
  524. end;
  525. begin
  526. { do we need to generate cmps? }
  527. if (with_sign and (min_label<0)) then
  528. genlinearcmplist(hp)
  529. else
  530. begin
  531. { sign/zero extend the value to a full register before starting to
  532. subtract values, so that on platforms that don't have
  533. subregisters of the same size as the value we don't generate
  534. sign/zero-extensions after every subtraction
  535. make newsize always signed, since we only do this if the size in
  536. bytes of the register is larger than the original opsize, so
  537. the value can always be represented by a larger signed type }
  538. newsize:=tcgsize2signed[reg_cgsize(hregister)];
  539. if tcgsize2size[newsize]>opsize.size then
  540. begin
  541. newdef:=cgsize_orddef(newsize);
  542. scratch_reg:=hlcg.getintregister(current_asmdata.CurrAsmList,newdef);
  543. hlcg.a_load_reg_reg(current_asmdata.CurrAsmList,opsize,newdef,hregister,scratch_reg);
  544. hregister:=scratch_reg;
  545. opsize:=newdef;
  546. end;
  547. last:=0;
  548. first:=true;
  549. scratch_reg:=hlcg.getintregister(current_asmdata.CurrAsmList,opsize);
  550. genitem(hp);
  551. hlcg.a_jmp_always(current_asmdata.CurrAsmList,elselabel);
  552. end;
  553. end;
  554. procedure tcgcasenode.genlinearcmplist(hp : pcaselabel);
  555. var
  556. last : TConstExprInt;
  557. lastwasrange: boolean;
  558. procedure genitem(t : pcaselabel);
  559. {$ifndef cpu64bitalu}
  560. var
  561. l1 : tasmlabel;
  562. {$endif not cpu64bitalu}
  563. begin
  564. if assigned(t^.less) then
  565. genitem(t^.less);
  566. if t^._low=t^._high then
  567. begin
  568. {$if defined(cpu32bitalu)}
  569. if def_cgsize(opsize) in [OS_S64,OS_64] then
  570. begin
  571. current_asmdata.getjumplabel(l1);
  572. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_32, OC_NE, aint(hi(int64(t^._low.svalue))),hregister2,l1);
  573. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_32, OC_EQ, aint(lo(int64(t^._low.svalue))),hregister, blocklabel(t^.blockid));
  574. cg.a_label(current_asmdata.CurrAsmList,l1);
  575. end
  576. else
  577. {$elseif defined(cpu16bitalu)}
  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_16, OC_NE, aint(hi(hi(int64(t^._low.svalue)))),GetNextReg(hregister2),l1);
  582. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, OC_NE, aint(lo(hi(int64(t^._low.svalue)))),hregister2,l1);
  583. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, OC_NE, aint(hi(lo(int64(t^._low.svalue)))),GetNextReg(hregister),l1);
  584. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, OC_EQ, aint(lo(lo(int64(t^._low.svalue)))),hregister, blocklabel(t^.blockid));
  585. cg.a_label(current_asmdata.CurrAsmList,l1);
  586. end
  587. else if def_cgsize(opsize) in [OS_S32,OS_32] then
  588. begin
  589. current_asmdata.getjumplabel(l1);
  590. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, OC_NE, aint(hi(int32(t^._low.svalue))),GetNextReg(hregister),l1);
  591. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, OC_EQ, aint(lo(int32(t^._low.svalue))),hregister, blocklabel(t^.blockid));
  592. cg.a_label(current_asmdata.CurrAsmList,l1);
  593. end
  594. else
  595. {$elseif defined(cpu8bitalu)}
  596. if def_cgsize(opsize) in [OS_S64,OS_64] then
  597. begin
  598. current_asmdata.getjumplabel(l1);
  599. 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);
  600. 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);
  601. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_8, OC_NE, aint(hi(lo(hi(int64(t^._low.svalue))))),GetNextReg(hregister2),l1);
  602. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_8, OC_NE, aint(lo(lo(hi(int64(t^._low.svalue))))),hregister2,l1);
  603. 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);
  604. 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);
  605. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_8, OC_NE, aint(hi(lo(lo(int64(t^._low.svalue))))),GetNextReg(hregister),l1);
  606. 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));
  607. cg.a_label(current_asmdata.CurrAsmList,l1);
  608. end
  609. else if def_cgsize(opsize) in [OS_S32,OS_32] then
  610. begin
  611. current_asmdata.getjumplabel(l1);
  612. 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);
  613. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_8, OC_NE, aint(lo(hi(int32(t^._low.svalue)))),GetNextReg(GetNextReg(hregister)),l1);
  614. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_8, OC_NE, aint(hi(lo(int32(t^._low.svalue)))),GetNextReg(hregister),l1);
  615. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_8, OC_EQ, aint(lo(lo(int32(t^._low.svalue)))),hregister, blocklabel(t^.blockid));
  616. cg.a_label(current_asmdata.CurrAsmList,l1);
  617. end
  618. else if def_cgsize(opsize) in [OS_S16,OS_16] then
  619. begin
  620. current_asmdata.getjumplabel(l1);
  621. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_8, OC_NE, aint(hi(int16(t^._low.svalue))),GetNextReg(hregister),l1);
  622. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_8, OC_EQ, aint(lo(int16(t^._low.svalue))),hregister, blocklabel(t^.blockid));
  623. cg.a_label(current_asmdata.CurrAsmList,l1);
  624. end
  625. else
  626. {$endif}
  627. begin
  628. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, opsize, OC_EQ, aint(t^._low.svalue),hregister, blocklabel(t^.blockid));
  629. end;
  630. { Reset last here, because we've only checked for one value and need to compare
  631. for the next range both the lower and upper bound }
  632. lastwasrange := false;
  633. end
  634. else
  635. begin
  636. { it begins with the smallest label, if the value }
  637. { is even smaller then jump immediately to the }
  638. { ELSE-label }
  639. if not lastwasrange or (t^._low-last>1) then
  640. begin
  641. {$if defined(cpu32bitalu)}
  642. if def_cgsize(opsize) in [OS_64,OS_S64] then
  643. begin
  644. current_asmdata.getjumplabel(l1);
  645. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_32, jmp_lt, aint(hi(int64(t^._low.svalue))),
  646. hregister2, elselabel);
  647. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_32, jmp_gt, aint(hi(int64(t^._low.svalue))),
  648. hregister2, l1);
  649. { the comparisation of the low dword must be always unsigned! }
  650. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_32, OC_B, aint(lo(int64(t^._low.svalue))), hregister, elselabel);
  651. cg.a_label(current_asmdata.CurrAsmList,l1);
  652. end
  653. else
  654. {$elseif defined(cpu16bitalu)}
  655. if def_cgsize(opsize) in [OS_64,OS_S64] then
  656. begin
  657. current_asmdata.getjumplabel(l1);
  658. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, jmp_lt, aint(hi(hi(int64(t^._low.svalue)))),
  659. GetNextReg(hregister2), elselabel);
  660. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, jmp_gt, aint(hi(hi(int64(t^._low.svalue)))),
  661. GetNextReg(hregister2), l1);
  662. { the comparison of the low words must be always unsigned! }
  663. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, OC_B, aint(lo(hi(int64(t^._low.svalue)))),
  664. hregister2, elselabel);
  665. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, OC_A, aint(lo(hi(int64(t^._low.svalue)))),
  666. hregister2, l1);
  667. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, OC_B, aint(hi(lo(int64(t^._low.svalue)))),
  668. GetNextReg(hregister), elselabel);
  669. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, OC_A, aint(hi(lo(int64(t^._low.svalue)))),
  670. GetNextReg(hregister), l1);
  671. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, OC_B, aint(lo(lo(int64(t^._low.svalue)))), hregister, elselabel);
  672. cg.a_label(current_asmdata.CurrAsmList,l1);
  673. end
  674. else if def_cgsize(opsize) in [OS_32,OS_S32] then
  675. begin
  676. current_asmdata.getjumplabel(l1);
  677. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, jmp_lt, aint(hi(int32(t^._low.svalue))),
  678. GetNextReg(hregister), elselabel);
  679. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, jmp_gt, aint(hi(int32(t^._low.svalue))),
  680. GetNextReg(hregister), l1);
  681. { the comparisation of the low dword must be always unsigned! }
  682. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, OC_B, aint(lo(int32(t^._low.svalue))), hregister, elselabel);
  683. cg.a_label(current_asmdata.CurrAsmList,l1);
  684. end
  685. else
  686. {$elseif defined(cpu8bitalu)}
  687. if def_cgsize(opsize) in [OS_64,OS_S64] then
  688. begin
  689. current_asmdata.getjumplabel(l1);
  690. 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);
  691. 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);
  692. { the comparison of the low words must be always unsigned! }
  693. 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);
  694. 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);
  695. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_B,aint(hi(lo(hi(int64(t^._low.svalue))))),GetNextReg(hregister2),elselabel);
  696. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_A,aint(hi(lo(hi(int64(t^._low.svalue))))),GetNextReg(hregister2),l1);
  697. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_B,aint(lo(lo(hi(int64(t^._low.svalue))))),hregister2,elselabel);
  698. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_A,aint(lo(lo(hi(int64(t^._low.svalue))))),hregister2,l1);
  699. 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);
  700. 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);
  701. 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);
  702. 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);
  703. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_B,aint(hi(lo(lo(int64(t^._low.svalue))))),GetNextReg(hregister),elselabel);
  704. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_A,aint(hi(lo(lo(int64(t^._low.svalue))))),GetNextReg(hregister),l1);
  705. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_B,aint(lo(lo(lo(int64(t^._low.svalue))))),hregister,elselabel);
  706. cg.a_label(current_asmdata.CurrAsmList,l1);
  707. end
  708. else if def_cgsize(opsize) in [OS_32,OS_S32] then
  709. begin
  710. current_asmdata.getjumplabel(l1);
  711. 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);
  712. 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);
  713. { the comparison of the low words must be always unsigned! }
  714. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_B,aint(lo(hi(int32(t^._low.svalue)))),GetNextReg(GetNextReg(hregister)),elselabel);
  715. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_A,aint(lo(hi(int32(t^._low.svalue)))),GetNextReg(GetNextReg(hregister)),l1);
  716. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_B,aint(hi(lo(int32(t^._low.svalue)))),GetNextReg(hregister),elselabel);
  717. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_A,aint(hi(lo(int32(t^._low.svalue)))),GetNextReg(hregister),l1);
  718. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_B,aint(lo(lo(int32(t^._low.svalue)))),hregister,elselabel);
  719. cg.a_label(current_asmdata.CurrAsmList,l1);
  720. end
  721. else if def_cgsize(opsize) in [OS_16,OS_S16] then
  722. begin
  723. current_asmdata.getjumplabel(l1);
  724. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,jmp_lt,aint(hi(int16(t^._low.svalue))),GetNextReg(hregister),elselabel);
  725. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,jmp_gt,aint(hi(int16(t^._low.svalue))),GetNextReg(hregister),l1);
  726. { the comparisation of the low dword must be always unsigned! }
  727. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_B,aint(lo(int16(t^._low.svalue))),hregister,elselabel);
  728. cg.a_label(current_asmdata.CurrAsmList,l1);
  729. end
  730. else
  731. {$endif}
  732. begin
  733. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, opsize, jmp_lt, aint(t^._low.svalue), hregister,
  734. elselabel);
  735. end;
  736. end;
  737. {$if defined(cpu32bitalu)}
  738. if def_cgsize(opsize) in [OS_S64,OS_64] then
  739. begin
  740. current_asmdata.getjumplabel(l1);
  741. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_32, jmp_lt, aint(hi(int64(t^._high.svalue))), hregister2,
  742. blocklabel(t^.blockid));
  743. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_32, jmp_gt, aint(hi(int64(t^._high.svalue))), hregister2,
  744. l1);
  745. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_32, OC_BE, aint(lo(int64(t^._high.svalue))), hregister, blocklabel(t^.blockid));
  746. cg.a_label(current_asmdata.CurrAsmList,l1);
  747. end
  748. else
  749. {$elseif defined(cpu16bitalu)}
  750. if def_cgsize(opsize) in [OS_S64,OS_64] then
  751. begin
  752. current_asmdata.getjumplabel(l1);
  753. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, jmp_lt, aint(hi(hi(int64(t^._high.svalue)))), GetNextReg(hregister2),
  754. blocklabel(t^.blockid));
  755. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, jmp_gt, aint(hi(hi(int64(t^._high.svalue)))), GetNextReg(hregister2),
  756. l1);
  757. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, OC_B, aint(lo(hi(int64(t^._high.svalue)))), hregister2,
  758. blocklabel(t^.blockid));
  759. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, OC_A, aint(lo(hi(int64(t^._high.svalue)))), hregister2,
  760. l1);
  761. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, OC_B, aint(hi(lo(int64(t^._high.svalue)))), GetNextReg(hregister),
  762. blocklabel(t^.blockid));
  763. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, OC_A, aint(hi(lo(int64(t^._high.svalue)))), GetNextReg(hregister),
  764. l1);
  765. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, OC_BE, aint(lo(lo(int64(t^._high.svalue)))), hregister, blocklabel(t^.blockid));
  766. cg.a_label(current_asmdata.CurrAsmList,l1);
  767. end
  768. else if def_cgsize(opsize) in [OS_S32,OS_32] then
  769. begin
  770. current_asmdata.getjumplabel(l1);
  771. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, jmp_lt, aint(hi(int32(t^._high.svalue))), GetNextReg(hregister),
  772. blocklabel(t^.blockid));
  773. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, jmp_gt, aint(hi(int32(t^._high.svalue))), GetNextReg(hregister),
  774. l1);
  775. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, OC_BE, aint(lo(int32(t^._high.svalue))), hregister, blocklabel(t^.blockid));
  776. cg.a_label(current_asmdata.CurrAsmList,l1);
  777. end
  778. else
  779. {$elseif defined(cpu8bitalu)}
  780. if def_cgsize(opsize) in [OS_S64,OS_64] then
  781. begin
  782. current_asmdata.getjumplabel(l1);
  783. 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));
  784. 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);
  785. 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));
  786. 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);
  787. 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));
  788. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_A,aint(hi(lo(hi(int64(t^._high.svalue))))),GetNextReg(hregister2),l1);
  789. 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));
  790. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_A,aint(lo(lo(hi(int64(t^._high.svalue))))),hregister2,l1);
  791. 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));
  792. 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);
  793. 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));
  794. 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);
  795. 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));
  796. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_A,aint(hi(lo(lo(int64(t^._high.svalue))))),GetNextReg(hregister),l1);
  797. 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));
  798. cg.a_label(current_asmdata.CurrAsmList,l1);
  799. end
  800. else if def_cgsize(opsize) in [OS_S32,OS_32] then
  801. begin
  802. current_asmdata.getjumplabel(l1);
  803. 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));
  804. 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);
  805. 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));
  806. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_A,aint(lo(hi(int32(t^._high.svalue)))),GetNextReg(GetNextReg(hregister)),l1);
  807. 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));
  808. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_A,aint(hi(lo(int32(t^._high.svalue)))),GetNextReg(hregister),l1);
  809. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_BE,aint(lo(lo(int32(t^._high.svalue)))),hregister,blocklabel(t^.blockid));
  810. cg.a_label(current_asmdata.CurrAsmList,l1);
  811. end
  812. else if def_cgsize(opsize) in [OS_S16,OS_16] then
  813. begin
  814. current_asmdata.getjumplabel(l1);
  815. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,jmp_lt,aint(hi(int16(t^._high.svalue))),GetNextReg(hregister),blocklabel(t^.blockid));
  816. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,jmp_gt,aint(hi(int16(t^._high.svalue))),GetNextReg(hregister),l1);
  817. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_BE,aint(lo(int16(t^._high.svalue))),hregister,blocklabel(t^.blockid));
  818. cg.a_label(current_asmdata.CurrAsmList,l1);
  819. end
  820. else
  821. {$endif}
  822. begin
  823. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, opsize, jmp_le, aint(t^._high.svalue), hregister, blocklabel(t^.blockid));
  824. end;
  825. last:=t^._high;
  826. lastwasrange := true;
  827. end;
  828. if assigned(t^.greater) then
  829. genitem(t^.greater);
  830. end;
  831. begin
  832. last:=0;
  833. lastwasrange:=false;
  834. genitem(hp);
  835. hlcg.a_jmp_always(current_asmdata.CurrAsmList,elselabel);
  836. end;
  837. procedure tcgcasenode.pass_generate_code;
  838. var
  839. oldflowcontrol: tflowcontrol;
  840. i : longint;
  841. distv,
  842. lv,hv,
  843. max_label: tconstexprint;
  844. labelcnt : aint;
  845. max_linear_list : aint;
  846. otl, ofl: tasmlabel;
  847. isjump : boolean;
  848. max_dist,
  849. dist : aword;
  850. oldexecutionweight : longint;
  851. begin
  852. location_reset(location,LOC_VOID,OS_NO);
  853. ofl:=nil;
  854. otl:=nil;
  855. oldflowcontrol := flowcontrol;
  856. include(flowcontrol,fc_inflowcontrol);
  857. { Allocate labels }
  858. current_asmdata.getjumplabel(endlabel);
  859. current_asmdata.getjumplabel(elselabel);
  860. for i:=0 to blocks.count-1 do
  861. current_asmdata.getjumplabel(pcaseblock(blocks[i])^.blocklabel);
  862. with_sign:=is_signed(left.resultdef);
  863. if with_sign then
  864. begin
  865. jmp_gt:=OC_GT;
  866. jmp_lt:=OC_LT;
  867. jmp_le:=OC_LTE;
  868. end
  869. else
  870. begin
  871. jmp_gt:=OC_A;
  872. jmp_lt:=OC_B;
  873. jmp_le:=OC_BE;
  874. end;
  875. { save current current_procinfo.CurrTrueLabel and current_procinfo.CurrFalseLabel }
  876. isjump:=false;
  877. if left.expectloc=LOC_JUMP then
  878. begin
  879. otl:=current_procinfo.CurrTrueLabel;
  880. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  881. ofl:=current_procinfo.CurrFalseLabel;
  882. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  883. isjump:=true;
  884. end;
  885. secondpass(left);
  886. { determines the size of the operand }
  887. opsize:=left.resultdef;
  888. { copy the case expression to a register }
  889. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,opsize,false);
  890. {$ifndef cpu64bitalu}
  891. if def_cgsize(opsize) in [OS_S64,OS_64] then
  892. begin
  893. hregister:=left.location.register64.reglo;
  894. hregister2:=left.location.register64.reghi;
  895. end
  896. else
  897. {$endif not cpu64bitalu}
  898. hregister:=left.location.register;
  899. if isjump then
  900. begin
  901. current_procinfo.CurrTrueLabel:=otl;
  902. current_procinfo.CurrFalseLabel:=ofl;
  903. end
  904. else
  905. if (left.location.loc=LOC_JUMP) then
  906. internalerror(2006050501);
  907. { we need the min_label always to choose between }
  908. { cmps and subs/decs }
  909. min_label:=case_get_min(labels);
  910. { Generate the jumps }
  911. {$ifdef OLDREGVARS}
  912. load_all_regvars(current_asmdata.CurrAsmList);
  913. {$endif OLDREGVARS}
  914. {$ifndef cpu64bitalu}
  915. if def_cgsize(opsize) in [OS_64,OS_S64] then
  916. genlinearcmplist(labels)
  917. else
  918. {$endif not cpu64bitalu}
  919. begin
  920. if cs_opt_level1 in current_settings.optimizerswitches then
  921. begin
  922. { procedures are empirically passed on }
  923. { consumption can also be calculated }
  924. { but does it pay on the different }
  925. { processors? }
  926. { moreover can the size only be appro- }
  927. { ximated as it is not known if rel8, }
  928. { rel16 or rel32 jumps are used }
  929. max_label:=case_get_max(labels);
  930. labelcnt:=case_count_labels(labels);
  931. { can we omit the range check of the jump table ? }
  932. getrange(left.resultdef,lv,hv);
  933. jumptable_no_range:=(lv=min_label) and (hv=max_label);
  934. { hack a little bit, because the range can be greater }
  935. { than the positive range of a aint }
  936. if (min_label<0) and (max_label>0) then
  937. distv:=max_label+min_label
  938. else
  939. distv:=max_label-min_label;
  940. if (distv>=0) then
  941. dist:=distv.uvalue
  942. else
  943. dist:=-distv.svalue;
  944. { optimize for size ? }
  945. if cs_opt_size in current_settings.optimizerswitches then
  946. begin
  947. if has_jumptable and
  948. (min_label>=int64(low(aint))) and
  949. (max_label<=high(aint)) and
  950. not((labelcnt<=2) or
  951. ((max_label-min_label)<0) or
  952. ((max_label-min_label)>3*labelcnt)) then
  953. begin
  954. { if the labels less or more a continuum then }
  955. genjumptable(labels,min_label.svalue,max_label.svalue);
  956. end
  957. else
  958. begin
  959. { a linear list is always smaller than a jump tree }
  960. genlinearlist(labels);
  961. end;
  962. end
  963. else
  964. begin
  965. max_dist:=4*labelcnt;
  966. if jumptable_no_range then
  967. max_linear_list:=4
  968. else
  969. max_linear_list:=2;
  970. { allow processor specific values }
  971. optimizevalues(max_linear_list,max_dist);
  972. if (labelcnt<=max_linear_list) then
  973. genlinearlist(labels)
  974. else
  975. begin
  976. if (has_jumptable) and
  977. (dist<max_dist) and
  978. (min_label>=int64(low(aint))) and
  979. (max_label<=high(aint)) then
  980. genjumptable(labels,min_label.svalue,max_label.svalue)
  981. else
  982. genlinearlist(labels);
  983. end;
  984. end;
  985. end
  986. else
  987. { it's always not bad }
  988. genlinearlist(labels);
  989. end;
  990. { estimates the repeat of each instruction }
  991. oldexecutionweight:=cg.executionweight;
  992. cg.executionweight:=cg.executionweight div case_count_labels(labels);
  993. if cg.executionweight<1 then
  994. cg.executionweight:=1;
  995. { generate the instruction blocks }
  996. for i:=0 to blocks.count-1 do
  997. begin
  998. current_asmdata.CurrAsmList.concat(cai_align.create(current_settings.alignment.jumpalign));
  999. cg.a_label(current_asmdata.CurrAsmList,pcaseblock(blocks[i])^.blocklabel);
  1000. secondpass(pcaseblock(blocks[i])^.statement);
  1001. { don't come back to case line }
  1002. current_filepos:=current_asmdata.CurrAsmList.getlasttaifilepos^;
  1003. {$ifdef OLDREGVARS}
  1004. load_all_regvars(current_asmdata.CurrAsmList);
  1005. {$endif OLDREGVARS}
  1006. hlcg.a_jmp_always(current_asmdata.CurrAsmList,endlabel);
  1007. end;
  1008. current_asmdata.CurrAsmList.concat(cai_align.create(current_settings.alignment.jumpalign));
  1009. { ...and the else block }
  1010. hlcg.a_label(current_asmdata.CurrAsmList,elselabel);
  1011. if assigned(elseblock) then
  1012. begin
  1013. secondpass(elseblock);
  1014. {$ifdef OLDREGVARS}
  1015. load_all_regvars(current_asmdata.CurrAsmList);
  1016. {$endif OLDREGVARS}
  1017. end;
  1018. cg.executionweight:=oldexecutionweight;
  1019. current_asmdata.CurrAsmList.concat(cai_align.create(current_settings.alignment.jumpalign));
  1020. hlcg.a_label(current_asmdata.CurrAsmList,endlabel);
  1021. { Reset labels }
  1022. for i:=0 to blocks.count-1 do
  1023. pcaseblock(blocks[i])^.blocklabel:=nil;
  1024. flowcontrol := oldflowcontrol + (flowcontrol - [fc_inflowcontrol]);
  1025. end;
  1026. begin
  1027. csetelementnode:=tcgsetelementnode;
  1028. cinnode:=tcginnode;
  1029. ccasenode:=tcgcasenode;
  1030. end.