nx86set.pas 45 KB

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