ncgset.pas 35 KB

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