ncgset.pas 40 KB

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