nx86set.pas 43 KB

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