ncgset.pas 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl and Carl Eric Codere
  3. Generate generic assembler for in set/case labels
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit ncgset;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,globals,constexp,symtype,
  22. node,nset,cpubase,cgbase,cgutils,cgobj,aasmbase,aasmtai,aasmdata;
  23. type
  24. tcgsetelementnode = class(tsetelementnode)
  25. procedure pass_generate_code;override;
  26. end;
  27. Tsetpart=record
  28. range : boolean; {Part is a range.}
  29. start,stop : byte; {Start/stop when range; Stop=element when an element.}
  30. end;
  31. Tsetparts=array[1..8] of Tsetpart;
  32. tcginnode = 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 : tdef;
  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,hlcgobj;
  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:=is_smallset(right.resultdef) and
  157. not is_signed(left.resultdef) and
  158. ((left.resultdef.typ=orddef) and (torddef(left.resultdef).high<32) or
  159. (left.resultdef.typ=enumdef) and (tenumdef(left.resultdef).max<32));
  160. { Can we generate jumps? Possible for all types of sets }
  161. checkgenjumps:=(right.nodetype=setconstn) and
  162. analizeset(Tsetconstnode(right).value_set^,setparts,numparts,use_small);
  163. end;
  164. function tcginnode.pass_1: tnode;
  165. var
  166. setparts: Tsetparts;
  167. numparts: byte;
  168. use_small: boolean;
  169. begin
  170. result := inherited pass_1;
  171. if not(assigned(result)) and
  172. checkgenjumps(setparts,numparts,use_small) then
  173. expectloc := LOC_JUMP;
  174. end;
  175. procedure tcginnode.pass_generate_code;
  176. var
  177. adjustment,
  178. setbase : aint;
  179. l, l2 : tasmlabel;
  180. otl, ofl : tasmlabel;
  181. hr,
  182. pleftreg : tregister;
  183. setparts : Tsetparts;
  184. opsize : tcgsize;
  185. opdef : tdef;
  186. uopsize : tcgsize;
  187. uopdef : tdef;
  188. orgopsize : tcgsize;
  189. orgopdef : tdef;
  190. genjumps,
  191. use_small,
  192. isjump : boolean;
  193. i,numparts : byte;
  194. needslabel : Boolean;
  195. begin
  196. { We check first if we can generate jumps, this can be done
  197. because the resultdef is already set in firstpass }
  198. genjumps := checkgenjumps(setparts,numparts,use_small);
  199. orgopsize := def_cgsize(left.resultdef);
  200. orgopdef := left.resultdef;
  201. uopsize := OS_32;
  202. uopdef := u32inttype;
  203. if is_signed(left.resultdef) then
  204. begin
  205. opsize := OS_S32;
  206. opdef := s32inttype;
  207. end
  208. else
  209. begin
  210. opsize := uopsize;
  211. opdef := uopdef;
  212. end;
  213. needslabel := false;
  214. isjump:=false;
  215. if (left.expectloc=LOC_JUMP) then
  216. begin
  217. otl:=current_procinfo.CurrTrueLabel;
  218. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  219. ofl:=current_procinfo.CurrFalseLabel;
  220. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  221. isjump:=true;
  222. end
  223. else if not genjumps then
  224. { calculate both operators }
  225. { the complex one first }
  226. { only if left will not be a LOC_JUMP, to keep complexity in the }
  227. { code generator down. This almost never happens anyway, only in }
  228. { case like "if ((a in someset) in someboolset) then" etc }
  229. { also not in case of genjumps, because then we don't secondpass }
  230. { right at all (so we have to make sure that "right" really is }
  231. { "right" and not "swapped left" in that case) }
  232. firstcomplex(self);
  233. secondpass(left);
  234. if isjump then
  235. begin
  236. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,orgopdef,opdef,true);
  237. left.resultdef:=opdef;
  238. current_procinfo.CurrTrueLabel:=otl;
  239. current_procinfo.CurrFalseLabel:=ofl;
  240. end
  241. else if (left.location.loc=LOC_JUMP) then
  242. internalerror(2007070101);
  243. { Only process the right if we are not generating jumps }
  244. if not genjumps then
  245. secondpass(right);
  246. if codegenerror then
  247. exit;
  248. { ofcourse not commutative }
  249. if nf_swapped in flags then
  250. swapleftright;
  251. setbase:=tsetdef(right.resultdef).setbase;
  252. if genjumps then
  253. begin
  254. { location is always LOC_JUMP }
  255. location_reset(location,LOC_JUMP,OS_NO);
  256. { If register is used, use only lower 8 bits }
  257. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,opdef,false);
  258. pleftreg := left.location.register;
  259. { how much have we already substracted from the x in the }
  260. { "x in [y..z]" expression }
  261. adjustment := 0;
  262. hr:=NR_NO;
  263. for i:=1 to numparts do
  264. if setparts[i].range then
  265. { use fact that a <= x <= b <=> aword(x-a) <= aword(b-a) }
  266. begin
  267. { is the range different from all legal values? }
  268. if (setparts[i].stop-setparts[i].start <> 255) or not (orgopsize = OS_8) then
  269. begin
  270. { yes, is the lower bound <> 0? }
  271. if (setparts[i].start <> 0) then
  272. { we're going to substract from the left register, }
  273. { so in case of a LOC_CREGISTER first move the value }
  274. { to edi (not done before because now we can do the }
  275. { move and substract in one instruction with LEA) }
  276. if (left.location.loc = LOC_CREGISTER) and
  277. (hr<>pleftreg) then
  278. begin
  279. { don't change this back to a_op_const_reg/a_load_reg_reg, since pleftreg must not be modified }
  280. hr:=hlcg.getintregister(current_asmdata.CurrAsmList,opdef);
  281. hlcg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,opdef,setparts[i].start,pleftreg,hr);
  282. pleftreg:=hr;
  283. end
  284. else
  285. begin
  286. { otherwise, the value is already in a register }
  287. { that can be modified }
  288. hlcg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SUB,opdef,
  289. setparts[i].start-adjustment,pleftreg)
  290. end;
  291. { new total value substracted from x: }
  292. { adjustment + (setparts[i].start - adjustment) }
  293. adjustment := setparts[i].start;
  294. { check if result < b-a+1 (not "result <= b-a", since }
  295. { we need a carry in case the element is in the range }
  296. { (this will never overflow since we check at the }
  297. { beginning whether stop-start <> 255) }
  298. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, uopdef, OC_B,
  299. setparts[i].stop-setparts[i].start+1,pleftreg,current_procinfo.CurrTrueLabel);
  300. end
  301. else
  302. { if setparts[i].start = 0 and setparts[i].stop = 255, }
  303. { it's always true since "in" is only allowed for bytes }
  304. begin
  305. hlcg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrTrueLabel);
  306. end;
  307. end
  308. else
  309. begin
  310. { Emit code to check if left is an element }
  311. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, opdef, OC_EQ,
  312. setparts[i].stop-adjustment,pleftreg,current_procinfo.CurrTrueLabel);
  313. end;
  314. { To compensate for not doing a second pass }
  315. right.location.reference.symbol:=nil;
  316. hlcg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  317. end
  318. else
  319. {*****************************************************************}
  320. { NO JUMP TABLE GENERATION }
  321. {*****************************************************************}
  322. begin
  323. { location is always LOC_REGISTER }
  324. location_reset(location, LOC_REGISTER, uopsize{def_cgsize(resultdef)});
  325. { allocate a register for the result }
  326. location.register := cg.getintregister(current_asmdata.CurrAsmList, uopsize);
  327. { We will now generated code to check the set itself, no jmps,
  328. handle smallsets separate, because it allows faster checks }
  329. if use_small then
  330. begin
  331. {**************************** SMALL SET **********************}
  332. if left.location.loc=LOC_CONSTANT then
  333. begin
  334. cg.a_bit_test_const_loc_reg(current_asmdata.CurrAsmList,location.size,
  335. left.location.value-setbase,right.location,
  336. location.register);
  337. end
  338. else
  339. begin
  340. location_force_reg(current_asmdata.CurrAsmList,left.location,opsize,true);
  341. register_maybe_adjust_setbase(current_asmdata.CurrAsmList,left.location,setbase);
  342. cg.a_bit_test_reg_loc_reg(current_asmdata.CurrAsmList,left.location.size,
  343. location.size,left.location.register,right.location,location.register);
  344. end;
  345. end
  346. else
  347. {************************** NOT SMALL SET ********************}
  348. begin
  349. if right.location.loc=LOC_CONSTANT then
  350. begin
  351. { can it actually occur currently? CEC }
  352. { yes: "if bytevar in [1,3,5,7,9,11,13,15]" (JM) }
  353. { note: this code assumes that left in [0..255], which is a valid }
  354. { assumption (other cases will be caught by range checking) (JM) }
  355. { load left in register }
  356. location_force_reg(current_asmdata.CurrAsmList,left.location,location.size,true);
  357. register_maybe_adjust_setbase(current_asmdata.CurrAsmList,left.location,setbase);
  358. { emit bit test operation -- warning: do not use
  359. location_force_reg() to force a set into a register, except
  360. to a register of the same size as the set. The reason is
  361. that on big endian systems, this would require moving the
  362. set to the most significant part of the new register,
  363. and location_force_register can't do that (it does not
  364. know the type).
  365. a_bit_test_reg_loc_reg() properly takes into account the
  366. size of the set to adjust the register index to test }
  367. cg.a_bit_test_reg_loc_reg(current_asmdata.CurrAsmList,
  368. left.location.size,location.size,
  369. left.location.register,right.location,location.register);
  370. { now zero the result if left > nr_of_bits_in_right_register }
  371. hr := cg.getintregister(current_asmdata.CurrAsmList,location.size);
  372. { if left > tcgsize2size[opsize]*8 then hr := 0 else hr := $ffffffff }
  373. { (left.location.size = location.size at this point) }
  374. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SUB, location.size, tcgsize2size[opsize]*8, left.location.register, hr);
  375. cg.a_op_const_reg(current_asmdata.CurrAsmList, OP_SAR, location.size, (tcgsize2size[opsize]*8)-1, hr);
  376. { if left > tcgsize2size[opsize]*8-1, then result := 0 else result := result of bit test }
  377. cg.a_op_reg_reg(current_asmdata.CurrAsmList, OP_AND, location.size, hr, location.register);
  378. end { of right.location.loc=LOC_CONSTANT }
  379. { do search in a normal set which could have >32 elements
  380. but also used if the left side contains higher values > 32 }
  381. else if (left.location.loc=LOC_CONSTANT) then
  382. begin
  383. if (left.location.value < setbase) or (((left.location.value-setbase) shr 3) >= right.resultdef.size) then
  384. {should be caught earlier }
  385. internalerror(2007020402);
  386. cg.a_bit_test_const_loc_reg(current_asmdata.CurrAsmList,location.size,left.location.value-setbase,
  387. right.location,location.register);
  388. end
  389. else
  390. begin
  391. location_force_reg(current_asmdata.CurrAsmList, left.location, opsize, true);
  392. register_maybe_adjust_setbase(current_asmdata.CurrAsmList,left.location,setbase);
  393. pleftreg := left.location.register;
  394. if (opsize >= OS_S8) or { = if signed }
  395. ((left.resultdef.typ=orddef) and
  396. ((torddef(left.resultdef).low < int64(tsetdef(right.resultdef).setbase)) or
  397. (torddef(left.resultdef).high > int64(tsetdef(right.resultdef).setmax)))) or
  398. ((left.resultdef.typ=enumdef) and
  399. ((tenumdef(left.resultdef).min < aint(tsetdef(right.resultdef).setbase)) or
  400. (tenumdef(left.resultdef).max > aint(tsetdef(right.resultdef).setmax)))) then
  401. begin
  402. current_asmdata.getjumplabel(l);
  403. current_asmdata.getjumplabel(l2);
  404. needslabel := True;
  405. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, left.location.size, OC_BE, tsetdef(right.resultdef).setmax-tsetdef(right.resultdef).setbase, pleftreg, l);
  406. cg.a_load_const_reg(current_asmdata.CurrAsmList, location.size, 0, location.register);
  407. cg.a_jmp_always(current_asmdata.CurrAsmList, l2);
  408. cg.a_label(current_asmdata.CurrAsmList, l);
  409. end;
  410. cg.a_bit_test_reg_loc_reg(current_asmdata.CurrAsmList,left.location.size,location.size,
  411. pleftreg,right.location,location.register);
  412. if needslabel then
  413. cg.a_label(current_asmdata.CurrAsmList, l2);
  414. end;
  415. end;
  416. end;
  417. location_freetemp(current_asmdata.CurrAsmList, right.location);
  418. location.size := def_cgsize(resultdef);
  419. location.register := cg.makeregsize(current_asmdata.CurrAsmList, location.register, location.size);
  420. end;
  421. {*****************************************************************************
  422. TCGCASENODE
  423. *****************************************************************************}
  424. function tcgcasenode.blocklabel(id:longint):tasmlabel;
  425. begin
  426. if not assigned(blocks[id]) then
  427. internalerror(200411301);
  428. result:=pcaseblock(blocks[id])^.blocklabel;
  429. end;
  430. procedure tcgcasenode.optimizevalues(var max_linear_list:aint;var max_dist:aword);
  431. begin
  432. { no changes by default }
  433. end;
  434. function tcgcasenode.has_jumptable : boolean;
  435. begin
  436. { No jumptable support in the default implementation }
  437. has_jumptable:=false;
  438. end;
  439. procedure tcgcasenode.genjumptable(hp : pcaselabel;min_,max_ : aint);
  440. begin
  441. internalerror(200209161);
  442. end;
  443. procedure tcgcasenode.genlinearlist(hp : pcaselabel);
  444. var
  445. first : boolean;
  446. last : TConstExprInt;
  447. scratch_reg: tregister;
  448. procedure genitem(t : pcaselabel);
  449. procedure gensub(value:aint);
  450. begin
  451. { here, since the sub and cmp are separate we need
  452. to move the result before subtract to help
  453. the register allocator
  454. }
  455. hlcg.a_load_reg_reg(current_asmdata.CurrAsmList, opsize, opsize, hregister, scratch_reg);
  456. hlcg.a_op_const_reg(current_asmdata.CurrAsmList, OP_SUB, opsize, value, hregister);
  457. end;
  458. begin
  459. if assigned(t^.less) then
  460. genitem(t^.less);
  461. { do we need to test the first value? }
  462. if first and (t^._low>get_min_value(left.resultdef)) then
  463. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,jmp_lt,aint(t^._low.svalue),hregister,elselabel);
  464. if t^._low=t^._high then
  465. begin
  466. if t^._low-last=0 then
  467. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,OC_EQ,0,hregister,blocklabel(t^.blockid))
  468. else
  469. begin
  470. gensub(aint(t^._low.svalue-last.svalue));
  471. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,
  472. OC_EQ,aint(t^._low.svalue-last.svalue),scratch_reg,blocklabel(t^.blockid));
  473. end;
  474. last:=t^._low;
  475. end
  476. else
  477. begin
  478. { it begins with the smallest label, if the value }
  479. { is even smaller then jump immediately to the }
  480. { ELSE-label }
  481. if first then
  482. begin
  483. { have we to ajust the first value ? }
  484. if (t^._low>get_min_value(left.resultdef)) or (get_min_value(left.resultdef)<>0) then
  485. gensub(aint(t^._low.svalue));
  486. end
  487. else
  488. begin
  489. { if there is no unused label between the last and the }
  490. { present label then the lower limit can be checked }
  491. { immediately. else check the range in between: }
  492. gensub(aint(t^._low.svalue-last.svalue));
  493. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, opsize,jmp_lt,aint(t^._low.svalue-last.svalue),scratch_reg,elselabel);
  494. end;
  495. gensub(aint(t^._high.svalue-t^._low.svalue));
  496. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,jmp_le,aint(t^._high.svalue-t^._low.svalue),scratch_reg,blocklabel(t^.blockid));
  497. last:=t^._high;
  498. end;
  499. first:=false;
  500. if assigned(t^.greater) then
  501. genitem(t^.greater);
  502. end;
  503. begin
  504. { do we need to generate cmps? }
  505. if (with_sign and (min_label<0)) then
  506. genlinearcmplist(hp)
  507. else
  508. begin
  509. last:=0;
  510. first:=true;
  511. scratch_reg:=hlcg.getintregister(current_asmdata.CurrAsmList,opsize);
  512. genitem(hp);
  513. hlcg.a_jmp_always(current_asmdata.CurrAsmList,elselabel);
  514. end;
  515. end;
  516. procedure tcgcasenode.genlinearcmplist(hp : pcaselabel);
  517. var
  518. last : TConstExprInt;
  519. lastwasrange: boolean;
  520. procedure genitem(t : pcaselabel);
  521. {$ifndef cpu64bitalu}
  522. var
  523. l1 : tasmlabel;
  524. {$endif not cpu64bitalu}
  525. begin
  526. if assigned(t^.less) then
  527. genitem(t^.less);
  528. if t^._low=t^._high then
  529. begin
  530. {$ifndef cpu64bitalu}
  531. if def_cgsize(opsize) in [OS_S64,OS_64] then
  532. begin
  533. current_asmdata.getjumplabel(l1);
  534. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_32, OC_NE, aint(hi(int64(t^._low.svalue))),hregister2,l1);
  535. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_32, OC_EQ, aint(lo(int64(t^._low.svalue))),hregister, blocklabel(t^.blockid));
  536. cg.a_label(current_asmdata.CurrAsmList,l1);
  537. end
  538. else
  539. {$endif not cpu64bitalu}
  540. begin
  541. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, opsize, OC_EQ, aint(t^._low.svalue),hregister, blocklabel(t^.blockid));
  542. end;
  543. { Reset last here, because we've only checked for one value and need to compare
  544. for the next range both the lower and upper bound }
  545. lastwasrange := false;
  546. end
  547. else
  548. begin
  549. { it begins with the smallest label, if the value }
  550. { is even smaller then jump immediately to the }
  551. { ELSE-label }
  552. if not lastwasrange or (t^._low-last>1) then
  553. begin
  554. {$ifndef cpu64bitalu}
  555. if def_cgsize(opsize) in [OS_64,OS_S64] then
  556. begin
  557. current_asmdata.getjumplabel(l1);
  558. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_32, jmp_lt, aint(hi(int64(t^._low.svalue))),
  559. hregister2, elselabel);
  560. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_32, jmp_gt, aint(hi(int64(t^._low.svalue))),
  561. hregister2, l1);
  562. { the comparisation of the low dword must be always unsigned! }
  563. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_32, OC_B, aint(lo(int64(t^._low.svalue))), hregister, elselabel);
  564. cg.a_label(current_asmdata.CurrAsmList,l1);
  565. end
  566. else
  567. {$endif not cpu64bitalu}
  568. begin
  569. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, opsize, jmp_lt, aint(t^._low.svalue), hregister,
  570. elselabel);
  571. end;
  572. end;
  573. {$ifndef cpu64bitalu}
  574. if def_cgsize(opsize) in [OS_S64,OS_64] then
  575. begin
  576. current_asmdata.getjumplabel(l1);
  577. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_32, jmp_lt, aint(hi(int64(t^._high.svalue))), hregister2,
  578. blocklabel(t^.blockid));
  579. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_32, jmp_gt, aint(hi(int64(t^._high.svalue))), hregister2,
  580. l1);
  581. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_32, OC_BE, aint(lo(int64(t^._high.svalue))), hregister, blocklabel(t^.blockid));
  582. cg.a_label(current_asmdata.CurrAsmList,l1);
  583. end
  584. else
  585. {$endif not cpu64bitalu}
  586. begin
  587. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, opsize, jmp_le, aint(t^._high.svalue), hregister, blocklabel(t^.blockid));
  588. end;
  589. last:=t^._high;
  590. lastwasrange := true;
  591. end;
  592. if assigned(t^.greater) then
  593. genitem(t^.greater);
  594. end;
  595. begin
  596. last:=0;
  597. lastwasrange:=false;
  598. genitem(hp);
  599. hlcg.a_jmp_always(current_asmdata.CurrAsmList,elselabel);
  600. end;
  601. procedure tcgcasenode.pass_generate_code;
  602. var
  603. oldflowcontrol: tflowcontrol;
  604. i : longint;
  605. distv,
  606. lv,hv,
  607. max_label: tconstexprint;
  608. labelcnt : aint;
  609. max_linear_list : aint;
  610. otl, ofl: tasmlabel;
  611. isjump : boolean;
  612. max_dist,
  613. dist : aword;
  614. oldexecutionweight : longint;
  615. begin
  616. location_reset(location,LOC_VOID,OS_NO);
  617. oldflowcontrol := flowcontrol;
  618. include(flowcontrol,fc_inflowcontrol);
  619. { Allocate labels }
  620. current_asmdata.getjumplabel(endlabel);
  621. current_asmdata.getjumplabel(elselabel);
  622. for i:=0 to blocks.count-1 do
  623. current_asmdata.getjumplabel(pcaseblock(blocks[i])^.blocklabel);
  624. with_sign:=is_signed(left.resultdef);
  625. if with_sign then
  626. begin
  627. jmp_gt:=OC_GT;
  628. jmp_lt:=OC_LT;
  629. jmp_le:=OC_LTE;
  630. end
  631. else
  632. begin
  633. jmp_gt:=OC_A;
  634. jmp_lt:=OC_B;
  635. jmp_le:=OC_BE;
  636. end;
  637. { save current current_procinfo.CurrTrueLabel and current_procinfo.CurrFalseLabel }
  638. isjump:=false;
  639. if left.expectloc=LOC_JUMP then
  640. begin
  641. otl:=current_procinfo.CurrTrueLabel;
  642. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  643. ofl:=current_procinfo.CurrFalseLabel;
  644. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  645. isjump:=true;
  646. end;
  647. secondpass(left);
  648. { determines the size of the operand }
  649. opsize:=left.resultdef;
  650. { copy the case expression to a register }
  651. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,opsize,false);
  652. {$ifndef cpu64bitalu}
  653. if def_cgsize(opsize) in [OS_S64,OS_64] then
  654. begin
  655. hregister:=left.location.register64.reglo;
  656. hregister2:=left.location.register64.reghi;
  657. end
  658. else
  659. {$endif not cpu64bitalu}
  660. hregister:=left.location.register;
  661. if isjump then
  662. begin
  663. current_procinfo.CurrTrueLabel:=otl;
  664. current_procinfo.CurrFalseLabel:=ofl;
  665. end
  666. else
  667. if (left.location.loc=LOC_JUMP) then
  668. internalerror(2006050501);
  669. { we need the min_label always to choose between }
  670. { cmps and subs/decs }
  671. min_label:=case_get_min(labels);
  672. { Generate the jumps }
  673. {$ifdef OLDREGVARS}
  674. load_all_regvars(current_asmdata.CurrAsmList);
  675. {$endif OLDREGVARS}
  676. {$ifndef cpu64bitalu}
  677. if def_cgsize(opsize) in [OS_64,OS_S64] then
  678. genlinearcmplist(labels)
  679. else
  680. {$endif not cpu64bitalu}
  681. begin
  682. if cs_opt_level1 in current_settings.optimizerswitches then
  683. begin
  684. { procedures are empirically passed on }
  685. { consumption can also be calculated }
  686. { but does it pay on the different }
  687. { processors? }
  688. { moreover can the size only be appro- }
  689. { ximated as it is not known if rel8, }
  690. { rel16 or rel32 jumps are used }
  691. max_label:=case_get_max(labels);
  692. labelcnt:=case_count_labels(labels);
  693. { can we omit the range check of the jump table ? }
  694. getrange(left.resultdef,lv,hv);
  695. jumptable_no_range:=(lv=min_label) and (hv=max_label);
  696. { hack a little bit, because the range can be greater }
  697. { than the positive range of a aint }
  698. if (min_label<0) and (max_label>0) then
  699. distv:=max_label+min_label
  700. else
  701. distv:=max_label-min_label;
  702. if (distv>=0) then
  703. dist:=distv.uvalue
  704. else
  705. dist:=-distv.svalue;
  706. { optimize for size ? }
  707. if cs_opt_size in current_settings.optimizerswitches then
  708. begin
  709. if has_jumptable and
  710. (min_label>=int64(low(aint))) and
  711. (max_label<=high(aint)) and
  712. not((labelcnt<=2) or
  713. ((max_label-min_label)<0) or
  714. ((max_label-min_label)>3*labelcnt)) then
  715. begin
  716. { if the labels less or more a continuum then }
  717. genjumptable(labels,min_label.svalue,max_label.svalue);
  718. end
  719. else
  720. begin
  721. { a linear list is always smaller than a jump tree }
  722. genlinearlist(labels);
  723. end;
  724. end
  725. else
  726. begin
  727. max_dist:=4*labelcnt;
  728. if jumptable_no_range then
  729. max_linear_list:=4
  730. else
  731. max_linear_list:=2;
  732. { allow processor specific values }
  733. optimizevalues(max_linear_list,max_dist);
  734. if (labelcnt<=max_linear_list) then
  735. genlinearlist(labels)
  736. else
  737. begin
  738. if (has_jumptable) and
  739. (dist<max_dist) and
  740. (min_label>=int64(low(aint))) and
  741. (max_label<=high(aint)) then
  742. genjumptable(labels,min_label.svalue,max_label.svalue)
  743. else
  744. genlinearlist(labels);
  745. end;
  746. end;
  747. end
  748. else
  749. { it's always not bad }
  750. genlinearlist(labels);
  751. end;
  752. { estimates the repeat of each instruction }
  753. oldexecutionweight:=cg.executionweight;
  754. cg.executionweight:=cg.executionweight div case_count_labels(labels);
  755. if cg.executionweight<1 then
  756. cg.executionweight:=1;
  757. { generate the instruction blocks }
  758. for i:=0 to blocks.count-1 do
  759. begin
  760. current_asmdata.CurrAsmList.concat(cai_align.create(current_settings.alignment.jumpalign));
  761. cg.a_label(current_asmdata.CurrAsmList,pcaseblock(blocks[i])^.blocklabel);
  762. secondpass(pcaseblock(blocks[i])^.statement);
  763. { don't come back to case line }
  764. current_filepos:=current_asmdata.CurrAsmList.getlasttaifilepos^;
  765. {$ifdef OLDREGVARS}
  766. load_all_regvars(current_asmdata.CurrAsmList);
  767. {$endif OLDREGVARS}
  768. hlcg.a_jmp_always(current_asmdata.CurrAsmList,endlabel);
  769. end;
  770. current_asmdata.CurrAsmList.concat(cai_align.create(current_settings.alignment.jumpalign));
  771. { ...and the else block }
  772. hlcg.a_label(current_asmdata.CurrAsmList,elselabel);
  773. if assigned(elseblock) then
  774. begin
  775. secondpass(elseblock);
  776. {$ifdef OLDREGVARS}
  777. load_all_regvars(current_asmdata.CurrAsmList);
  778. {$endif OLDREGVARS}
  779. end;
  780. cg.executionweight:=oldexecutionweight;
  781. current_asmdata.CurrAsmList.concat(cai_align.create(current_settings.alignment.jumpalign));
  782. hlcg.a_label(current_asmdata.CurrAsmList,endlabel);
  783. { Reset labels }
  784. for i:=0 to blocks.count-1 do
  785. pcaseblock(blocks[i])^.blocklabel:=nil;
  786. flowcontrol := oldflowcontrol + (flowcontrol - [fc_inflowcontrol]);
  787. end;
  788. begin
  789. csetelementnode:=tcgsetelementnode;
  790. cinnode:=tcginnode;
  791. ccasenode:=tcgcasenode;
  792. end.