ncgset.pas 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364
  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 }
  33. tcginnode = class(tinnode)
  34. procedure in_smallset(opdef: tdef; setbase: aint); virtual;
  35. function pass_1: tnode;override;
  36. procedure pass_generate_code;override;
  37. protected
  38. function checkgenjumps(out setparts: Tsetparts; out numparts: byte; out use_small: boolean): boolean; virtual;
  39. function analizeset(const Aset:Tconstset;out setparts: Tsetparts; out numparts: byte;is_small:boolean):boolean;virtual;
  40. end;
  41. tcgcasenode = class(tcasenode)
  42. {
  43. Emits the case node statement. Contrary to the intel
  44. 80x86 version, this version does not emit jump tables,
  45. because of portability problems.
  46. }
  47. procedure pass_generate_code;override;
  48. protected
  49. with_sign : boolean;
  50. opsize : tdef;
  51. jmp_gt,jmp_lt,jmp_le : topcmp;
  52. { register with case expression }
  53. hregister,hregister2 : tregister;
  54. endlabel,elselabel : tasmlabel;
  55. { true, if we can omit the range check of the jump table }
  56. jumptable_no_range : boolean;
  57. { has the implementation jumptable support }
  58. min_label : tconstexprint;
  59. { Number of labels }
  60. labelcnt: TCgInt;
  61. { Number of individual values checked, counting each value in a range
  62. individually (e.g. 0..2 counts as 3). }
  63. TrueCount: TCgInt;
  64. function GetBranchLabel(Block: TNode; out _Label: TAsmLabel): Boolean;
  65. function blocklabel(id:longint):tasmlabel;
  66. procedure optimizevalues(var max_linear_list:aint;var max_dist:aword);virtual;
  67. function has_jumptable : boolean;virtual;
  68. procedure genjumptable(hp : pcaselabel;min_,max_ : aint); virtual;
  69. procedure genlinearlist(hp : pcaselabel); virtual;
  70. procedure genlinearcmplist(hp : pcaselabel); virtual;
  71. procedure genjmptreeentry(p : pcaselabel;parentvalue : TConstExprInt); virtual;
  72. procedure genjmptree(root : pcaselabel); virtual;
  73. end;
  74. implementation
  75. uses
  76. verbose,
  77. cutils,
  78. symconst,symdef,symsym,defutil,
  79. pass_2,tgobj,
  80. nbas,ncon,ncgflw,
  81. ncgutil,hlcgobj;
  82. {*****************************************************************************
  83. TCGSETELEMENTNODE
  84. *****************************************************************************}
  85. procedure tcgsetelementnode.pass_generate_code;
  86. begin
  87. { load the set element's value }
  88. secondpass(left);
  89. { also a second value ? }
  90. if assigned(right) then
  91. internalerror(2015111106);
  92. { we don't modify the left side, we only check the location type; our
  93. parent node (an add-node) will use the resulting location to perform
  94. the set operation without creating an intermediate set }
  95. location_copy(location,left.location);
  96. end;
  97. {*****************************************************************************
  98. *****************************************************************************}
  99. function tcginnode.analizeset(const Aset:Tconstset; out setparts:tsetparts; out numparts: byte; is_small:boolean):boolean;
  100. var
  101. compares,maxcompares:word;
  102. i:byte;
  103. begin
  104. analizeset:=false;
  105. fillchar(setparts,sizeof(setparts),0);
  106. numparts:=0;
  107. compares:=0;
  108. { Lots of comparisions take a lot of time, so do not allow
  109. too much comparisions. 8 comparisions are, however, still
  110. smalller than emitting the set }
  111. if cs_opt_size in current_settings.optimizerswitches then
  112. maxcompares:=8
  113. else
  114. maxcompares:=5;
  115. { when smallset is possible allow only 3 compares the smallset
  116. code is for littlesize also smaller when more compares are used }
  117. if is_small then
  118. maxcompares:=3;
  119. for i:=0 to 255 do
  120. if i in Aset then
  121. begin
  122. if (numparts=0) or (i<>setparts[numparts].stop+1) then
  123. begin
  124. {Set element is a separate element.}
  125. inc(compares);
  126. if compares>maxcompares then
  127. exit;
  128. inc(numparts);
  129. setparts[numparts].range:=false;
  130. setparts[numparts].stop:=i;
  131. end
  132. else
  133. {Set element is part of a range.}
  134. if not setparts[numparts].range then
  135. begin
  136. {Transform an element into a range.}
  137. setparts[numparts].range:=true;
  138. setparts[numparts].start:=setparts[numparts].stop;
  139. setparts[numparts].stop:=i;
  140. { there's only one compare per range anymore. Only a }
  141. { sub is added, but that's much faster than a }
  142. { cmp/jcc combo so neglect its effect }
  143. { inc(compares);
  144. if compares>maxcompares then
  145. exit; }
  146. end
  147. else
  148. begin
  149. {Extend a range.}
  150. setparts[numparts].stop:=i;
  151. end;
  152. end;
  153. analizeset:=true;
  154. end;
  155. procedure tcginnode.in_smallset(opdef: tdef; setbase: aint);
  156. begin
  157. { location is always LOC_REGISTER }
  158. location_reset(location, LOC_REGISTER, def_cgsize(resultdef));
  159. { allocate a register for the result }
  160. location.register := hlcg.getintregister(current_asmdata.CurrAsmList, resultdef);
  161. {**************************** SMALL SET **********************}
  162. if left.location.loc=LOC_CONSTANT then
  163. begin
  164. hlcg.a_bit_test_const_loc_reg(current_asmdata.CurrAsmList,
  165. right.resultdef, resultdef,
  166. left.location.value-setbase, right.location,
  167. location.register);
  168. end
  169. else
  170. begin
  171. hlcg.location_force_reg(current_asmdata.CurrAsmList, left.location,
  172. left.resultdef, opdef, true);
  173. register_maybe_adjust_setbase(current_asmdata.CurrAsmList, opdef, left.location,
  174. setbase);
  175. hlcg.a_bit_test_reg_loc_reg(current_asmdata.CurrAsmList, opdef,
  176. right.resultdef, resultdef, left.location.register, right.location,
  177. location.register);
  178. end;
  179. end;
  180. function tcginnode.checkgenjumps(out setparts: Tsetparts; out numparts: byte;out use_small: boolean): boolean;
  181. begin
  182. { check if we can use smallset operation using btl which is limited
  183. to 32 bits, the left side may also not contain higher values !! }
  184. use_small:=is_smallset(right.resultdef) and
  185. not is_signed(left.resultdef) and
  186. ((left.resultdef.typ=orddef) and (torddef(left.resultdef).high<32) or
  187. (left.resultdef.typ=enumdef) and (tenumdef(left.resultdef).max<32));
  188. { Can we generate jumps? Possible for all types of sets }
  189. checkgenjumps:=(right.nodetype=setconstn) and
  190. analizeset(Tsetconstnode(right).value_set^,setparts,numparts,use_small);
  191. end;
  192. function tcginnode.pass_1: tnode;
  193. var
  194. setparts: Tsetparts;
  195. numparts: byte;
  196. use_small: boolean;
  197. begin
  198. result := inherited pass_1;
  199. if not(assigned(result)) and
  200. checkgenjumps(setparts,numparts,use_small) then
  201. expectloc := LOC_JUMP;
  202. end;
  203. procedure tcginnode.pass_generate_code;
  204. var
  205. adjustment,
  206. setbase : {$ifdef CPU8BITALU}smallint{$else}aint{$endif};
  207. l, l2 : tasmlabel;
  208. hr,
  209. pleftreg : tregister;
  210. setparts : Tsetparts;
  211. opsize : tcgsize;
  212. opdef : tdef;
  213. uopsize : tcgsize;
  214. uopdef : tdef;
  215. orgopsize : tcgsize;
  216. genjumps,
  217. use_small : boolean;
  218. i,numparts : byte;
  219. needslabel : Boolean;
  220. begin
  221. l2:=nil;
  222. { We check first if we can generate jumps, this can be done
  223. because the resultdef is already set in firstpass }
  224. genjumps := checkgenjumps(setparts,numparts,use_small);
  225. orgopsize := def_cgsize(left.resultdef);
  226. {$if defined(cpu8bitalu)}
  227. if (tsetdef(right.resultdef).setbase>=-128) and
  228. (tsetdef(right.resultdef).setmax-tsetdef(right.resultdef).setbase+1<=256) then
  229. begin
  230. uopsize := OS_8;
  231. uopdef := u8inttype;
  232. if is_signed(left.resultdef) then
  233. begin
  234. opsize := OS_S8;
  235. opdef := s8inttype;
  236. end
  237. else
  238. begin
  239. opsize := uopsize;
  240. opdef := uopdef;
  241. end;
  242. end
  243. {$endif defined(cpu8bitalu)}
  244. {$if defined(cpu8bitalu)}
  245. { this should be also enabled for 16 bit CPUs, however, I have no proper testing facility for 16 bit, my
  246. testing results using Dosbox are no reliable }
  247. { $if defined(cpu8bitalu) or defined(cpu16bitalu)}
  248. else if (tsetdef(right.resultdef).setbase>=-32768) and
  249. (tsetdef(right.resultdef).setmax-tsetdef(right.resultdef).setbase+1<=65536) then
  250. begin
  251. uopsize := OS_16;
  252. uopdef := u16inttype;
  253. if is_signed(left.resultdef) then
  254. begin
  255. opsize := OS_S16;
  256. opdef := s16inttype;
  257. end
  258. else
  259. begin
  260. opsize := uopsize;
  261. opdef := uopdef;
  262. end;
  263. end
  264. else
  265. {$endif defined(cpu8bitalu)}
  266. begin
  267. uopsize := OS_32;
  268. uopdef := u32inttype;
  269. if is_signed(left.resultdef) then
  270. begin
  271. opsize := OS_S32;
  272. opdef := s32inttype;
  273. end
  274. else
  275. begin
  276. opsize := uopsize;
  277. opdef := uopdef;
  278. end;
  279. end;
  280. needslabel := false;
  281. if not genjumps then
  282. { calculate both operators }
  283. { the complex one first }
  284. { not in case of genjumps, because then we don't secondpass }
  285. { right at all (so we have to make sure that "right" really is }
  286. { "right" and not "swapped left" in that case) }
  287. firstcomplex(self);
  288. secondpass(left);
  289. if (left.expectloc=LOC_JUMP)<>
  290. (left.location.loc=LOC_JUMP) then
  291. internalerror(2007070101);
  292. { Only process the right if we are not generating jumps }
  293. if not genjumps then
  294. secondpass(right);
  295. if codegenerror then
  296. exit;
  297. { ofcourse not commutative }
  298. if nf_swapped in flags then
  299. swapleftright;
  300. setbase:=tsetdef(right.resultdef).setbase;
  301. if genjumps then
  302. begin
  303. { location is always LOC_JUMP }
  304. current_asmdata.getjumplabel(l);
  305. current_asmdata.getjumplabel(l2);
  306. location_reset_jump(location,l,l2);
  307. { If register is used, use only lower 8 bits }
  308. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,opdef,false);
  309. pleftreg := left.location.register;
  310. { how much have we already substracted from the x in the }
  311. { "x in [y..z]" expression }
  312. adjustment := 0;
  313. hr:=NR_NO;
  314. for i:=1 to numparts do
  315. if setparts[i].range then
  316. { use fact that a <= x <= b <=> aword(x-a) <= aword(b-a) }
  317. begin
  318. { is the range different from all legal values? }
  319. if (setparts[i].stop-setparts[i].start <> 255) or not (orgopsize = OS_8) then
  320. begin
  321. { yes, is the lower bound <> 0? }
  322. if (setparts[i].start <> 0) then
  323. { we're going to substract from the left register, }
  324. { so in case of a LOC_CREGISTER first move the value }
  325. { to edi (not done before because now we can do the }
  326. { move and substract in one instruction with LEA) }
  327. if (left.location.loc = LOC_CREGISTER) and
  328. (hr<>pleftreg) then
  329. begin
  330. { don't change this back to a_op_const_reg/a_load_reg_reg, since pleftreg must not be modified }
  331. hr:=hlcg.getintregister(current_asmdata.CurrAsmList,opdef);
  332. hlcg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,opdef,setparts[i].start,pleftreg,hr);
  333. pleftreg:=hr;
  334. end
  335. else
  336. begin
  337. { otherwise, the value is already in a register }
  338. { that can be modified }
  339. hlcg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SUB,opdef,
  340. setparts[i].start-adjustment,pleftreg)
  341. end;
  342. { new total value substracted from x: }
  343. { adjustment + (setparts[i].start - adjustment) }
  344. adjustment := setparts[i].start;
  345. { check if result < b-a+1 (not "result <= b-a", since }
  346. { we need a carry in case the element is in the range }
  347. { (this will never overflow since we check at the }
  348. { beginning whether stop-start <> 255) }
  349. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, uopdef, OC_B,
  350. setparts[i].stop-setparts[i].start+1,pleftreg,location.truelabel);
  351. end
  352. else
  353. { if setparts[i].start = 0 and setparts[i].stop = 255, }
  354. { it's always true since "in" is only allowed for bytes }
  355. begin
  356. hlcg.a_jmp_always(current_asmdata.CurrAsmList,location.truelabel);
  357. end;
  358. end
  359. else
  360. begin
  361. { Emit code to check if left is an element }
  362. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, opdef, OC_EQ,
  363. setparts[i].stop-adjustment,pleftreg,location.truelabel);
  364. end;
  365. { To compensate for not doing a second pass }
  366. right.location.reference.symbol:=nil;
  367. hlcg.a_jmp_always(current_asmdata.CurrAsmList,location.falselabel);
  368. end
  369. else
  370. {*****************************************************************}
  371. { NO JUMP TABLE GENERATION }
  372. {*****************************************************************}
  373. begin
  374. { We will now generated code to check the set itself, no jmps,
  375. handle smallsets separate, because it allows faster checks }
  376. if use_small then
  377. begin
  378. in_smallset(opdef, setbase);
  379. end
  380. else
  381. {************************** NOT SMALL SET ********************}
  382. begin
  383. { location is always LOC_REGISTER }
  384. location_reset(location, LOC_REGISTER, uopsize{def_cgsize(resultdef)});
  385. { allocate a register for the result }
  386. location.register := hlcg.getintregister(current_asmdata.CurrAsmList, uopdef);
  387. if right.location.loc=LOC_CONSTANT then
  388. begin
  389. { can it actually occur currently? CEC }
  390. { yes: "if bytevar in [1,3,5,7,9,11,13,15]" (JM) }
  391. { note: this code assumes that left in [0..255], which is a valid }
  392. { assumption (other cases will be caught by range checking) (JM) }
  393. { load left in register }
  394. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,uopdef,true);
  395. register_maybe_adjust_setbase(current_asmdata.CurrAsmList,uopdef,left.location,setbase);
  396. { emit bit test operation -- warning: do not use
  397. location_force_reg() to force a set into a register, except
  398. to a register of the same size as the set. The reason is
  399. that on big endian systems, this would require moving the
  400. set to the most significant part of the new register,
  401. and location_force_register can't do that (it does not
  402. know the type).
  403. a_bit_test_reg_loc_reg() properly takes into account the
  404. size of the set to adjust the register index to test }
  405. hlcg.a_bit_test_reg_loc_reg(current_asmdata.CurrAsmList,
  406. uopdef,right.resultdef,uopdef,
  407. left.location.register,right.location,location.register);
  408. { now zero the result if left > nr_of_bits_in_right_register }
  409. hr := hlcg.getintregister(current_asmdata.CurrAsmList,uopdef);
  410. { if left > tcgsize2size[opsize]*8 then hr := 0 else hr := $ffffffff }
  411. { (left.location.size = location.size at this point) }
  412. hlcg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SUB, uopdef, tcgsize2size[opsize]*8, left.location.register, hr);
  413. hlcg.a_op_const_reg(current_asmdata.CurrAsmList, OP_SAR, uopdef, (tcgsize2size[opsize]*8)-1, hr);
  414. { if left > tcgsize2size[opsize]*8-1, then result := 0 else result := result of bit test }
  415. hlcg.a_op_reg_reg(current_asmdata.CurrAsmList, OP_AND, uopdef, hr, location.register);
  416. end { of right.location.loc=LOC_CONSTANT }
  417. { do search in a normal set which could have >32 elements
  418. but also used if the left side contains higher values > 32 }
  419. else if (left.location.loc=LOC_CONSTANT) then
  420. begin
  421. if (left.location.value < setbase) or (((left.location.value-setbase) shr 3) >= right.resultdef.size) then
  422. {should be caught earlier }
  423. internalerror(2007020402);
  424. hlcg.a_bit_test_const_loc_reg(current_asmdata.CurrAsmList,right.resultdef,uopdef,left.location.value-setbase,
  425. right.location,location.register);
  426. end
  427. else
  428. begin
  429. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,opdef,true);
  430. register_maybe_adjust_setbase(current_asmdata.CurrAsmList,opdef,left.location,setbase);
  431. pleftreg := left.location.register;
  432. if (opsize >= OS_S8) or { = if signed }
  433. ((left.resultdef.typ=orddef) and
  434. ((torddef(left.resultdef).low < int64(tsetdef(right.resultdef).setbase)) or
  435. (torddef(left.resultdef).high > int64(tsetdef(right.resultdef).setmax)))) or
  436. ((left.resultdef.typ=enumdef) and
  437. ((tenumdef(left.resultdef).min < aint(tsetdef(right.resultdef).setbase)) or
  438. (tenumdef(left.resultdef).max > aint(tsetdef(right.resultdef).setmax)))) then
  439. begin
  440. current_asmdata.getjumplabel(l);
  441. current_asmdata.getjumplabel(l2);
  442. needslabel := True;
  443. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, opdef, OC_BE, tsetdef(right.resultdef).setmax-tsetdef(right.resultdef).setbase, pleftreg, l);
  444. hlcg.a_load_const_reg(current_asmdata.CurrAsmList, uopdef, 0, location.register);
  445. hlcg.a_jmp_always(current_asmdata.CurrAsmList, l2);
  446. hlcg.a_label(current_asmdata.CurrAsmList, l);
  447. end;
  448. hlcg.a_bit_test_reg_loc_reg(current_asmdata.CurrAsmList,opdef,right.resultdef,uopdef,
  449. pleftreg,right.location,location.register);
  450. if needslabel then
  451. hlcg.a_label(current_asmdata.CurrAsmList, l2);
  452. end;
  453. {$ifndef cpuhighleveltarget}
  454. location.size := def_cgsize(resultdef);
  455. location.register := cg.makeregsize(current_asmdata.CurrAsmList, location.register, location.size);
  456. {$else not cpuhighleveltarget}
  457. hr:=hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  458. hlcg.a_load_reg_reg(current_asmdata.CurrAsmList,uopdef,resultdef,location.register,hr);
  459. location.register:=hr;
  460. location.size := def_cgsize(resultdef);
  461. {$endif not cpuhighleveltarget}
  462. end;
  463. end;
  464. location_freetemp(current_asmdata.CurrAsmList, right.location);
  465. end;
  466. {*****************************************************************************
  467. TCGCASENODE
  468. *****************************************************************************}
  469. { Analyse the nodes following the else label - if empty, change to end label }
  470. function tcgcasenode.GetBranchLabel(Block: TNode; out _Label: TAsmLabel): Boolean;
  471. var
  472. LabelSym: TLabelSym;
  473. begin
  474. Result := True;
  475. if not Assigned(Block) then
  476. begin
  477. { Block doesn't exist / is empty }
  478. _Label := endlabel;
  479. Exit;
  480. end;
  481. { These optimisations aren't particularly debugger friendly }
  482. if not (cs_opt_level2 in current_settings.optimizerswitches) then
  483. begin
  484. Result := False;
  485. current_asmdata.getjumplabel(_Label);
  486. Exit;
  487. end;
  488. while Assigned(Block) do
  489. begin
  490. case Block.nodetype of
  491. nothingn:
  492. begin
  493. _Label := endlabel;
  494. Exit;
  495. end;
  496. goton:
  497. begin
  498. LabelSym := TCGGotoNode(Block).labelsym;
  499. if not Assigned(LabelSym) then
  500. InternalError(2018121131);
  501. _Label := TCGLabelNode(TCGGotoNode(Block).labelnode).getasmlabel;
  502. if Assigned(_Label) then
  503. { Keep tabs on the fact that an actual 'goto' was used }
  504. Include(flowcontrol,fc_gotolabel)
  505. else
  506. Break;
  507. Exit;
  508. end;
  509. blockn:
  510. begin
  511. Block := TBlockNode(Block).Left;
  512. Continue;
  513. end;
  514. statementn:
  515. begin
  516. { If the right node is assigned, then it's a compound block
  517. that can't be simplified, so fall through, set Result to
  518. False and make a new label }
  519. if Assigned(TStatementNode(Block).right) then
  520. Break;
  521. Block := TStatementNode(Block).Left;
  522. Continue;
  523. end;
  524. end;
  525. Break;
  526. end;
  527. { Create unique label }
  528. Result := False;
  529. current_asmdata.getjumplabel(_Label);
  530. end;
  531. function tcgcasenode.blocklabel(id:longint):tasmlabel;
  532. begin
  533. if not assigned(blocks[id]) then
  534. internalerror(200411301);
  535. result:=pcaseblock(blocks[id])^.blocklabel;
  536. end;
  537. procedure tcgcasenode.optimizevalues(var max_linear_list:aint;var max_dist:aword);
  538. begin
  539. { no changes by default }
  540. end;
  541. function tcgcasenode.has_jumptable : boolean;
  542. begin
  543. { No jumptable support in the default implementation }
  544. has_jumptable:=false;
  545. end;
  546. procedure tcgcasenode.genjumptable(hp : pcaselabel;min_,max_ : aint);
  547. begin
  548. internalerror(200209161);
  549. end;
  550. procedure tcgcasenode.genlinearlist(hp : pcaselabel);
  551. var
  552. first : boolean;
  553. last : TConstExprInt;
  554. scratch_reg: tregister;
  555. newsize: tcgsize;
  556. newdef: tdef;
  557. procedure gensub(value:tcgint);
  558. begin
  559. { here, since the sub and cmp are separate we need
  560. to move the result before subtract to help
  561. the register allocator
  562. }
  563. hlcg.a_load_reg_reg(current_asmdata.CurrAsmList, opsize, opsize, hregister, scratch_reg);
  564. hlcg.a_op_const_reg(current_asmdata.CurrAsmList, OP_SUB, opsize, value, hregister);
  565. end;
  566. procedure genitem(t : pcaselabel);
  567. begin
  568. if assigned(t^.less) then
  569. genitem(t^.less);
  570. { do we need to test the first value? }
  571. if first and (t^._low>get_min_value(left.resultdef)) then
  572. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,jmp_lt,tcgint(t^._low.svalue),hregister,elselabel);
  573. if t^._low=t^._high then
  574. begin
  575. if t^._low-last=0 then
  576. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,OC_EQ,0,hregister,blocklabel(t^.blockid))
  577. else
  578. begin
  579. gensub(tcgint(t^._low.svalue-last.svalue));
  580. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,
  581. OC_EQ,tcgint(t^._low.svalue-last.svalue),scratch_reg,blocklabel(t^.blockid));
  582. end;
  583. last:=t^._low;
  584. end
  585. else
  586. begin
  587. { it begins with the smallest label, if the value }
  588. { is even smaller then jump immediately to the }
  589. { ELSE-label }
  590. if first then
  591. begin
  592. { have we to ajust the first value ? }
  593. if (t^._low>get_min_value(left.resultdef)) or (get_min_value(left.resultdef)<>0) then
  594. gensub(tcgint(t^._low.svalue));
  595. end
  596. else
  597. begin
  598. { if there is no unused label between the last and the }
  599. { present label then the lower limit can be checked }
  600. { immediately. else check the range in between: }
  601. gensub(tcgint(t^._low.svalue-last.svalue));
  602. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, opsize,jmp_lt,tcgint(t^._low.svalue-last.svalue),scratch_reg,elselabel);
  603. end;
  604. gensub(tcgint(t^._high.svalue-t^._low.svalue));
  605. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,jmp_le,tcgint(t^._high.svalue-t^._low.svalue),scratch_reg,blocklabel(t^.blockid));
  606. last:=t^._high;
  607. end;
  608. first:=false;
  609. if assigned(t^.greater) then
  610. genitem(t^.greater);
  611. end;
  612. begin
  613. { do we need to generate cmps? }
  614. if (with_sign and (min_label<0)) then
  615. genlinearcmplist(hp)
  616. else
  617. begin
  618. { sign/zero extend the value to a full register before starting to
  619. subtract values, so that on platforms that don't have
  620. subregisters of the same size as the value we don't generate
  621. sign/zero-extensions after every subtraction
  622. make newsize always signed, since we only do this if the size in
  623. bytes of the register is larger than the original opsize, so
  624. the value can always be represented by a larger signed type }
  625. newsize:=tcgsize2signed[reg_cgsize(hregister)];
  626. if tcgsize2size[newsize]>opsize.size then
  627. begin
  628. newdef:=cgsize_orddef(newsize);
  629. scratch_reg:=hlcg.getintregister(current_asmdata.CurrAsmList,newdef);
  630. hlcg.a_load_reg_reg(current_asmdata.CurrAsmList,opsize,newdef,hregister,scratch_reg);
  631. hregister:=scratch_reg;
  632. opsize:=newdef;
  633. end;
  634. if (labelcnt>1) or not(cs_opt_level1 in current_settings.optimizerswitches) then
  635. begin
  636. last:=0;
  637. first:=true;
  638. scratch_reg:=hlcg.getintregister(current_asmdata.CurrAsmList,opsize);
  639. genitem(hp);
  640. end
  641. else
  642. begin
  643. { If only one label exists, we can greatly simplify the checks to a simple comparison }
  644. if hp^._low=hp^._high then
  645. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, opsize, OC_EQ, tcgint(hp^._low.svalue), hregister, blocklabel(hp^.blockid))
  646. else
  647. begin
  648. scratch_reg:=hlcg.getintregister(current_asmdata.CurrAsmList,opsize);
  649. gensub(tcgint(hp^._low.svalue));
  650. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, opsize, jmp_le, tcgint(hp^._high.svalue-hp^._low.svalue), hregister, blocklabel(hp^.blockid))
  651. end;
  652. end;
  653. hlcg.a_jmp_always(current_asmdata.CurrAsmList,elselabel);
  654. end;
  655. end;
  656. procedure tcgcasenode.genlinearcmplist(hp : pcaselabel);
  657. var
  658. last : TConstExprInt;
  659. lastwasrange: boolean;
  660. procedure genitem(t : pcaselabel);
  661. {$ifndef cpu64bitalu}
  662. var
  663. l1 : tasmlabel;
  664. {$endif not cpu64bitalu}
  665. begin
  666. if assigned(t^.less) then
  667. genitem(t^.less);
  668. if t^._low=t^._high then
  669. begin
  670. {$if defined(cpu32bitalu)}
  671. if def_cgsize(opsize) in [OS_S64,OS_64] then
  672. begin
  673. current_asmdata.getjumplabel(l1);
  674. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_32, OC_NE, aint(hi(int64(t^._low.svalue))),hregister2,l1);
  675. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_32, OC_EQ, aint(lo(int64(t^._low.svalue))),hregister, blocklabel(t^.blockid));
  676. cg.a_label(current_asmdata.CurrAsmList,l1);
  677. end
  678. else
  679. {$elseif defined(cpu16bitalu)}
  680. if def_cgsize(opsize) in [OS_S64,OS_64] then
  681. begin
  682. current_asmdata.getjumplabel(l1);
  683. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, OC_NE, aint(hi(hi(int64(t^._low.svalue)))),cg.GetNextReg(hregister2),l1);
  684. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, OC_NE, aint(lo(hi(int64(t^._low.svalue)))),hregister2,l1);
  685. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, OC_NE, aint(hi(lo(int64(t^._low.svalue)))),cg.GetNextReg(hregister),l1);
  686. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, OC_EQ, aint(lo(lo(int64(t^._low.svalue)))),hregister, blocklabel(t^.blockid));
  687. cg.a_label(current_asmdata.CurrAsmList,l1);
  688. end
  689. else if def_cgsize(opsize) in [OS_S32,OS_32] then
  690. begin
  691. current_asmdata.getjumplabel(l1);
  692. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, OC_NE, aint(hi(int32(t^._low.svalue))),cg.GetNextReg(hregister),l1);
  693. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, OC_EQ, aint(lo(int32(t^._low.svalue))),hregister, blocklabel(t^.blockid));
  694. cg.a_label(current_asmdata.CurrAsmList,l1);
  695. end
  696. else
  697. {$elseif defined(cpu8bitalu)}
  698. if def_cgsize(opsize) in [OS_S64,OS_64] then
  699. begin
  700. current_asmdata.getjumplabel(l1);
  701. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_8, OC_NE, aint(hi(hi(hi(int64(t^._low.svalue))))),cg.GetNextReg(cg.GetNextReg(cg.GetNextReg(hregister2))),l1);
  702. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_8, OC_NE, aint(lo(hi(hi(int64(t^._low.svalue))))),cg.GetNextReg(cg.GetNextReg(hregister2)),l1);
  703. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_8, OC_NE, aint(hi(lo(hi(int64(t^._low.svalue))))),cg.GetNextReg(hregister2),l1);
  704. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_8, OC_NE, aint(lo(lo(hi(int64(t^._low.svalue))))),hregister2,l1);
  705. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_8, OC_NE, aint(hi(hi(lo(int64(t^._low.svalue))))),cg.GetNextReg(cg.GetNextReg(cg.GetNextReg(hregister))),l1);
  706. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_8, OC_NE, aint(lo(hi(lo(int64(t^._low.svalue))))),cg.GetNextReg(cg.GetNextReg(hregister)),l1);
  707. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_8, OC_NE, aint(hi(lo(lo(int64(t^._low.svalue))))),cg.GetNextReg(hregister),l1);
  708. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_8, OC_EQ, aint(lo(lo(lo(int64(t^._low.svalue))))),hregister,blocklabel(t^.blockid));
  709. cg.a_label(current_asmdata.CurrAsmList,l1);
  710. end
  711. else if def_cgsize(opsize) in [OS_S32,OS_32] then
  712. begin
  713. current_asmdata.getjumplabel(l1);
  714. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_8, OC_NE, aint(hi(hi(int32(t^._low.svalue)))),cg.GetNextReg(cg.GetNextReg(cg.GetNextReg(hregister))),l1);
  715. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_8, OC_NE, aint(lo(hi(int32(t^._low.svalue)))),cg.GetNextReg(cg.GetNextReg(hregister)),l1);
  716. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_8, OC_NE, aint(hi(lo(int32(t^._low.svalue)))),cg.GetNextReg(hregister),l1);
  717. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_8, OC_EQ, aint(lo(lo(int32(t^._low.svalue)))),hregister, blocklabel(t^.blockid));
  718. cg.a_label(current_asmdata.CurrAsmList,l1);
  719. end
  720. else if def_cgsize(opsize) in [OS_S16,OS_16] then
  721. begin
  722. current_asmdata.getjumplabel(l1);
  723. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_8, OC_NE, aint(hi(int16(t^._low.svalue))),cg.GetNextReg(hregister),l1);
  724. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_8, OC_EQ, aint(lo(int16(t^._low.svalue))),hregister, blocklabel(t^.blockid));
  725. cg.a_label(current_asmdata.CurrAsmList,l1);
  726. end
  727. else
  728. {$endif}
  729. begin
  730. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, opsize, OC_EQ, aint(t^._low.svalue),hregister, blocklabel(t^.blockid));
  731. end;
  732. { Reset last here, because we've only checked for one value and need to compare
  733. for the next range both the lower and upper bound }
  734. lastwasrange := false;
  735. end
  736. else
  737. begin
  738. { it begins with the smallest label, if the value }
  739. { is even smaller then jump immediately to the }
  740. { ELSE-label }
  741. if not lastwasrange or (t^._low-last>1) then
  742. begin
  743. {$if defined(cpu32bitalu)}
  744. if def_cgsize(opsize) in [OS_64,OS_S64] then
  745. begin
  746. current_asmdata.getjumplabel(l1);
  747. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_32, jmp_lt, aint(hi(int64(t^._low.svalue))),
  748. hregister2, elselabel);
  749. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_32, jmp_gt, aint(hi(int64(t^._low.svalue))),
  750. hregister2, l1);
  751. { the comparisation of the low dword must be always unsigned! }
  752. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_32, OC_B, aint(lo(int64(t^._low.svalue))), hregister, elselabel);
  753. cg.a_label(current_asmdata.CurrAsmList,l1);
  754. end
  755. else
  756. {$elseif defined(cpu16bitalu)}
  757. if def_cgsize(opsize) in [OS_64,OS_S64] then
  758. begin
  759. current_asmdata.getjumplabel(l1);
  760. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, jmp_lt, aint(hi(hi(int64(t^._low.svalue)))),
  761. cg.GetNextReg(hregister2), elselabel);
  762. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, jmp_gt, aint(hi(hi(int64(t^._low.svalue)))),
  763. cg.GetNextReg(hregister2), l1);
  764. { the comparison of the low words must be always unsigned! }
  765. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, OC_B, aint(lo(hi(int64(t^._low.svalue)))),
  766. hregister2, elselabel);
  767. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, OC_A, aint(lo(hi(int64(t^._low.svalue)))),
  768. hregister2, l1);
  769. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, OC_B, aint(hi(lo(int64(t^._low.svalue)))),
  770. cg.GetNextReg(hregister), elselabel);
  771. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, OC_A, aint(hi(lo(int64(t^._low.svalue)))),
  772. cg.GetNextReg(hregister), l1);
  773. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, OC_B, aint(lo(lo(int64(t^._low.svalue)))), hregister, elselabel);
  774. cg.a_label(current_asmdata.CurrAsmList,l1);
  775. end
  776. else if def_cgsize(opsize) in [OS_32,OS_S32] then
  777. begin
  778. current_asmdata.getjumplabel(l1);
  779. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, jmp_lt, aint(hi(int32(t^._low.svalue))),
  780. cg.GetNextReg(hregister), elselabel);
  781. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, jmp_gt, aint(hi(int32(t^._low.svalue))),
  782. cg.GetNextReg(hregister), l1);
  783. { the comparisation of the low dword must be always unsigned! }
  784. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, OC_B, aint(lo(int32(t^._low.svalue))), hregister, elselabel);
  785. cg.a_label(current_asmdata.CurrAsmList,l1);
  786. end
  787. else
  788. {$elseif defined(cpu8bitalu)}
  789. if def_cgsize(opsize) in [OS_64,OS_S64] then
  790. begin
  791. current_asmdata.getjumplabel(l1);
  792. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,jmp_lt,aint(hi(hi(hi(int64(t^._low.svalue))))),cg.GetNextReg(cg.GetNextReg(cg.GetNextReg(hregister2))),elselabel);
  793. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,jmp_gt,aint(hi(hi(hi(int64(t^._low.svalue))))),cg.GetNextReg(cg.GetNextReg(cg.GetNextReg(hregister2))),l1);
  794. { the comparison of the low words must be always unsigned! }
  795. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_B,aint(lo(hi(hi(int64(t^._low.svalue))))),cg.GetNextReg(cg.GetNextReg(hregister2)),elselabel);
  796. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_A,aint(lo(hi(hi(int64(t^._low.svalue))))),cg.GetNextReg(cg.GetNextReg(hregister2)),l1);
  797. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_B,aint(hi(lo(hi(int64(t^._low.svalue))))),cg.GetNextReg(hregister2),elselabel);
  798. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_A,aint(hi(lo(hi(int64(t^._low.svalue))))),cg.GetNextReg(hregister2),l1);
  799. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_B,aint(lo(lo(hi(int64(t^._low.svalue))))),hregister2,elselabel);
  800. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_A,aint(lo(lo(hi(int64(t^._low.svalue))))),hregister2,l1);
  801. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_B,aint(hi(hi(lo(int64(t^._low.svalue))))),cg.GetNextReg(cg.GetNextReg(cg.GetNextReg(hregister))),elselabel);
  802. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_A,aint(hi(hi(lo(int64(t^._low.svalue))))),cg.GetNextReg(cg.GetNextReg(cg.GetNextReg(hregister))),l1);
  803. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_B,aint(lo(hi(lo(int64(t^._low.svalue))))),cg.GetNextReg(cg.GetNextReg(hregister)),elselabel);
  804. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_A,aint(lo(hi(lo(int64(t^._low.svalue))))),cg.GetNextReg(cg.GetNextReg(hregister)),l1);
  805. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_B,aint(hi(lo(lo(int64(t^._low.svalue))))),cg.GetNextReg(hregister),elselabel);
  806. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_A,aint(hi(lo(lo(int64(t^._low.svalue))))),cg.GetNextReg(hregister),l1);
  807. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_B,aint(lo(lo(lo(int64(t^._low.svalue))))),hregister,elselabel);
  808. cg.a_label(current_asmdata.CurrAsmList,l1);
  809. end
  810. else if def_cgsize(opsize) in [OS_32,OS_S32] then
  811. begin
  812. current_asmdata.getjumplabel(l1);
  813. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,jmp_lt,aint(hi(hi(int32(t^._low.svalue)))),cg.GetNextReg(cg.GetNextReg(cg.GetNextReg(hregister))),elselabel);
  814. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_8,jmp_gt,aint(hi(hi(int32(t^._low.svalue)))),cg.GetNextReg(cg.GetNextReg(cg.GetNextReg(hregister))),l1);
  815. { the comparison of the low words must be always unsigned! }
  816. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_B,aint(lo(hi(int32(t^._low.svalue)))),cg.GetNextReg(cg.GetNextReg(hregister)),elselabel);
  817. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_A,aint(lo(hi(int32(t^._low.svalue)))),cg.GetNextReg(cg.GetNextReg(hregister)),l1);
  818. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_B,aint(hi(lo(int32(t^._low.svalue)))),cg.GetNextReg(hregister),elselabel);
  819. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_A,aint(hi(lo(int32(t^._low.svalue)))),cg.GetNextReg(hregister),l1);
  820. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_B,aint(lo(lo(int32(t^._low.svalue)))),hregister,elselabel);
  821. cg.a_label(current_asmdata.CurrAsmList,l1);
  822. end
  823. else if def_cgsize(opsize) in [OS_16,OS_S16] then
  824. begin
  825. current_asmdata.getjumplabel(l1);
  826. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,jmp_lt,aint(hi(int16(t^._low.svalue))),cg.GetNextReg(hregister),elselabel);
  827. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,jmp_gt,aint(hi(int16(t^._low.svalue))),cg.GetNextReg(hregister),l1);
  828. { the comparisation of the low dword must be always unsigned! }
  829. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_B,aint(lo(int16(t^._low.svalue))),hregister,elselabel);
  830. cg.a_label(current_asmdata.CurrAsmList,l1);
  831. end
  832. else
  833. {$endif}
  834. begin
  835. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, opsize, jmp_lt, aint(t^._low.svalue), hregister,
  836. elselabel);
  837. end;
  838. end;
  839. {$if defined(cpu32bitalu)}
  840. if def_cgsize(opsize) in [OS_S64,OS_64] then
  841. begin
  842. current_asmdata.getjumplabel(l1);
  843. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_32, jmp_lt, aint(hi(int64(t^._high.svalue))), hregister2,
  844. blocklabel(t^.blockid));
  845. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_32, jmp_gt, aint(hi(int64(t^._high.svalue))), hregister2,
  846. l1);
  847. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_32, OC_BE, aint(lo(int64(t^._high.svalue))), hregister, blocklabel(t^.blockid));
  848. cg.a_label(current_asmdata.CurrAsmList,l1);
  849. end
  850. else
  851. {$elseif defined(cpu16bitalu)}
  852. if def_cgsize(opsize) in [OS_S64,OS_64] then
  853. begin
  854. current_asmdata.getjumplabel(l1);
  855. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, jmp_lt, aint(hi(hi(int64(t^._high.svalue)))), cg.GetNextReg(hregister2),
  856. blocklabel(t^.blockid));
  857. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, jmp_gt, aint(hi(hi(int64(t^._high.svalue)))), cg.GetNextReg(hregister2),
  858. l1);
  859. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, OC_B, aint(lo(hi(int64(t^._high.svalue)))), hregister2,
  860. blocklabel(t^.blockid));
  861. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, OC_A, aint(lo(hi(int64(t^._high.svalue)))), hregister2,
  862. l1);
  863. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, OC_B, aint(hi(lo(int64(t^._high.svalue)))), cg.GetNextReg(hregister),
  864. blocklabel(t^.blockid));
  865. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, OC_A, aint(hi(lo(int64(t^._high.svalue)))), cg.GetNextReg(hregister),
  866. l1);
  867. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, OC_BE, aint(lo(lo(int64(t^._high.svalue)))), hregister, blocklabel(t^.blockid));
  868. cg.a_label(current_asmdata.CurrAsmList,l1);
  869. end
  870. else if def_cgsize(opsize) in [OS_S32,OS_32] then
  871. begin
  872. current_asmdata.getjumplabel(l1);
  873. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, jmp_lt, aint(hi(int32(t^._high.svalue))), cg.GetNextReg(hregister),
  874. blocklabel(t^.blockid));
  875. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, jmp_gt, aint(hi(int32(t^._high.svalue))), cg.GetNextReg(hregister),
  876. l1);
  877. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, OS_16, OC_BE, aint(lo(int32(t^._high.svalue))), hregister, blocklabel(t^.blockid));
  878. cg.a_label(current_asmdata.CurrAsmList,l1);
  879. end
  880. else
  881. {$elseif defined(cpu8bitalu)}
  882. if def_cgsize(opsize) in [OS_S64,OS_64] then
  883. begin
  884. current_asmdata.getjumplabel(l1);
  885. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,jmp_lt,aint(hi(hi(hi(int64(t^._high.svalue))))),cg.GetNextReg(cg.GetNextReg(cg.GetNextReg(hregister2))),blocklabel(t^.blockid));
  886. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,jmp_gt,aint(hi(hi(hi(int64(t^._high.svalue))))),cg.GetNextReg(cg.GetNextReg(cg.GetNextReg(hregister2))),l1);
  887. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_B,aint(lo(hi(hi(int64(t^._high.svalue))))),cg.GetNextReg(cg.GetNextReg(hregister2)),blocklabel(t^.blockid));
  888. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_A,aint(lo(hi(hi(int64(t^._high.svalue))))),cg.GetNextReg(cg.GetNextReg(hregister2)),l1);
  889. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_B,aint(hi(lo(hi(int64(t^._high.svalue))))),cg.GetNextReg(hregister2),blocklabel(t^.blockid));
  890. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_A,aint(hi(lo(hi(int64(t^._high.svalue))))),cg.GetNextReg(hregister2),l1);
  891. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_B,aint(lo(lo(hi(int64(t^._high.svalue))))),hregister2,blocklabel(t^.blockid));
  892. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_A,aint(lo(lo(hi(int64(t^._high.svalue))))),hregister2,l1);
  893. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_B,aint(hi(hi(lo(int64(t^._high.svalue))))),cg.GetNextReg(cg.GetNextReg(cg.GetNextReg(hregister))),blocklabel(t^.blockid));
  894. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_A,aint(hi(hi(lo(int64(t^._high.svalue))))),cg.GetNextReg(cg.GetNextReg(cg.GetNextReg(hregister))),l1);
  895. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_B,aint(lo(hi(lo(int64(t^._high.svalue))))),cg.GetNextReg(cg.GetNextReg(hregister)),blocklabel(t^.blockid));
  896. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_A,aint(lo(hi(lo(int64(t^._high.svalue))))),cg.GetNextReg(cg.GetNextReg(hregister)),l1);
  897. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_B,aint(hi(lo(lo(int64(t^._high.svalue))))),cg.GetNextReg(hregister),blocklabel(t^.blockid));
  898. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_A,aint(hi(lo(lo(int64(t^._high.svalue))))),cg.GetNextReg(hregister),l1);
  899. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_BE,aint(lo(lo(lo(int64(t^._high.svalue))))),hregister,blocklabel(t^.blockid));
  900. cg.a_label(current_asmdata.CurrAsmList,l1);
  901. end
  902. else if def_cgsize(opsize) in [OS_S32,OS_32] then
  903. begin
  904. current_asmdata.getjumplabel(l1);
  905. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,jmp_lt,aint(hi(hi(int32(t^._high.svalue)))),cg.GetNextReg(cg.GetNextReg(cg.GetNextReg(hregister))),blocklabel(t^.blockid));
  906. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,jmp_gt,aint(hi(hi(int32(t^._high.svalue)))),cg.GetNextReg(cg.GetNextReg(cg.GetNextReg(hregister))),l1);
  907. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_B,aint(lo(hi(int32(t^._high.svalue)))),cg.GetNextReg(cg.GetNextReg(hregister)),blocklabel(t^.blockid));
  908. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_A,aint(lo(hi(int32(t^._high.svalue)))),cg.GetNextReg(cg.GetNextReg(hregister)),l1);
  909. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_B,aint(hi(lo(int32(t^._high.svalue)))),cg.GetNextReg(hregister),blocklabel(t^.blockid));
  910. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_A,aint(hi(lo(int32(t^._high.svalue)))),cg.GetNextReg(hregister),l1);
  911. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_BE,aint(lo(lo(int32(t^._high.svalue)))),hregister,blocklabel(t^.blockid));
  912. cg.a_label(current_asmdata.CurrAsmList,l1);
  913. end
  914. else if def_cgsize(opsize) in [OS_S16,OS_16] then
  915. begin
  916. current_asmdata.getjumplabel(l1);
  917. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,jmp_lt,aint(hi(int16(t^._high.svalue))),cg.GetNextReg(hregister),blocklabel(t^.blockid));
  918. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,jmp_gt,aint(hi(int16(t^._high.svalue))),cg.GetNextReg(hregister),l1);
  919. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_8,OC_BE,aint(lo(int16(t^._high.svalue))),hregister,blocklabel(t^.blockid));
  920. cg.a_label(current_asmdata.CurrAsmList,l1);
  921. end
  922. else
  923. {$endif}
  924. begin
  925. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, opsize, jmp_le, aint(t^._high.svalue), hregister, blocklabel(t^.blockid));
  926. end;
  927. last:=t^._high;
  928. lastwasrange := true;
  929. end;
  930. if assigned(t^.greater) then
  931. genitem(t^.greater);
  932. end;
  933. begin
  934. last:=0;
  935. lastwasrange:=false;
  936. genitem(hp);
  937. hlcg.a_jmp_always(current_asmdata.CurrAsmList,elselabel);
  938. end;
  939. procedure tcgcasenode.genjmptreeentry(p : pcaselabel;parentvalue : TConstExprInt);
  940. var
  941. lesslabel,greaterlabel : tasmlabel;
  942. begin
  943. current_asmdata.CurrAsmList.concat(cai_align.Create(current_settings.alignment.jumpalign));
  944. cg.a_label(current_asmdata.CurrAsmList,p^.labellabel);
  945. { calculate labels for left and right }
  946. if p^.less=nil then
  947. lesslabel:=elselabel
  948. else
  949. lesslabel:=p^.less^.labellabel;
  950. if p^.greater=nil then
  951. greaterlabel:=elselabel
  952. else
  953. greaterlabel:=p^.greater^.labellabel;
  954. { calculate labels for left and right }
  955. { no range label: }
  956. if p^._low=p^._high then
  957. begin
  958. if greaterlabel=lesslabel then
  959. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, opsize, OC_NE,p^._low,hregister, lesslabel)
  960. else
  961. begin
  962. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize, jmp_lt,p^._low,hregister, lesslabel);
  963. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize, jmp_gt,p^._low,hregister, greaterlabel);
  964. end;
  965. hlcg.a_jmp_always(current_asmdata.CurrAsmList,blocklabel(p^.blockid));
  966. end
  967. else
  968. begin
  969. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,jmp_lt,p^._low, hregister, lesslabel);
  970. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,jmp_gt,p^._high,hregister, greaterlabel);
  971. hlcg.a_jmp_always(current_asmdata.CurrAsmList,blocklabel(p^.blockid));
  972. end;
  973. if assigned(p^.less) then
  974. genjmptreeentry(p^.less,p^._low);
  975. if assigned(p^.greater) then
  976. genjmptreeentry(p^.greater,p^._high);
  977. end;
  978. procedure tcgcasenode.genjmptree(root : pcaselabel);
  979. type
  980. tlabelarrayentry = record
  981. caselabel : pcaselabel;
  982. asmlabel : TAsmLabel;
  983. end;
  984. tlabelarray = array of tlabelarrayentry;
  985. var
  986. labelarray : tlabelarray;
  987. var
  988. nextarrayentry : int64;
  989. i : longint;
  990. procedure addarrayentry(entry : pcaselabel);
  991. begin
  992. if assigned(entry^.less) then
  993. addarrayentry(entry^.less);
  994. with labelarray[nextarrayentry] do
  995. begin
  996. caselabel:=entry;
  997. current_asmdata.getjumplabel(asmlabel);
  998. end;
  999. inc(nextarrayentry);
  1000. if assigned(entry^.greater) then
  1001. addarrayentry(entry^.greater);
  1002. end;
  1003. { rebuild the label tree balanced }
  1004. procedure rebuild(first,last : int64;var p : pcaselabel);
  1005. var
  1006. current : int64;
  1007. begin
  1008. current:=(first+last) div 2;
  1009. p:=labelarray[current].caselabel;
  1010. if first<current then
  1011. rebuild(first,current-1,p^.less)
  1012. else
  1013. p^.less:=nil;
  1014. if last>current then
  1015. rebuild(current+1,last,p^.greater)
  1016. else
  1017. p^.greater:=nil;
  1018. end;
  1019. begin
  1020. labelarray:=nil;
  1021. SetLength(labelarray,case_count_labels(root));
  1022. nextarrayentry:=0;
  1023. addarrayentry(root);
  1024. rebuild(0,high(labelarray),root);
  1025. for i:=0 to high(labelarray) do
  1026. current_asmdata.getjumplabel(labelarray[i].caselabel^.labellabel);
  1027. genjmptreeentry(root,root^._high+10);
  1028. end;
  1029. procedure tcgcasenode.pass_generate_code;
  1030. { Combines "case_count_labels" and "case_true_count" }
  1031. procedure CountBoth(p : pcaselabel);
  1032. begin
  1033. Inc(labelcnt);
  1034. Inc(TrueCount, (p^._high.svalue - p^._low.svalue) + 1);
  1035. if assigned(p^.less) then
  1036. CountBoth(p^.less);
  1037. if assigned(p^.greater) then
  1038. CountBoth(p^.greater);
  1039. end;
  1040. var
  1041. oldflowcontrol: tflowcontrol;
  1042. i : longint;
  1043. dist : aword;
  1044. distv,
  1045. lv,hv,
  1046. max_label: tconstexprint;
  1047. max_linear_list : aint;
  1048. max_dist : aword;
  1049. ShortcutElse: Boolean;
  1050. begin
  1051. location_reset(location,LOC_VOID,OS_NO);
  1052. oldflowcontrol := flowcontrol;
  1053. include(flowcontrol,fc_inflowcontrol);
  1054. { Allocate labels }
  1055. current_asmdata.getjumplabel(endlabel);
  1056. { Do some optimisation to deal with empty else blocks }
  1057. ShortcutElse := GetBranchLabel(elseblock, elselabel);
  1058. for i:=0 to blocks.count-1 do
  1059. with pcaseblock(blocks[i])^ do
  1060. shortcut := GetBranchLabel(statement, blocklabel);
  1061. with_sign:=is_signed(left.resultdef);
  1062. if with_sign then
  1063. begin
  1064. jmp_gt:=OC_GT;
  1065. jmp_lt:=OC_LT;
  1066. jmp_le:=OC_LTE;
  1067. end
  1068. else
  1069. begin
  1070. jmp_gt:=OC_A;
  1071. jmp_lt:=OC_B;
  1072. jmp_le:=OC_BE;
  1073. end;
  1074. secondpass(left);
  1075. if (left.expectloc=LOC_JUMP)<>
  1076. (left.location.loc=LOC_JUMP) then
  1077. internalerror(2006050501);
  1078. { determines the size of the operand }
  1079. opsize:=left.resultdef;
  1080. { copy the case expression to a register }
  1081. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,opsize,false);
  1082. {$ifndef cpu64bitalu}
  1083. if def_cgsize(opsize) in [OS_S64,OS_64] then
  1084. begin
  1085. hregister:=left.location.register64.reglo;
  1086. hregister2:=left.location.register64.reghi;
  1087. end
  1088. else
  1089. {$endif not cpu64bitalu}
  1090. hregister:=left.location.register;
  1091. { we need the min_label always to choose between }
  1092. { cmps and subs/decs }
  1093. min_label:=case_get_min(labels);
  1094. { Generate the jumps }
  1095. {$ifdef OLDREGVARS}
  1096. load_all_regvars(current_asmdata.CurrAsmList);
  1097. {$endif OLDREGVARS}
  1098. {$ifndef cpu64bitalu}
  1099. if def_cgsize(opsize) in [OS_64,OS_S64] then
  1100. genlinearcmplist(labels)
  1101. else
  1102. {$endif not cpu64bitalu}
  1103. begin
  1104. labelcnt := 0;
  1105. TrueCount := 0;
  1106. if cs_opt_level1 in current_settings.optimizerswitches then
  1107. begin
  1108. { procedures are empirically passed on }
  1109. { consumption can also be calculated }
  1110. { but does it pay on the different }
  1111. { processors? }
  1112. { moreover can the size only be appro- }
  1113. { ximated as it is not known if rel8, }
  1114. { rel16 or rel32 jumps are used }
  1115. CountBoth(labels);
  1116. max_label := case_get_max(labels);
  1117. { can we omit the range check of the jump table ? }
  1118. getrange(left.resultdef,lv,hv);
  1119. jumptable_no_range:=(lv=min_label) and (hv=max_label);
  1120. distv:=max_label-min_label;
  1121. if distv>=0 then
  1122. dist:=distv.uvalue
  1123. else
  1124. dist:=aword(-distv.svalue);
  1125. { optimize for size ? }
  1126. if cs_opt_size in current_settings.optimizerswitches then
  1127. begin
  1128. if has_jumptable and
  1129. (min_label>=int64(low(aint))) and
  1130. (max_label<=high(aint)) and
  1131. not((labelcnt<=2) or
  1132. (distv.svalue<0) or
  1133. (dist>3*TrueCount)) then
  1134. begin
  1135. { if the labels less or more a continuum then }
  1136. genjumptable(labels,min_label.svalue,max_label.svalue);
  1137. end
  1138. else
  1139. begin
  1140. { a linear list is always smaller than a jump tree }
  1141. genlinearlist(labels);
  1142. end;
  1143. end
  1144. else
  1145. begin
  1146. max_dist:=4*TrueCount;
  1147. { Don't allow jump tables to get too large }
  1148. if max_dist>4*labelcnt then
  1149. max_dist:=min(max_dist,2048);
  1150. if jumptable_no_range then
  1151. max_linear_list:=4
  1152. else
  1153. max_linear_list:=2;
  1154. { allow processor specific values }
  1155. optimizevalues(max_linear_list,max_dist);
  1156. if (labelcnt<=max_linear_list) then
  1157. genlinearlist(labels)
  1158. else
  1159. begin
  1160. if (has_jumptable) and
  1161. (dist<max_dist) and
  1162. (min_label>=int64(low(aint))) and
  1163. (max_label<=high(aint)) then
  1164. genjumptable(labels,min_label.svalue,max_label.svalue)
  1165. { value has been determined on an i7-4770 using a random case with random values
  1166. if more values are known, this can be handled depending on the target CPU
  1167. Testing on a Core 2 Duo E6850 as well as on a Raspi3 showed also, that 64 is
  1168. a good value }
  1169. else if labelcnt>=64 then
  1170. genjmptree(labels)
  1171. else
  1172. genlinearlist(labels);
  1173. end;
  1174. end;
  1175. end
  1176. else
  1177. { it's always not bad }
  1178. genlinearlist(labels);
  1179. end;
  1180. { generate the instruction blocks }
  1181. for i:=0 to blocks.count-1 do with pcaseblock(blocks[i])^ do
  1182. begin
  1183. { If the labels are not equal, then the block label has been shortcut to point elsewhere,
  1184. so there's no need to implement it }
  1185. if not shortcut then
  1186. begin
  1187. current_asmdata.CurrAsmList.concat(cai_align.create(current_settings.alignment.jumpalign));
  1188. cg.a_label(current_asmdata.CurrAsmList,blocklabel);
  1189. secondpass(statement);
  1190. { don't come back to case line }
  1191. current_filepos:=current_asmdata.CurrAsmList.getlasttaifilepos^;
  1192. {$ifdef OLDREGVARS}
  1193. load_all_regvars(current_asmdata.CurrAsmList);
  1194. {$endif OLDREGVARS}
  1195. hlcg.a_jmp_always(current_asmdata.CurrAsmList,endlabel);
  1196. end;
  1197. end;
  1198. { ...and the else block }
  1199. if not ShortcutElse then
  1200. begin
  1201. current_asmdata.CurrAsmList.concat(cai_align.create(current_settings.alignment.jumpalign));
  1202. hlcg.a_label(current_asmdata.CurrAsmList,elselabel);
  1203. end;
  1204. if Assigned(elseblock) then
  1205. begin
  1206. secondpass(elseblock);
  1207. {$ifdef OLDREGVARS}
  1208. load_all_regvars(current_asmdata.CurrAsmList);
  1209. {$endif OLDREGVARS}
  1210. end;
  1211. current_asmdata.CurrAsmList.concat(cai_align.create(current_settings.alignment.jumpalign));
  1212. hlcg.a_label(current_asmdata.CurrAsmList,endlabel);
  1213. { Reset labels }
  1214. for i:=0 to blocks.count-1 do
  1215. pcaseblock(blocks[i])^.blocklabel:=nil;
  1216. flowcontrol := oldflowcontrol + (flowcontrol - [fc_inflowcontrol]);
  1217. end;
  1218. begin
  1219. csetelementnode:=tcgsetelementnode;
  1220. cinnode:=tcginnode;
  1221. ccasenode:=tcgcasenode;
  1222. end.