nx86set.pas 41 KB

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