nx86set.pas 38 KB

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