ncgset.pas 35 KB

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