nx86set.pas 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Generate x86 assembler for in/case nodes
  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 nx86set;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,constexp,
  22. node,nset,pass_1,ncgset;
  23. type
  24. tx86innode = class(tinnode)
  25. procedure pass_generate_code;override;
  26. function pass_1 : tnode;override;
  27. end;
  28. tx86casenode = class(tcgcasenode)
  29. function has_jumptable : boolean;override;
  30. procedure genjumptable(hp : pcaselabel;min_,max_ : int64);override;
  31. procedure genlinearlist(hp : pcaselabel);override;
  32. procedure genjmptreeentry(p : pcaselabel;parentvalue : TConstExprInt);override;
  33. end;
  34. implementation
  35. uses
  36. systems,
  37. verbose,globals,
  38. symconst,symdef,defutil,cutils,
  39. aasmbase,aasmtai,aasmdata,aasmcpu,
  40. cgbase,pass_2,tgobj,
  41. ncon,
  42. cpubase,
  43. cga,cgobj,hlcgobj,cgutils,ncgutil,
  44. cgx86,
  45. procinfo;
  46. {*****************************************************************************
  47. TX86CASENODE
  48. *****************************************************************************}
  49. function tx86casenode.has_jumptable : boolean;
  50. begin
  51. has_jumptable:=true;
  52. end;
  53. procedure tx86casenode.genjumptable(hp : pcaselabel;min_,max_ : int64);
  54. var
  55. table : tasmlabel;
  56. last : TConstExprInt;
  57. indexreg : tregister;
  58. href : treference;
  59. jtlist: tasmlist;
  60. opcgsize: tcgsize;
  61. jumpreg: tregister;
  62. labeltyp: taiconst_type;
  63. AlmostExhaustive: Boolean;
  64. lv, hv: TConstExprInt;
  65. ExhaustiveLimit, Range, x, oldmin : int64;
  66. const
  67. ExhaustiveLimitBase = 32;
  68. procedure genitem(list:TAsmList;t : pcaselabel);
  69. var
  70. i : TConstExprInt;
  71. begin
  72. if assigned(t^.less) then
  73. genitem(list,t^.less);
  74. { fill possible hole }
  75. i:=last+1;
  76. while i<=t^._low-1 do
  77. begin
  78. list.concat(Tai_const.Create_type_sym(labeltyp,elselabel));
  79. i:=i+1;
  80. end;
  81. i:=t^._low;
  82. while i<=t^._high do
  83. begin
  84. list.concat(Tai_const.Create_type_sym(labeltyp,blocklabel(t^.blockid)));
  85. i:=i+1;
  86. end;
  87. last:=t^._high;
  88. if assigned(t^.greater) then
  89. genitem(list,t^.greater);
  90. end;
  91. begin
  92. lv:=0;
  93. hv:=0;
  94. oldmin:=0;
  95. last:=min_;
  96. { This generates near pointers on i8086 }
  97. labeltyp:=aitconst_ptr;
  98. opcgsize:=def_cgsize(opsize);
  99. AlmostExhaustive := False;
  100. if not(jumptable_no_range) then
  101. begin
  102. getrange(left.resultdef,lv,hv);
  103. Range := aint(max_)-aint(min_);
  104. if (cs_opt_size in current_settings.optimizerswitches) then
  105. { Limit size of jump tables for small enumerations so they have
  106. to be at least two-thirds full before being considered for the
  107. "almost exhaustive" treatment }
  108. ExhaustiveLimit := min(ExhaustiveLimitBase, labelcoverage shl 1)
  109. else
  110. ExhaustiveLimit := ExhaustiveLimitBase;
  111. { If true, then this indicates that almost every possible value of x is covered by
  112. a label. As such, it's more cost-efficient to remove the initial range check and
  113. instead insert the remaining values into the jump table, pointing at elselabel. [Kit] }
  114. if ((hv - lv) - Range <= ExhaustiveLimit) then
  115. begin
  116. oldmin := min_;
  117. min_ := lv.svalue;
  118. AlmostExhaustive := True;
  119. end
  120. else
  121. begin
  122. { a <= x <= b <-> unsigned(x-a) <= (b-a) }
  123. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SUB,opcgsize,aint(min_),hregister);
  124. { case expr greater than max_ => goto elselabel }
  125. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opcgsize,OC_A,aint(max_)-aint(min_),hregister,elselabel);
  126. min_:=0;
  127. { do not sign extend when we load the index register, as we applied an offset above }
  128. opcgsize:=tcgsize2unsigned[opcgsize];
  129. end;
  130. end;
  131. current_asmdata.getdatalabel(table);
  132. { make it a 32bit register }
  133. indexreg:=cg.makeregsize(current_asmdata.CurrAsmList,hregister,OS_INT);
  134. cg.a_load_reg_reg(current_asmdata.CurrAsmList,opcgsize,OS_INT,hregister,indexreg);
  135. { create reference }
  136. reference_reset_symbol(href,table,0,sizeof(pint),[]);
  137. href.offset:=(-aint(min_))*sizeof(aint);
  138. href.index:=indexreg;
  139. {$ifdef i8086}
  140. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SHL,OS_INT,1,indexreg);
  141. {$else i8086}
  142. href.scalefactor:=sizeof(aint);
  143. {$endif i8086}
  144. if (not (target_info.system in [system_i386_darwin,system_i386_iphonesim])) and
  145. (cs_create_pic in current_settings.moduleswitches) then
  146. begin
  147. labeltyp:=aitconst_gotoff_symbol;
  148. jumpreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_ADDR);
  149. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,href,jumpreg);
  150. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_ADD,OS_ADDR,current_procinfo.got,jumpreg);
  151. emit_reg(A_JMP,S_NO,jumpreg);
  152. include(current_procinfo.flags,pi_needs_got);
  153. end
  154. else
  155. emit_ref(A_JMP,S_NO,href);
  156. { generate jump table }
  157. if (target_info.system in [system_i386_darwin,system_i386_iphonesim]) then
  158. jtlist:=current_asmdata.asmlists[al_const]
  159. else
  160. jtlist:=current_procinfo.aktlocaldata;
  161. new_section(jtlist,sec_rodata,current_procinfo.procdef.mangledname,sizeof(aint));
  162. jtlist.concat(Tai_label.Create(table));
  163. if AlmostExhaustive then
  164. begin
  165. { Fill the table with the values below _min }
  166. x := lv.svalue;
  167. while x < oldmin do
  168. begin
  169. jtlist.concat(Tai_const.Create_type_sym(labeltyp, elselabel));
  170. Inc(x);
  171. end;
  172. genitem(jtlist,hp);
  173. { Fill the table with the values above _max }
  174. { Subtracting one from hv and not adding 1 to max averts the risk of an overflow }
  175. x := max_;
  176. hv := hv - 1;
  177. while x <= hv.svalue do
  178. begin
  179. jtlist.concat(Tai_const.Create_type_sym(labeltyp, elselabel));
  180. Inc(x);
  181. end;
  182. end
  183. else
  184. genitem(jtlist,hp)
  185. end;
  186. procedure tx86casenode.genlinearlist(hp : pcaselabel);
  187. var
  188. first : boolean;
  189. lastrange : boolean;
  190. last : TConstExprInt;
  191. cond_lt,cond_le : tresflags;
  192. opcgsize: tcgsize;
  193. procedure genitem(t : pcaselabel);
  194. var
  195. range, gap: aint;
  196. begin
  197. if assigned(t^.less) then
  198. genitem(t^.less);
  199. { need we to test the first value }
  200. if first and (t^._low>get_min_value(left.resultdef)) then
  201. begin
  202. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opcgsize,jmp_lt,aint(t^._low.svalue),hregister,elselabel);
  203. end;
  204. if t^._low=t^._high then
  205. begin
  206. if t^._low-last=0 then
  207. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, opcgsize, OC_EQ,0,hregister,blocklabel(t^.blockid))
  208. else
  209. begin
  210. cg.a_reg_alloc(current_asmdata.CurrAsmList, NR_DEFAULTFLAGS);
  211. cg.a_op_const_reg(current_asmdata.CurrAsmList, OP_SUB, opcgsize, aint(t^._low.svalue-last.svalue), hregister);
  212. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_E,blocklabel(t^.blockid));
  213. cg.a_reg_dealloc(current_asmdata.CurrAsmList, NR_DEFAULTFLAGS);
  214. end;
  215. last:=t^._low;
  216. lastrange:=false;
  217. end
  218. else
  219. begin
  220. range := aint(t^._high.svalue - t^._low.svalue);
  221. { it begins with the smallest label, if the value }
  222. { is even smaller then jump immediately to the }
  223. { ELSE-label }
  224. cg.a_reg_alloc(current_asmdata.CurrAsmList, NR_DEFAULTFLAGS);
  225. if first then
  226. begin
  227. { have we to ajust the first value ? }
  228. if (t^._low>get_min_value(left.resultdef)) or (get_min_value(left.resultdef)<>0) then
  229. cg.a_op_const_reg(current_asmdata.CurrAsmList, OP_SUB, opcgsize, aint(t^._low.svalue), hregister);
  230. end
  231. else
  232. begin
  233. gap := aint(t^._low.svalue - last.svalue);
  234. { if there is no unused label between the last and the }
  235. { present label then the lower limit can be checked }
  236. { immediately. else check the range in between: }
  237. { we need to use A_SUB, if cond_lt uses the carry flags
  238. because A_DEC does not set the correct flags, therefor
  239. using a_op_const_reg(OP_SUB) is not possible }
  240. if (gap = 1) and (cond_lt in [F_C,F_NC,F_A,F_AE,F_B,F_BE]) then
  241. emit_const_reg(A_SUB, TCGSize2OpSize[opcgsize], gap, hregister)
  242. else
  243. cg.a_op_const_reg(current_asmdata.CurrAsmList, OP_SUB, opcgsize, gap, hregister);
  244. { no jump necessary here if the new range starts at
  245. at the value following the previous one }
  246. if (gap <> 1) or
  247. (not lastrange) then
  248. cg.a_jmp_flags(current_asmdata.CurrAsmList,cond_lt,elselabel);
  249. end;
  250. { we need to use A_SUB, if cond_le uses the carry flags
  251. because A_DEC does not set the correct flags, therefor
  252. using a_op_const_reg(OP_SUB) is not possible }
  253. if (cond_le in [F_C,F_NC,F_A,F_AE,F_B,F_BE]) and (range = 1) then
  254. emit_const_reg(A_SUB,TCGSize2OpSize[opcgsize], range, hregister)
  255. else
  256. cg.a_op_const_reg(current_asmdata.CurrAsmList, OP_SUB, opcgsize, range, hregister);
  257. cg.a_jmp_flags(current_asmdata.CurrAsmList,cond_le,blocklabel(t^.blockid));
  258. cg.a_reg_dealloc(current_asmdata.CurrAsmList, NR_DEFAULTFLAGS);
  259. last:=t^._high;
  260. lastrange:=true;
  261. end;
  262. first:=false;
  263. if assigned(t^.greater) then
  264. genitem(t^.greater);
  265. end;
  266. begin
  267. opcgsize:=def_cgsize(opsize);
  268. if with_sign then
  269. begin
  270. cond_lt:=F_L;
  271. cond_le:=F_LE;
  272. end
  273. else
  274. begin
  275. cond_lt:=F_B;
  276. cond_le:=F_BE;
  277. end;
  278. { do we need to generate cmps? }
  279. {$ifdef i8086}
  280. if (with_sign and (min_label<0)) or (opcgsize in [OS_32, OS_S32]) then
  281. {$else i8086}
  282. if (with_sign and (min_label<0)) then
  283. {$endif i8086}
  284. genlinearcmplist(hp)
  285. else
  286. begin
  287. if (labelcnt>1) or not(cs_opt_level1 in current_settings.optimizerswitches) then
  288. begin
  289. last:=0;
  290. lastrange:=false;
  291. first:=true;
  292. genitem(hp);
  293. end
  294. else
  295. begin
  296. { If only one label exists, we can greatly simplify the checks to a simple comparison }
  297. if hp^._low=hp^._high then
  298. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, opcgsize, OC_EQ, tcgint(hp^._low.svalue), hregister, blocklabel(hp^.blockid))
  299. else
  300. begin
  301. cg.a_op_const_reg(current_asmdata.CurrAsmList, OP_SUB, opcgsize, tcgint(hp^._low.svalue), hregister);
  302. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, opcgsize, OC_BE, tcgint(hp^._high.svalue - hp^._low.svalue), hregister,blocklabel(hp^.blockid));
  303. end;
  304. end;
  305. cg.a_jmp_always(current_asmdata.CurrAsmList,elselabel);
  306. end;
  307. end;
  308. procedure tx86casenode.genjmptreeentry(p : pcaselabel;parentvalue : TConstExprInt);
  309. var
  310. lesslabel,greaterlabel : tasmlabel;
  311. cond_gt: TResFlags;
  312. cmplow : Boolean;
  313. begin
  314. if with_sign then
  315. cond_gt:=F_G
  316. else
  317. cond_gt:=F_A;
  318. current_asmdata.CurrAsmList.concat(cai_align.Create(current_settings.alignment.jumpalign));
  319. cg.a_label(current_asmdata.CurrAsmList,p^.labellabel);
  320. { calculate labels for left and right }
  321. if p^.less=nil then
  322. lesslabel:=elselabel
  323. else
  324. lesslabel:=p^.less^.labellabel;
  325. if p^.greater=nil then
  326. greaterlabel:=elselabel
  327. else
  328. greaterlabel:=p^.greater^.labellabel;
  329. { calculate labels for left and right }
  330. { no range label: }
  331. if p^._low=p^._high then
  332. begin
  333. if greaterlabel=lesslabel then
  334. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,OC_NE,p^._low,hregister,lesslabel)
  335. else
  336. begin
  337. cmplow:=p^._low-1<>parentvalue;
  338. if cmplow then
  339. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,jmp_lt,p^._low,hregister,lesslabel);
  340. if p^._high+1<>parentvalue then
  341. begin
  342. if cmplow then
  343. hlcg.a_jmp_flags(current_asmdata.CurrAsmList,cond_gt,greaterlabel)
  344. else
  345. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,jmp_gt,p^._low,hregister,greaterlabel);
  346. end;
  347. end;
  348. hlcg.a_jmp_always(current_asmdata.CurrAsmList,blocklabel(p^.blockid));
  349. end
  350. else
  351. begin
  352. if p^._low-1<>parentvalue then
  353. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,jmp_lt,p^._low,hregister,lesslabel);
  354. if p^._high+1<>parentvalue then
  355. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,jmp_gt,p^._high,hregister,greaterlabel);
  356. hlcg.a_jmp_always(current_asmdata.CurrAsmList,blocklabel(p^.blockid));
  357. end;
  358. if assigned(p^.less) then
  359. genjmptreeentry(p^.less,p^._low);
  360. if assigned(p^.greater) then
  361. genjmptreeentry(p^.greater,p^._high);
  362. end;
  363. {*****************************************************************************
  364. TX86INNODE
  365. *****************************************************************************}
  366. function tx86innode.pass_1 : tnode;
  367. begin
  368. result:=nil;
  369. { this is the only difference from the generic version }
  370. expectloc:=LOC_FLAGS;
  371. firstpass(right);
  372. firstpass(left);
  373. if codegenerror then
  374. exit;
  375. end;
  376. procedure tx86innode.pass_generate_code;
  377. type
  378. Tsetpart=record
  379. range : boolean; {Part is a range.}
  380. start,stop : byte; {Start/stop when range; Stop=element when an element.}
  381. end;
  382. var
  383. hreg,{$ifndef i8086} hreg2, {$endif}
  384. pleftreg : tregister;
  385. opsize : tcgsize;
  386. opdef : torddef;
  387. orgopsize : tcgsize;
  388. setparts : array[1..8] of Tsetpart;
  389. setbase : aint;
  390. adjustment : longint;
  391. l,l2 : tasmlabel;
  392. i,numparts : byte;
  393. genjumps,
  394. use_small,
  395. ranges : boolean;
  396. {$ifdef CORRECT_SET_IN_FPC}
  397. AM : tasmop;
  398. {$endif CORRECT_SET_IN_FPC}
  399. {$ifdef i8086}
  400. extra_offset_reg: TRegister;
  401. {$endif i8086}
  402. function analizeset(Aset:pconstset;is_small:boolean):boolean;
  403. var
  404. compares,maxcompares:word;
  405. i:byte;
  406. begin
  407. if tnormalset(Aset^)=[] then
  408. {The expression...
  409. if expr in []
  410. ...is allways false. It should be optimized away in the
  411. resultdef pass, and thus never occur here. Since we
  412. do generate wrong code for it, do internalerror.}
  413. internalerror(2002072301);
  414. analizeset:=false;
  415. ranges:=false;
  416. numparts:=0;
  417. compares:=0;
  418. { Lots of comparisions take a lot of time, so do not allow
  419. too much comparisions. 8 comparisions are, however, still
  420. smalller than emitting the set }
  421. if cs_opt_size in current_settings.optimizerswitches then
  422. maxcompares:=8
  423. else
  424. maxcompares:=5;
  425. { when smallset is possible allow only 3 compares the smallset
  426. code is for littlesize also smaller when more compares are used }
  427. if is_small then
  428. maxcompares:=3;
  429. for i:=0 to 255 do
  430. if i in tnormalset(Aset^) then
  431. begin
  432. if (numparts=0) or (i<>setparts[numparts].stop+1) then
  433. begin
  434. {Set element is a separate element.}
  435. inc(compares);
  436. if compares>maxcompares then
  437. exit;
  438. inc(numparts);
  439. setparts[numparts].range:=false;
  440. setparts[numparts].stop:=i;
  441. end
  442. else
  443. {Set element is part of a range.}
  444. if not setparts[numparts].range then
  445. begin
  446. {Transform an element into a range.}
  447. setparts[numparts].range:=true;
  448. setparts[numparts].start:=setparts[numparts].stop;
  449. setparts[numparts].stop:=i;
  450. ranges := true;
  451. end
  452. else
  453. begin
  454. {Extend a range.}
  455. setparts[numparts].stop:=i;
  456. end;
  457. end;
  458. analizeset:=true;
  459. end;
  460. {$ifdef i8086}
  461. procedure add_extra_offset(offset_reg:TRegister;var ref:treference);
  462. var
  463. reg: TRegister;
  464. begin
  465. if ref.index=NR_NO then
  466. ref.index:=offset_reg
  467. else if ref.base=NR_NO then
  468. ref.base:=offset_reg
  469. else
  470. begin
  471. reg:=cg.getaddressregister(current_asmdata.CurrAsmList);
  472. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,ref.index,reg);
  473. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_ADD,OS_ADDR,offset_reg,reg);
  474. ref.index:=reg;
  475. end;
  476. end;
  477. {$endif i8086}
  478. begin
  479. ranges:=false;
  480. numparts:=0;
  481. fillchar(setparts,sizeof(setparts),0);
  482. { We check first if we can generate jumps, this can be done
  483. because the resultdef is already set in firstpass }
  484. { check if we can use smallset operation using btl which is limited
  485. to 32 bits, the left side may also not contain higher values or be signed !! }
  486. use_small:=is_smallset(right.resultdef) and
  487. not is_signed(left.resultdef) and
  488. ((left.resultdef.typ=orddef) and (torddef(left.resultdef).high.svalue<{$ifdef i8086}16{$else}32{$endif}) or
  489. (left.resultdef.typ=enumdef) and (tenumdef(left.resultdef).max<{$ifdef i8086}16{$else}32{$endif}));
  490. { Can we generate jumps? Possible for all types of sets }
  491. genjumps:=(right.nodetype=setconstn) and
  492. analizeset(tsetconstnode(right).value_set,use_small);
  493. { calculate both operators }
  494. { the complex one first }
  495. { not in case of genjumps, because then we don't secondpass }
  496. { right at all (so we have to make sure that "right" really is }
  497. { "right" and not "swapped left" in that case) }
  498. if not(genjumps) then
  499. firstcomplex(self);
  500. secondpass(left);
  501. { Only process the right if we are not generating jumps }
  502. if not genjumps then
  503. begin
  504. secondpass(right);
  505. end;
  506. if codegenerror then
  507. exit;
  508. { ofcourse not commutative }
  509. if nf_swapped in flags then
  510. swapleftright;
  511. orgopsize := def_cgsize(left.resultdef);
  512. {$ifdef i8086}
  513. opsize := OS_16;
  514. {$else i8086}
  515. opsize := OS_32;
  516. {$endif i8086}
  517. if is_signed(left.resultdef) then
  518. opsize := tcgsize(ord(opsize)+(ord(OS_S8)-ord(OS_8)));
  519. opdef:=cgsize_orddef(opsize);
  520. if not(left.location.loc in [LOC_REGISTER,LOC_CREGISTER,LOC_REFERENCE,LOC_CREFERENCE,LOC_CONSTANT]) then
  521. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,opdef,true);
  522. if (right.location.loc in [LOC_SUBSETREG,LOC_CSUBSETREG]) then
  523. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,left.resultdef,opdef,true);
  524. if genjumps then
  525. begin
  526. { It gives us advantage to check for the set elements
  527. separately instead of using the SET_IN_BYTE procedure.
  528. To do: Build in support for LOC_JUMP }
  529. { load and zero or sign extend as necessary }
  530. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,opdef,false);
  531. pleftreg:=left.location.register;
  532. { Get a label to jump to the end }
  533. location_reset(location,LOC_FLAGS,OS_NO);
  534. { It's better to use the zero flag when there are
  535. no ranges }
  536. if ranges then
  537. location.resflags:=F_C
  538. else
  539. location.resflags:=F_E;
  540. current_asmdata.getjumplabel(l);
  541. { how much have we already substracted from the x in the }
  542. { "x in [y..z]" expression }
  543. adjustment := 0;
  544. cg.a_reg_alloc(current_asmdata.CurrAsmList, NR_DEFAULTFLAGS);
  545. for i:=1 to numparts do
  546. if setparts[i].range then
  547. { use fact that a <= x <= b <=> cardinal(x-a) <= cardinal(b-a) }
  548. begin
  549. { is the range different from all legal values? }
  550. if (setparts[i].stop-setparts[i].start <> 255) or not (orgopsize = OS_8) then
  551. begin
  552. { yes, is the lower bound <> 0? }
  553. if (setparts[i].start <> 0) then
  554. begin
  555. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,opdef,false);
  556. hreg:=left.location.register;
  557. pleftreg:=hreg;
  558. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SUB,opsize,setparts[i].start-adjustment,pleftreg);
  559. end;
  560. { new total value substracted from x: }
  561. { adjustment + (setparts[i].start - adjustment) }
  562. adjustment := setparts[i].start;
  563. { check if result < b-a+1 (not "result <= b-a", since }
  564. { we need a carry in case the element is in the range }
  565. { (this will never overflow since we check at the }
  566. { beginning whether stop-start <> 255) }
  567. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,OC_B,setparts[i].stop-setparts[i].start+1,pleftreg,l);
  568. cg.a_reg_alloc(current_asmdata.CurrAsmList, NR_DEFAULTFLAGS);
  569. end
  570. else
  571. { if setparts[i].start = 0 and setparts[i].stop = 255, }
  572. { it's always true since "in" is only allowed for bytes }
  573. begin
  574. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_STC,S_NO));
  575. cg.a_jmp_always(current_asmdata.CurrAsmList,l);
  576. end;
  577. end
  578. else
  579. begin
  580. { Emit code to check if left is an element }
  581. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,TCGSize2OpSize[opsize],setparts[i].stop-adjustment,
  582. pleftreg));
  583. { Result should be in carry flag when ranges are used }
  584. if ranges then
  585. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_STC,S_NO));
  586. { If found, jump to end }
  587. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_E,l);
  588. end;
  589. if ranges and
  590. { if the last one was a range, the carry flag is already }
  591. { set appropriately }
  592. not(setparts[numparts].range) then
  593. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_CLC,S_NO));
  594. { To compensate for not doing a second pass }
  595. right.location.reference.symbol:=nil;
  596. { Now place the end label }
  597. cg.a_label(current_asmdata.CurrAsmList,l);
  598. end
  599. else
  600. begin
  601. location_reset(location,LOC_FLAGS,OS_NO);
  602. setbase:=tsetdef(right.resultdef).setbase;
  603. { We will now generated code to check the set itself, no jmps,
  604. handle smallsets separate, because it allows faster checks }
  605. if use_small then
  606. begin
  607. if left.location.loc=LOC_CONSTANT then
  608. begin
  609. cg.a_reg_alloc(current_asmdata.CurrAsmList, NR_DEFAULTFLAGS);
  610. location.resflags:=F_NE;
  611. case right.location.loc of
  612. LOC_REGISTER,
  613. LOC_CREGISTER:
  614. begin
  615. emit_const_reg(A_TEST,TCGSize2OpSize[right.location.size],
  616. 1 shl ((left.location.value-setbase) and 31),right.location.register);
  617. end;
  618. LOC_REFERENCE,
  619. LOC_CREFERENCE :
  620. begin
  621. emit_const_ref(A_TEST,TCGSize2OpSize[right.location.size],1 shl ((left.location.value-setbase) and 31),
  622. right.location.reference);
  623. end;
  624. else
  625. internalerror(200203312);
  626. end;
  627. end
  628. else
  629. begin
  630. {$ifdef i8086}
  631. register_maybe_adjust_setbase(current_asmdata.CurrAsmList,left.resultdef,left.location,setbase);
  632. cg.getcpuregister(current_asmdata.CurrAsmList,NR_CX);
  633. if TCGSize2Size[left.location.size] > 2 then
  634. left.location.size := OS_16;
  635. cg.a_load_loc_reg(current_asmdata.CurrAsmList,OS_16,left.location,NR_CX);
  636. if (tcgsize2size[right.location.size] < 2) or
  637. (right.location.loc = LOC_CONSTANT) then
  638. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,u16inttype,true);
  639. hreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_16);
  640. emit_const_reg(A_MOV,S_W,1,hreg);
  641. emit_reg_reg(A_SHL,S_W,NR_CL,hreg);
  642. cg.a_reg_alloc(current_asmdata.CurrAsmList, NR_DEFAULTFLAGS);
  643. case right.location.loc of
  644. LOC_REGISTER,
  645. LOC_CREGISTER :
  646. begin
  647. emit_reg_reg(A_TEST,S_W,hreg,right.location.register);
  648. end;
  649. LOC_CREFERENCE,
  650. LOC_REFERENCE :
  651. begin
  652. emit_reg_ref(A_TEST,S_W,hreg,right.location.reference);
  653. end;
  654. else
  655. internalerror(2002032210);
  656. end;
  657. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_CX);
  658. location.resflags:=F_NE;
  659. {$else i8086}
  660. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,u32inttype,true);
  661. register_maybe_adjust_setbase(current_asmdata.CurrAsmList,u32inttype,left.location,setbase);
  662. if (tcgsize2size[right.location.size] < opdef.size) or
  663. (right.location.loc = LOC_CONSTANT) or
  664. { bt ...,[mem] is slow, see #40039, so try to use a register if we are not optimizing for size }
  665. ((right.resultdef.size<=u32inttype.size) and not(cs_opt_size in current_settings.optimizerswitches)) then
  666. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,u32inttype,true);
  667. hreg:=left.location.register;
  668. cg.a_reg_alloc(current_asmdata.CurrAsmList, NR_DEFAULTFLAGS);
  669. case right.location.loc of
  670. LOC_REGISTER,
  671. LOC_CREGISTER :
  672. begin
  673. emit_reg_reg(A_BT,S_L,hreg,right.location.register);
  674. end;
  675. LOC_CREFERENCE,
  676. LOC_REFERENCE :
  677. begin
  678. emit_reg_ref(A_BT,S_L,hreg,right.location.reference);
  679. end;
  680. else
  681. internalerror(2002032210);
  682. end;
  683. location.resflags:=F_C;
  684. {$endif i8086}
  685. end;
  686. end
  687. else
  688. begin
  689. if right.location.loc=LOC_CONSTANT then
  690. begin
  691. {$ifdef i8086}
  692. location.resflags:=F_NE;
  693. current_asmdata.getjumplabel(l);
  694. current_asmdata.getjumplabel(l2);
  695. { load constants to a register }
  696. if (left.location.loc=LOC_CONSTANT) or
  697. (setbase<>0) then
  698. begin
  699. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,opdef,true);
  700. register_maybe_adjust_setbase(current_asmdata.CurrAsmList,opdef,left.location,setbase);
  701. end;
  702. cg.getcpuregister(current_asmdata.CurrAsmList,NR_CX);
  703. if TCGSize2Size[left.location.size] > 2 then
  704. left.location.size := OS_16;
  705. cg.a_load_loc_reg(current_asmdata.CurrAsmList,OS_16,left.location,NR_CX);
  706. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,OC_BE,15,NR_CX,l);
  707. cg.a_reg_alloc(current_asmdata.CurrAsmList, NR_DEFAULTFLAGS);
  708. { set the zero flag }
  709. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_TEST,S_B,0,NR_AL));
  710. cg.a_jmp_always(current_asmdata.CurrAsmList,l2);
  711. cg.a_reg_dealloc(current_asmdata.CurrAsmList, NR_DEFAULTFLAGS);
  712. hreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_16);
  713. cg.a_label(current_asmdata.CurrAsmList,l);
  714. emit_const_reg(A_MOV,S_W,1,hreg);
  715. emit_reg_reg(A_SHL,S_W,NR_CL,hreg);
  716. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_CX);
  717. cg.a_reg_alloc(current_asmdata.CurrAsmList, NR_DEFAULTFLAGS);
  718. emit_const_reg(A_TEST,S_W,right.location.value,hreg);
  719. cg.a_label(current_asmdata.CurrAsmList,l2);
  720. {$else i8086}
  721. location.resflags:=F_C;
  722. current_asmdata.getjumplabel(l);
  723. current_asmdata.getjumplabel(l2);
  724. { load constants to a register }
  725. if (left.location.loc=LOC_CONSTANT) or
  726. (setbase<>0) then
  727. begin
  728. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,opdef,true);
  729. register_maybe_adjust_setbase(current_asmdata.CurrAsmList,opdef,left.location,setbase);
  730. end;
  731. case left.location.loc of
  732. LOC_REGISTER,
  733. LOC_CREGISTER:
  734. begin
  735. hreg:=cg.makeregsize(current_asmdata.CurrAsmList,left.location.register,opsize);
  736. cg.a_load_reg_reg(current_asmdata.CurrAsmList,left.location.size,opsize,left.location.register,hreg);
  737. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,OC_BE,31,hreg,l);
  738. cg.a_reg_alloc(current_asmdata.CurrAsmList, NR_DEFAULTFLAGS);
  739. { reset carry flag }
  740. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_CLC,S_NO));
  741. cg.a_jmp_always(current_asmdata.CurrAsmList,l2);
  742. cg.a_label(current_asmdata.CurrAsmList,l);
  743. { We have to load the value into a register because
  744. btl does not accept values only refs or regs (PFV) }
  745. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  746. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_32,right.location.value,hreg2);
  747. emit_reg_reg(A_BT,S_L,hreg,hreg2);
  748. end;
  749. else
  750. begin
  751. cg.a_reg_alloc(current_asmdata.CurrAsmList, NR_DEFAULTFLAGS);
  752. emit_const_ref(A_CMP,TCGSize2OpSize[orgopsize],31,left.location.reference);
  753. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_BE,l);
  754. { reset carry flag }
  755. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_CLC,S_NO));
  756. cg.a_jmp_always(current_asmdata.CurrAsmList,l2);
  757. cg.a_label(current_asmdata.CurrAsmList,l);
  758. hreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  759. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_32,OS_32,left.location.reference,hreg);
  760. { We have to load the value into a register because
  761. btl does not accept values only refs or regs (PFV) }
  762. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  763. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_32,right.location.value,hreg2);
  764. emit_reg_reg(A_BT,S_L,hreg,hreg2);
  765. end;
  766. end;
  767. cg.a_label(current_asmdata.CurrAsmList,l2);
  768. {$endif i8086}
  769. end { of right.location.loc=LOC_CONSTANT }
  770. { do search in a normal set which could have >32 elementsm
  771. but also used if the left side contains values > 32 or < 0 }
  772. else if left.location.loc=LOC_CONSTANT then
  773. begin
  774. if (left.location.value<setbase) or (((left.location.value-setbase) shr 3) >= right.resultdef.size) then
  775. {should be caught earlier }
  776. internalerror(2007020201);
  777. location.resflags:=F_NE;
  778. case right.location.loc of
  779. LOC_REFERENCE,LOC_CREFERENCE:
  780. begin
  781. inc(right.location.reference.offset,(left.location.value-setbase) shr 3);
  782. cg.a_reg_alloc(current_asmdata.CurrAsmList, NR_DEFAULTFLAGS);
  783. emit_const_ref(A_TEST,S_B,1 shl ((left.location.value-setbase) and 7),right.location.reference);
  784. end;
  785. LOC_REGISTER,LOC_CREGISTER:
  786. begin
  787. cg.a_reg_alloc(current_asmdata.CurrAsmList, NR_DEFAULTFLAGS);
  788. emit_const_reg(A_TEST,TCGSize2OpSize[right.location.size],1 shl (left.location.value-setbase),right.location.register);
  789. end;
  790. else
  791. internalerror(2007051901);
  792. end;
  793. end
  794. else
  795. begin
  796. {$ifdef i8086}
  797. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,opdef,false);
  798. register_maybe_adjust_setbase(current_asmdata.CurrAsmList,opdef,left.location,setbase);
  799. if TCGSize2Size[left.location.size] > 2 then
  800. left.location.size := OS_16;
  801. if not use_small then
  802. begin
  803. extra_offset_reg:=cg.getintregister(current_asmdata.CurrAsmList,OS_16);
  804. cg.a_load_loc_reg(current_asmdata.CurrAsmList,OS_16,left.location,extra_offset_reg);
  805. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SHR,OS_16,4,extra_offset_reg);
  806. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SHL,OS_16,1,extra_offset_reg);
  807. end
  808. else
  809. extra_offset_reg:=NR_NO;
  810. cg.getcpuregister(current_asmdata.CurrAsmList,NR_CX);
  811. cg.a_load_loc_reg(current_asmdata.CurrAsmList,OS_16,left.location,NR_CX);
  812. if not use_small then
  813. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_AND,S_B,15,NR_CL));
  814. pleftreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_16);
  815. if (right.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  816. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,opdef,true);
  817. if (opsize >= OS_S8) or { = if signed }
  818. ((left.resultdef.typ=orddef) and
  819. ((torddef(left.resultdef).low < int64(tsetdef(right.resultdef).setbase)) or
  820. (torddef(left.resultdef).high > int64(tsetdef(right.resultdef).setmax)))) or
  821. ((left.resultdef.typ=enumdef) and
  822. ((tenumdef(left.resultdef).min < aint(tsetdef(right.resultdef).setbase)) or
  823. (tenumdef(left.resultdef).max > aint(tsetdef(right.resultdef).setmax)))) then
  824. begin
  825. { we have to check if the value is < 0 or > setmax }
  826. current_asmdata.getjumplabel(l);
  827. current_asmdata.getjumplabel(l2);
  828. { BE will be false for negative values }
  829. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,OC_BE,tsetdef(right.resultdef).setmax-tsetdef(right.resultdef).setbase,pleftreg,l);
  830. { set the zero flag }
  831. cg.a_reg_alloc(current_asmdata.CurrAsmList, NR_DEFAULTFLAGS);
  832. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_TEST,S_B,0,NR_AL));
  833. cg.a_jmp_always(current_asmdata.CurrAsmList,l2);
  834. cg.a_label(current_asmdata.CurrAsmList,l);
  835. cg.a_reg_dealloc(current_asmdata.CurrAsmList, NR_DEFAULTFLAGS);
  836. emit_const_reg(A_MOV,S_W,1,pleftreg);
  837. emit_reg_reg(A_SHL,S_W,NR_CL,pleftreg);
  838. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_CX);
  839. case right.location.loc of
  840. LOC_REGISTER, LOC_CREGISTER :
  841. begin
  842. cg.a_reg_alloc(current_asmdata.CurrAsmList, NR_DEFAULTFLAGS);
  843. emit_reg_reg(A_TEST,S_W,pleftreg,right.location.register);
  844. end;
  845. LOC_CREFERENCE, LOC_REFERENCE :
  846. begin
  847. if not use_small then
  848. add_extra_offset(extra_offset_reg,right.location.reference);
  849. cg.a_reg_alloc(current_asmdata.CurrAsmList, NR_DEFAULTFLAGS);
  850. emit_reg_ref(A_TEST,S_W,pleftreg,right.location.reference);
  851. end;
  852. else
  853. internalerror(2007020301);
  854. end;
  855. cg.a_label(current_asmdata.CurrAsmList,l2);
  856. location.resflags:=F_NE;
  857. end
  858. else
  859. begin
  860. emit_const_reg(A_MOV,S_W,1,pleftreg);
  861. emit_reg_reg(A_SHL,S_W,NR_CL,pleftreg);
  862. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_CX);
  863. case right.location.loc of
  864. LOC_REGISTER, LOC_CREGISTER :
  865. begin
  866. cg.a_reg_alloc(current_asmdata.CurrAsmList, NR_DEFAULTFLAGS);
  867. emit_reg_reg(A_TEST,S_W,pleftreg,right.location.register);
  868. end;
  869. LOC_CREFERENCE, LOC_REFERENCE :
  870. begin
  871. if not use_small then
  872. add_extra_offset(extra_offset_reg,right.location.reference);
  873. cg.a_reg_alloc(current_asmdata.CurrAsmList, NR_DEFAULTFLAGS);
  874. emit_reg_ref(A_TEST,S_W,pleftreg,right.location.reference);
  875. end;
  876. else
  877. internalerror(2007020302);
  878. end;
  879. location.resflags:=F_NE;
  880. end;
  881. {$else i8086}
  882. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,opdef,false);
  883. register_maybe_adjust_setbase(current_asmdata.CurrAsmList,opdef,left.location,setbase);
  884. if (right.location.loc in [LOC_REGISTER,LOC_CREGISTER]) or
  885. { bt ...,[mem] is slow, see #40039, so try to use a register if we are not optimizing for size }
  886. ((right.resultdef.size<=opdef.size) and not(cs_opt_size in current_settings.optimizerswitches)) then
  887. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,opdef,true);
  888. pleftreg:=left.location.register;
  889. if (opsize >= OS_S8) or { = if signed }
  890. ((left.resultdef.typ=orddef) and
  891. ((torddef(left.resultdef).low < int64(tsetdef(right.resultdef).setbase)) or
  892. (torddef(left.resultdef).high > int64(tsetdef(right.resultdef).setmax)))) or
  893. ((left.resultdef.typ=enumdef) and
  894. ((tenumdef(left.resultdef).min < aint(tsetdef(right.resultdef).setbase)) or
  895. (tenumdef(left.resultdef).max > aint(tsetdef(right.resultdef).setmax)))) then
  896. begin
  897. { we have to check if the value is < 0 or > setmax }
  898. current_asmdata.getjumplabel(l);
  899. current_asmdata.getjumplabel(l2);
  900. { BE will be false for negative values }
  901. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,OC_BE,tsetdef(right.resultdef).setmax-tsetdef(right.resultdef).setbase,pleftreg,l);
  902. cg.a_reg_alloc(current_asmdata.CurrAsmList, NR_DEFAULTFLAGS);
  903. { reset carry flag }
  904. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_CLC,S_NO));
  905. cg.a_jmp_always(current_asmdata.CurrAsmList,l2);
  906. cg.a_label(current_asmdata.CurrAsmList,l);
  907. pleftreg:=left.location.register;
  908. case right.location.loc of
  909. LOC_REGISTER, LOC_CREGISTER :
  910. emit_reg_reg(A_BT,S_L,pleftreg,right.location.register);
  911. LOC_CREFERENCE, LOC_REFERENCE :
  912. emit_reg_ref(A_BT,S_L,pleftreg,right.location.reference);
  913. else
  914. internalerror(2007020301);
  915. end;
  916. cg.a_label(current_asmdata.CurrAsmList,l2);
  917. location.resflags:=F_C;
  918. end
  919. else
  920. begin
  921. cg.a_reg_alloc(current_asmdata.CurrAsmList, NR_DEFAULTFLAGS);
  922. case right.location.loc of
  923. LOC_REGISTER, LOC_CREGISTER :
  924. emit_reg_reg(A_BT,S_L,pleftreg,right.location.register);
  925. LOC_CREFERENCE, LOC_REFERENCE :
  926. emit_reg_ref(A_BT,S_L,pleftreg,right.location.reference);
  927. else
  928. internalerror(2007020302);
  929. end;
  930. location.resflags:=F_C;
  931. end;
  932. {$endif i8086}
  933. end;
  934. end;
  935. end;
  936. if not genjumps then
  937. location_freetemp(current_asmdata.CurrAsmList,right.location);
  938. end;
  939. begin
  940. cinnode:=tx86innode;
  941. ccasenode:=tx86casenode;
  942. end.