nx86set.pas 40 KB

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