nx86set.pas 38 KB

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