ncgset.pas 66 KB

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