ncgset.pas 39 KB

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