nx86set.pas 37 KB

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