nx86set.pas 37 KB

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