2
0

ncgset.pas 34 KB

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