nx86set.pas 42 KB

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