nx86set.pas 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008
  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. 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,hreg2,
  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. for i:=1 to numparts do
  541. if setparts[i].range then
  542. { use fact that a <= x <= b <=> cardinal(x-a) <= cardinal(b-a) }
  543. begin
  544. { is the range different from all legal values? }
  545. if (setparts[i].stop-setparts[i].start <> 255) or not (orgopsize = OS_8) then
  546. begin
  547. { yes, is the lower bound <> 0? }
  548. if (setparts[i].start <> 0) then
  549. begin
  550. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,opdef,false);
  551. hreg:=left.location.register;
  552. pleftreg:=hreg;
  553. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SUB,opsize,setparts[i].start-adjustment,pleftreg);
  554. end;
  555. { new total value substracted from x: }
  556. { adjustment + (setparts[i].start - adjustment) }
  557. adjustment := setparts[i].start;
  558. { check if result < b-a+1 (not "result <= b-a", since }
  559. { we need a carry in case the element is in the range }
  560. { (this will never overflow since we check at the }
  561. { beginning whether stop-start <> 255) }
  562. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,OC_B,setparts[i].stop-setparts[i].start+1,pleftreg,l);
  563. end
  564. else
  565. { if setparts[i].start = 0 and setparts[i].stop = 255, }
  566. { it's always true since "in" is only allowed for bytes }
  567. begin
  568. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_STC,S_NO));
  569. cg.a_jmp_always(current_asmdata.CurrAsmList,l);
  570. end;
  571. end
  572. else
  573. begin
  574. { Emit code to check if left is an element }
  575. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,TCGSize2OpSize[opsize],setparts[i].stop-adjustment,
  576. pleftreg));
  577. { Result should be in carry flag when ranges are used }
  578. if ranges then
  579. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_STC,S_NO));
  580. { If found, jump to end }
  581. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_E,l);
  582. end;
  583. if ranges and
  584. { if the last one was a range, the carry flag is already }
  585. { set appropriately }
  586. not(setparts[numparts].range) then
  587. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_CLC,S_NO));
  588. { To compensate for not doing a second pass }
  589. right.location.reference.symbol:=nil;
  590. { Now place the end label }
  591. cg.a_label(current_asmdata.CurrAsmList,l);
  592. end
  593. else
  594. begin
  595. location_reset(location,LOC_FLAGS,OS_NO);
  596. setbase:=tsetdef(right.resultdef).setbase;
  597. { We will now generated code to check the set itself, no jmps,
  598. handle smallsets separate, because it allows faster checks }
  599. if use_small then
  600. begin
  601. if left.location.loc=LOC_CONSTANT then
  602. begin
  603. location.resflags:=F_NE;
  604. case right.location.loc of
  605. LOC_REGISTER,
  606. LOC_CREGISTER:
  607. begin
  608. emit_const_reg(A_TEST,TCGSize2OpSize[right.location.size],
  609. 1 shl ((left.location.value-setbase) and 31),right.location.register);
  610. end;
  611. LOC_REFERENCE,
  612. LOC_CREFERENCE :
  613. begin
  614. emit_const_ref(A_TEST,TCGSize2OpSize[right.location.size],1 shl ((left.location.value-setbase) and 31),
  615. right.location.reference);
  616. end;
  617. else
  618. internalerror(200203312);
  619. end;
  620. end
  621. else
  622. begin
  623. {$ifdef i8086}
  624. register_maybe_adjust_setbase(current_asmdata.CurrAsmList,left.resultdef,left.location,setbase);
  625. cg.getcpuregister(current_asmdata.CurrAsmList,NR_CX);
  626. if TCGSize2Size[left.location.size] > 2 then
  627. left.location.size := OS_16;
  628. cg.a_load_loc_reg(current_asmdata.CurrAsmList,OS_16,left.location,NR_CX);
  629. if (tcgsize2size[right.location.size] < 2) or
  630. (right.location.loc = LOC_CONSTANT) then
  631. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,u16inttype,true);
  632. hreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_16);
  633. emit_const_reg(A_MOV,S_W,1,hreg);
  634. emit_reg_reg(A_SHL,S_W,NR_CL,hreg);
  635. case right.location.loc of
  636. LOC_REGISTER,
  637. LOC_CREGISTER :
  638. begin
  639. emit_reg_reg(A_TEST,S_W,hreg,right.location.register);
  640. end;
  641. LOC_CREFERENCE,
  642. LOC_REFERENCE :
  643. begin
  644. emit_reg_ref(A_TEST,S_W,hreg,right.location.reference);
  645. end;
  646. else
  647. internalerror(2002032210);
  648. end;
  649. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_CX);
  650. location.resflags:=F_NE;
  651. {$else i8086}
  652. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,u32inttype,true);
  653. register_maybe_adjust_setbase(current_asmdata.CurrAsmList,u32inttype,left.location,setbase);
  654. if (tcgsize2size[right.location.size] < 4) or
  655. (right.location.loc = LOC_CONSTANT) then
  656. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,u32inttype,true);
  657. hreg:=left.location.register;
  658. case right.location.loc of
  659. LOC_REGISTER,
  660. LOC_CREGISTER :
  661. begin
  662. emit_reg_reg(A_BT,S_L,hreg,right.location.register);
  663. end;
  664. LOC_CREFERENCE,
  665. LOC_REFERENCE :
  666. begin
  667. emit_reg_ref(A_BT,S_L,hreg,right.location.reference);
  668. end;
  669. else
  670. internalerror(2002032210);
  671. end;
  672. location.resflags:=F_C;
  673. {$endif i8086}
  674. end;
  675. end
  676. else
  677. begin
  678. if right.location.loc=LOC_CONSTANT then
  679. begin
  680. {$ifdef i8086}
  681. location.resflags:=F_NE;
  682. current_asmdata.getjumplabel(l);
  683. current_asmdata.getjumplabel(l2);
  684. { load constants to a register }
  685. if (left.location.loc=LOC_CONSTANT) or
  686. (setbase<>0) then
  687. begin
  688. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,opdef,true);
  689. register_maybe_adjust_setbase(current_asmdata.CurrAsmList,opdef,left.location,setbase);
  690. end;
  691. cg.getcpuregister(current_asmdata.CurrAsmList,NR_CX);
  692. if TCGSize2Size[left.location.size] > 2 then
  693. left.location.size := OS_16;
  694. cg.a_load_loc_reg(current_asmdata.CurrAsmList,OS_16,left.location,NR_CX);
  695. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,OC_BE,15,NR_CX,l);
  696. { set the zero flag }
  697. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_TEST,S_B,0,NR_AL));
  698. cg.a_jmp_always(current_asmdata.CurrAsmList,l2);
  699. hreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_16);
  700. cg.a_label(current_asmdata.CurrAsmList,l);
  701. emit_const_reg(A_MOV,S_W,1,hreg);
  702. emit_reg_reg(A_SHL,S_W,NR_CL,hreg);
  703. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_CX);
  704. emit_const_reg(A_TEST,S_W,right.location.value,hreg);
  705. cg.a_label(current_asmdata.CurrAsmList,l2);
  706. {$else i8086}
  707. location.resflags:=F_C;
  708. current_asmdata.getjumplabel(l);
  709. current_asmdata.getjumplabel(l2);
  710. { load constants to a register }
  711. if (left.location.loc=LOC_CONSTANT) or
  712. (setbase<>0) then
  713. begin
  714. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,opdef,true);
  715. register_maybe_adjust_setbase(current_asmdata.CurrAsmList,opdef,left.location,setbase);
  716. end;
  717. case left.location.loc of
  718. LOC_REGISTER,
  719. LOC_CREGISTER:
  720. begin
  721. hreg:=cg.makeregsize(current_asmdata.CurrAsmList,left.location.register,opsize);
  722. cg.a_load_reg_reg(current_asmdata.CurrAsmList,left.location.size,opsize,left.location.register,hreg);
  723. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,OC_BE,31,hreg,l);
  724. { reset carry flag }
  725. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_CLC,S_NO));
  726. cg.a_jmp_always(current_asmdata.CurrAsmList,l2);
  727. cg.a_label(current_asmdata.CurrAsmList,l);
  728. { We have to load the value into a register because
  729. btl does not accept values only refs or regs (PFV) }
  730. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  731. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_32,right.location.value,hreg2);
  732. emit_reg_reg(A_BT,S_L,hreg,hreg2);
  733. end;
  734. else
  735. begin
  736. emit_const_ref(A_CMP,TCGSize2OpSize[orgopsize],31,left.location.reference);
  737. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_BE,l);
  738. { reset carry flag }
  739. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_CLC,S_NO));
  740. cg.a_jmp_always(current_asmdata.CurrAsmList,l2);
  741. cg.a_label(current_asmdata.CurrAsmList,l);
  742. hreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  743. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_32,OS_32,left.location.reference,hreg);
  744. { We have to load the value into a register because
  745. btl does not accept values only refs or regs (PFV) }
  746. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  747. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_32,right.location.value,hreg2);
  748. emit_reg_reg(A_BT,S_L,hreg,hreg2);
  749. end;
  750. end;
  751. cg.a_label(current_asmdata.CurrAsmList,l2);
  752. {$endif i8086}
  753. end { of right.location.loc=LOC_CONSTANT }
  754. { do search in a normal set which could have >32 elementsm
  755. but also used if the left side contains values > 32 or < 0 }
  756. else if left.location.loc=LOC_CONSTANT then
  757. begin
  758. if (left.location.value<setbase) or (((left.location.value-setbase) shr 3) >= right.resultdef.size) then
  759. {should be caught earlier }
  760. internalerror(2007020201);
  761. location.resflags:=F_NE;
  762. case right.location.loc of
  763. LOC_REFERENCE,LOC_CREFERENCE:
  764. begin
  765. inc(right.location.reference.offset,(left.location.value-setbase) shr 3);
  766. emit_const_ref(A_TEST,S_B,1 shl ((left.location.value-setbase) and 7),right.location.reference);
  767. end;
  768. LOC_REGISTER,LOC_CREGISTER:
  769. begin
  770. emit_const_reg(A_TEST,TCGSize2OpSize[right.location.size],1 shl (left.location.value-setbase),right.location.register);
  771. end;
  772. else
  773. internalerror(2007051901);
  774. end;
  775. end
  776. else
  777. begin
  778. {$ifdef i8086}
  779. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,opdef,false);
  780. register_maybe_adjust_setbase(current_asmdata.CurrAsmList,opdef,left.location,setbase);
  781. if TCGSize2Size[left.location.size] > 2 then
  782. left.location.size := OS_16;
  783. if not use_small then
  784. begin
  785. extra_offset_reg:=cg.getintregister(current_asmdata.CurrAsmList,OS_16);
  786. cg.a_load_loc_reg(current_asmdata.CurrAsmList,OS_16,left.location,extra_offset_reg);
  787. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SHR,OS_16,4,extra_offset_reg);
  788. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SHL,OS_16,1,extra_offset_reg);
  789. end
  790. else
  791. extra_offset_reg:=NR_NO;
  792. cg.getcpuregister(current_asmdata.CurrAsmList,NR_CX);
  793. cg.a_load_loc_reg(current_asmdata.CurrAsmList,OS_16,left.location,NR_CX);
  794. if not use_small then
  795. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_AND,S_B,15,NR_CL));
  796. pleftreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_16);
  797. if (right.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  798. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,opdef,true);
  799. if (opsize >= OS_S8) or { = if signed }
  800. ((left.resultdef.typ=orddef) and
  801. ((torddef(left.resultdef).low < int64(tsetdef(right.resultdef).setbase)) or
  802. (torddef(left.resultdef).high > int64(tsetdef(right.resultdef).setmax)))) or
  803. ((left.resultdef.typ=enumdef) and
  804. ((tenumdef(left.resultdef).min < aint(tsetdef(right.resultdef).setbase)) or
  805. (tenumdef(left.resultdef).max > aint(tsetdef(right.resultdef).setmax)))) then
  806. begin
  807. { we have to check if the value is < 0 or > setmax }
  808. current_asmdata.getjumplabel(l);
  809. current_asmdata.getjumplabel(l2);
  810. { BE will be false for negative values }
  811. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,OC_BE,tsetdef(right.resultdef).setmax-tsetdef(right.resultdef).setbase,pleftreg,l);
  812. { set the zero flag }
  813. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_TEST,S_B,0,NR_AL));
  814. cg.a_jmp_always(current_asmdata.CurrAsmList,l2);
  815. cg.a_label(current_asmdata.CurrAsmList,l);
  816. emit_const_reg(A_MOV,S_W,1,pleftreg);
  817. emit_reg_reg(A_SHL,S_W,NR_CL,pleftreg);
  818. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_CX);
  819. case right.location.loc of
  820. LOC_REGISTER, LOC_CREGISTER :
  821. emit_reg_reg(A_TEST,S_W,pleftreg,right.location.register);
  822. LOC_CREFERENCE, LOC_REFERENCE :
  823. begin
  824. if not use_small then
  825. add_extra_offset(extra_offset_reg,right.location.reference);
  826. emit_reg_ref(A_TEST,S_W,pleftreg,right.location.reference);
  827. end;
  828. else
  829. internalerror(2007020301);
  830. end;
  831. cg.a_label(current_asmdata.CurrAsmList,l2);
  832. location.resflags:=F_NE;
  833. end
  834. else
  835. begin
  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. emit_reg_reg(A_TEST,S_W,pleftreg,right.location.register);
  842. LOC_CREFERENCE, LOC_REFERENCE :
  843. begin
  844. if not use_small then
  845. add_extra_offset(extra_offset_reg,right.location.reference);
  846. emit_reg_ref(A_TEST,S_W,pleftreg,right.location.reference);
  847. end;
  848. else
  849. internalerror(2007020302);
  850. end;
  851. location.resflags:=F_NE;
  852. end;
  853. {$else i8086}
  854. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,opdef,false);
  855. register_maybe_adjust_setbase(current_asmdata.CurrAsmList,opdef,left.location,setbase);
  856. if (right.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  857. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,opdef,true);
  858. pleftreg:=left.location.register;
  859. if (opsize >= OS_S8) or { = if signed }
  860. ((left.resultdef.typ=orddef) and
  861. ((torddef(left.resultdef).low < int64(tsetdef(right.resultdef).setbase)) or
  862. (torddef(left.resultdef).high > int64(tsetdef(right.resultdef).setmax)))) or
  863. ((left.resultdef.typ=enumdef) and
  864. ((tenumdef(left.resultdef).min < aint(tsetdef(right.resultdef).setbase)) or
  865. (tenumdef(left.resultdef).max > aint(tsetdef(right.resultdef).setmax)))) then
  866. begin
  867. { we have to check if the value is < 0 or > setmax }
  868. current_asmdata.getjumplabel(l);
  869. current_asmdata.getjumplabel(l2);
  870. { BE will be false for negative values }
  871. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,OC_BE,tsetdef(right.resultdef).setmax-tsetdef(right.resultdef).setbase,pleftreg,l);
  872. { reset carry flag }
  873. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_CLC,S_NO));
  874. cg.a_jmp_always(current_asmdata.CurrAsmList,l2);
  875. cg.a_label(current_asmdata.CurrAsmList,l);
  876. pleftreg:=left.location.register;
  877. case right.location.loc of
  878. LOC_REGISTER, LOC_CREGISTER :
  879. emit_reg_reg(A_BT,S_L,pleftreg,right.location.register);
  880. LOC_CREFERENCE, LOC_REFERENCE :
  881. emit_reg_ref(A_BT,S_L,pleftreg,right.location.reference);
  882. else
  883. internalerror(2007020301);
  884. end;
  885. cg.a_label(current_asmdata.CurrAsmList,l2);
  886. location.resflags:=F_C;
  887. end
  888. else
  889. begin
  890. case right.location.loc of
  891. LOC_REGISTER, LOC_CREGISTER :
  892. emit_reg_reg(A_BT,S_L,pleftreg,right.location.register);
  893. LOC_CREFERENCE, LOC_REFERENCE :
  894. emit_reg_ref(A_BT,S_L,pleftreg,right.location.reference);
  895. else
  896. internalerror(2007020302);
  897. end;
  898. location.resflags:=F_C;
  899. end;
  900. {$endif i8086}
  901. end;
  902. end;
  903. end;
  904. if not genjumps then
  905. location_freetemp(current_asmdata.CurrAsmList,right.location);
  906. end;
  907. begin
  908. cinnode:=tx86innode;
  909. ccasenode:=tx86casenode;
  910. end.