2
0

ncgset.pas 61 KB

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