nx86set.pas 40 KB

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