nx86set.pas 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  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. href.scalefactor:=sizeof(aint);
  104. emit_ref(A_JMP,S_NO,href);
  105. { generate jump table }
  106. if (target_info.system in [system_i386_darwin,system_i386_iphonesim]) then
  107. begin
  108. jtlist:=current_asmdata.asmlists[al_const];
  109. sectype:=sec_rodata;
  110. end
  111. else
  112. begin
  113. jtlist:=current_procinfo.aktlocaldata;
  114. sectype:=sec_data;
  115. end;
  116. new_section(jtlist,sectype,current_procinfo.procdef.mangledname,sizeof(aint));
  117. jtlist.concat(Tai_label.Create(table));
  118. genitem(jtlist,hp);
  119. end;
  120. procedure tx86casenode.genlinearlist(hp : pcaselabel);
  121. var
  122. first : boolean;
  123. lastrange : boolean;
  124. last : TConstExprInt;
  125. cond_lt,cond_le : tresflags;
  126. opcgsize: tcgsize;
  127. procedure genitem(t : pcaselabel);
  128. begin
  129. if assigned(t^.less) then
  130. genitem(t^.less);
  131. { need we to test the first value }
  132. if first and (t^._low>get_min_value(left.resultdef)) then
  133. begin
  134. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opcgsize,jmp_lt,aint(t^._low.svalue),hregister,elselabel);
  135. end;
  136. if t^._low=t^._high then
  137. begin
  138. if t^._low-last=0 then
  139. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, opcgsize, OC_EQ,0,hregister,blocklabel(t^.blockid))
  140. else
  141. begin
  142. cg.a_op_const_reg(current_asmdata.CurrAsmList, OP_SUB, opcgsize, aint(t^._low.svalue-last.svalue), hregister);
  143. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_E,blocklabel(t^.blockid));
  144. end;
  145. last:=t^._low;
  146. lastrange:=false;
  147. end
  148. else
  149. begin
  150. { it begins with the smallest label, if the value }
  151. { is even smaller then jump immediately to the }
  152. { ELSE-label }
  153. if first then
  154. begin
  155. { have we to ajust the first value ? }
  156. if (t^._low>get_min_value(left.resultdef)) or (get_min_value(left.resultdef)<>0) then
  157. cg.a_op_const_reg(current_asmdata.CurrAsmList, OP_SUB, opcgsize, aint(t^._low.svalue), hregister);
  158. end
  159. else
  160. begin
  161. { if there is no unused label between the last and the }
  162. { present label then the lower limit can be checked }
  163. { immediately. else check the range in between: }
  164. { we need to use A_SUB, if cond_lt uses the carry flags
  165. because A_DEC does not set the correct flags, therefor
  166. using a_op_const_reg(OP_SUB) is not possible }
  167. 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
  168. emit_const_reg(A_SUB,TCGSize2OpSize[opcgsize],aint(t^._low.svalue-last.svalue),hregister)
  169. else
  170. cg.a_op_const_reg(current_asmdata.CurrAsmList, OP_SUB, opcgsize, aint(t^._low.svalue-last.svalue), hregister);
  171. { no jump necessary here if the new range starts at
  172. at the value following the previous one }
  173. if ((t^._low-last) <> 1) or
  174. (not lastrange) then
  175. cg.a_jmp_flags(current_asmdata.CurrAsmList,cond_lt,elselabel);
  176. end;
  177. { we need to use A_SUB, if cond_le 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_le in [F_C,F_NC,F_A,F_AE,F_B,F_BE]) and (aint(t^._high.svalue-t^._low.svalue)=1) then
  181. emit_const_reg(A_SUB,TCGSize2OpSize[opcgsize],aint(t^._high.svalue-t^._low.svalue),hregister)
  182. else
  183. cg.a_op_const_reg(current_asmdata.CurrAsmList, OP_SUB, opcgsize, aint(t^._high.svalue-t^._low.svalue), hregister);
  184. cg.a_jmp_flags(current_asmdata.CurrAsmList,cond_le,blocklabel(t^.blockid));
  185. last:=t^._high;
  186. lastrange:=true;
  187. end;
  188. first:=false;
  189. if assigned(t^.greater) then
  190. genitem(t^.greater);
  191. end;
  192. begin
  193. opcgsize:=def_cgsize(opsize);
  194. if with_sign then
  195. begin
  196. cond_lt:=F_L;
  197. cond_le:=F_LE;
  198. end
  199. else
  200. begin
  201. cond_lt:=F_B;
  202. cond_le:=F_BE;
  203. end;
  204. { do we need to generate cmps? }
  205. if (with_sign and (min_label<0)) then
  206. genlinearcmplist(hp)
  207. else
  208. begin
  209. last:=0;
  210. lastrange:=false;
  211. first:=true;
  212. genitem(hp);
  213. cg.a_jmp_always(current_asmdata.CurrAsmList,elselabel);
  214. end;
  215. end;
  216. {*****************************************************************************
  217. TX86INNODE
  218. *****************************************************************************}
  219. function tx86innode.pass_1 : tnode;
  220. begin
  221. result:=nil;
  222. { this is the only difference from the generic version }
  223. expectloc:=LOC_FLAGS;
  224. firstpass(right);
  225. firstpass(left);
  226. if codegenerror then
  227. exit;
  228. end;
  229. procedure tx86innode.pass_generate_code;
  230. type
  231. Tsetpart=record
  232. range : boolean; {Part is a range.}
  233. start,stop : byte; {Start/stop when range; Stop=element when an element.}
  234. end;
  235. var
  236. hreg,hreg2,
  237. pleftreg : tregister;
  238. opsize : tcgsize;
  239. opdef : torddef;
  240. orgopsize : tcgsize;
  241. setparts : array[1..8] of Tsetpart;
  242. setbase : aint;
  243. adjustment : longint;
  244. l,l2 : tasmlabel;
  245. i,numparts : byte;
  246. genjumps,
  247. use_small,
  248. ranges : boolean;
  249. {$ifdef CORRECT_SET_IN_FPC}
  250. AM : tasmop;
  251. {$endif CORRECT_SET_IN_FPC}
  252. function analizeset(Aset:pconstset;is_small:boolean):boolean;
  253. var
  254. compares,maxcompares:word;
  255. i:byte;
  256. begin
  257. if tnormalset(Aset^)=[] then
  258. {The expression...
  259. if expr in []
  260. ...is allways false. It should be optimized away in the
  261. resultdef pass, and thus never occur here. Since we
  262. do generate wrong code for it, do internalerror.}
  263. internalerror(2002072301);
  264. analizeset:=false;
  265. ranges:=false;
  266. numparts:=0;
  267. compares:=0;
  268. { Lots of comparisions take a lot of time, so do not allow
  269. too much comparisions. 8 comparisions are, however, still
  270. smalller than emitting the set }
  271. if cs_opt_size in current_settings.optimizerswitches then
  272. maxcompares:=8
  273. else
  274. maxcompares:=5;
  275. { when smallset is possible allow only 3 compares the smallset
  276. code is for littlesize also smaller when more compares are used }
  277. if is_small then
  278. maxcompares:=3;
  279. for i:=0 to 255 do
  280. if i in tnormalset(Aset^) then
  281. begin
  282. if (numparts=0) or (i<>setparts[numparts].stop+1) then
  283. begin
  284. {Set element is a separate element.}
  285. inc(compares);
  286. if compares>maxcompares then
  287. exit;
  288. inc(numparts);
  289. setparts[numparts].range:=false;
  290. setparts[numparts].stop:=i;
  291. end
  292. else
  293. {Set element is part of a range.}
  294. if not setparts[numparts].range then
  295. begin
  296. {Transform an element into a range.}
  297. setparts[numparts].range:=true;
  298. setparts[numparts].start:=setparts[numparts].stop;
  299. setparts[numparts].stop:=i;
  300. ranges := true;
  301. end
  302. else
  303. begin
  304. {Extend a range.}
  305. setparts[numparts].stop:=i;
  306. end;
  307. end;
  308. analizeset:=true;
  309. end;
  310. begin
  311. { We check first if we can generate jumps, this can be done
  312. because the resultdef is already set in firstpass }
  313. { check if we can use smallset operation using btl which is limited
  314. to 32 bits, the left side may also not contain higher values or be signed !! }
  315. use_small:=is_smallset(right.resultdef) and
  316. not is_signed(left.resultdef) and
  317. ((left.resultdef.typ=orddef) and (torddef(left.resultdef).high.svalue<32) or
  318. (left.resultdef.typ=enumdef) and (tenumdef(left.resultdef).max<32));
  319. { Can we generate jumps? Possible for all types of sets }
  320. genjumps:=(right.nodetype=setconstn) and
  321. analizeset(tsetconstnode(right).value_set,use_small);
  322. { calculate both operators }
  323. { the complex one first }
  324. { not in case of genjumps, because then we don't secondpass }
  325. { right at all (so we have to make sure that "right" really is }
  326. { "right" and not "swapped left" in that case) }
  327. if not(genjumps) then
  328. firstcomplex(self);
  329. secondpass(left);
  330. { Only process the right if we are not generating jumps }
  331. if not genjumps then
  332. begin
  333. secondpass(right);
  334. end;
  335. if codegenerror then
  336. exit;
  337. { ofcourse not commutative }
  338. if nf_swapped in flags then
  339. swapleftright;
  340. orgopsize := def_cgsize(left.resultdef);
  341. opsize := OS_32;
  342. if is_signed(left.resultdef) then
  343. opsize := tcgsize(ord(opsize)+(ord(OS_S8)-ord(OS_8)));
  344. opdef:=hlcg.tcgsize2orddef(opsize);
  345. if not(left.location.loc in [LOC_REGISTER,LOC_CREGISTER,LOC_REFERENCE,LOC_CREFERENCE,LOC_CONSTANT]) then
  346. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,opdef,true);
  347. if (right.location.loc in [LOC_SUBSETREG,LOC_CSUBSETREG]) then
  348. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,left.resultdef,opdef,true);
  349. if genjumps then
  350. begin
  351. { It gives us advantage to check for the set elements
  352. separately instead of using the SET_IN_BYTE procedure.
  353. To do: Build in support for LOC_JUMP }
  354. { load and zero or sign extend as necessary }
  355. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,opdef,false);
  356. pleftreg:=left.location.register;
  357. { Get a label to jump to the end }
  358. location_reset(location,LOC_FLAGS,OS_NO);
  359. { It's better to use the zero flag when there are
  360. no ranges }
  361. if ranges then
  362. location.resflags:=F_C
  363. else
  364. location.resflags:=F_E;
  365. current_asmdata.getjumplabel(l);
  366. { how much have we already substracted from the x in the }
  367. { "x in [y..z]" expression }
  368. adjustment := 0;
  369. for i:=1 to numparts do
  370. if setparts[i].range then
  371. { use fact that a <= x <= b <=> cardinal(x-a) <= cardinal(b-a) }
  372. begin
  373. { is the range different from all legal values? }
  374. if (setparts[i].stop-setparts[i].start <> 255) or not (orgopsize = OS_8) then
  375. begin
  376. { yes, is the lower bound <> 0? }
  377. if (setparts[i].start <> 0) then
  378. begin
  379. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,opdef,false);
  380. hreg:=left.location.register;
  381. pleftreg:=hreg;
  382. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SUB,opsize,setparts[i].start-adjustment,pleftreg);
  383. end;
  384. { new total value substracted from x: }
  385. { adjustment + (setparts[i].start - adjustment) }
  386. adjustment := setparts[i].start;
  387. { check if result < b-a+1 (not "result <= b-a", since }
  388. { we need a carry in case the element is in the range }
  389. { (this will never overflow since we check at the }
  390. { beginning whether stop-start <> 255) }
  391. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,OC_B,setparts[i].stop-setparts[i].start+1,pleftreg,l);
  392. end
  393. else
  394. { if setparts[i].start = 0 and setparts[i].stop = 255, }
  395. { it's always true since "in" is only allowed for bytes }
  396. begin
  397. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_STC,S_NO));
  398. cg.a_jmp_always(current_asmdata.CurrAsmList,l);
  399. end;
  400. end
  401. else
  402. begin
  403. { Emit code to check if left is an element }
  404. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,TCGSize2OpSize[opsize],setparts[i].stop-adjustment,
  405. pleftreg));
  406. { Result should be in carry flag when ranges are used }
  407. if ranges then
  408. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_STC,S_NO));
  409. { If found, jump to end }
  410. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_E,l);
  411. end;
  412. if ranges and
  413. { if the last one was a range, the carry flag is already }
  414. { set appropriately }
  415. not(setparts[numparts].range) then
  416. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_CLC,S_NO));
  417. { To compensate for not doing a second pass }
  418. right.location.reference.symbol:=nil;
  419. { Now place the end label }
  420. cg.a_label(current_asmdata.CurrAsmList,l);
  421. end
  422. else
  423. begin
  424. location_reset(location,LOC_FLAGS,OS_NO);
  425. setbase:=tsetdef(right.resultdef).setbase;
  426. { We will now generated code to check the set itself, no jmps,
  427. handle smallsets separate, because it allows faster checks }
  428. if use_small then
  429. begin
  430. if left.location.loc=LOC_CONSTANT then
  431. begin
  432. location.resflags:=F_NE;
  433. case right.location.loc of
  434. LOC_REGISTER,
  435. LOC_CREGISTER:
  436. begin
  437. emit_const_reg(A_TEST,TCGSize2OpSize[right.location.size],
  438. 1 shl ((left.location.value-setbase) and 31),right.location.register);
  439. end;
  440. LOC_REFERENCE,
  441. LOC_CREFERENCE :
  442. begin
  443. emit_const_ref(A_TEST,TCGSize2OpSize[right.location.size],1 shl ((left.location.value-setbase) and 31),
  444. right.location.reference);
  445. end;
  446. else
  447. internalerror(200203312);
  448. end;
  449. end
  450. else
  451. begin
  452. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,u32inttype,true);
  453. register_maybe_adjust_setbase(current_asmdata.CurrAsmList,left.location,setbase);
  454. if (tcgsize2size[right.location.size] < 4) or
  455. (right.location.loc = LOC_CONSTANT) then
  456. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,u32inttype,true);
  457. hreg:=left.location.register;
  458. case right.location.loc of
  459. LOC_REGISTER,
  460. LOC_CREGISTER :
  461. begin
  462. emit_reg_reg(A_BT,S_L,hreg,right.location.register);
  463. end;
  464. LOC_CREFERENCE,
  465. LOC_REFERENCE :
  466. begin
  467. emit_reg_ref(A_BT,S_L,hreg,right.location.reference);
  468. end;
  469. else
  470. internalerror(2002032210);
  471. end;
  472. location.resflags:=F_C;
  473. end;
  474. end
  475. else
  476. begin
  477. if right.location.loc=LOC_CONSTANT then
  478. begin
  479. location.resflags:=F_C;
  480. current_asmdata.getjumplabel(l);
  481. current_asmdata.getjumplabel(l2);
  482. { load constants to a register }
  483. if (left.location.loc=LOC_CONSTANT) or
  484. (setbase<>0) then
  485. begin
  486. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,opdef,true);
  487. register_maybe_adjust_setbase(current_asmdata.CurrAsmList,left.location,setbase);
  488. end;
  489. case left.location.loc of
  490. LOC_REGISTER,
  491. LOC_CREGISTER:
  492. begin
  493. hreg:=cg.makeregsize(current_asmdata.CurrAsmList,left.location.register,opsize);
  494. cg.a_load_reg_reg(current_asmdata.CurrAsmList,left.location.size,opsize,left.location.register,hreg);
  495. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,OC_BE,31,hreg,l);
  496. { reset carry flag }
  497. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_CLC,S_NO));
  498. cg.a_jmp_always(current_asmdata.CurrAsmList,l2);
  499. cg.a_label(current_asmdata.CurrAsmList,l);
  500. { We have to load the value into a register because
  501. btl does not accept values only refs or regs (PFV) }
  502. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  503. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_32,right.location.value,hreg2);
  504. emit_reg_reg(A_BT,S_L,hreg,hreg2);
  505. end;
  506. else
  507. begin
  508. emit_const_ref(A_CMP,TCGSize2OpSize[orgopsize],31,left.location.reference);
  509. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_BE,l);
  510. { reset carry flag }
  511. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_CLC,S_NO));
  512. cg.a_jmp_always(current_asmdata.CurrAsmList,l2);
  513. cg.a_label(current_asmdata.CurrAsmList,l);
  514. hreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  515. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_32,OS_32,left.location.reference,hreg);
  516. { We have to load the value into a register because
  517. btl does not accept values only refs or regs (PFV) }
  518. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  519. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_32,right.location.value,hreg2);
  520. emit_reg_reg(A_BT,S_L,hreg,hreg2);
  521. end;
  522. end;
  523. cg.a_label(current_asmdata.CurrAsmList,l2);
  524. end { of right.location.loc=LOC_CONSTANT }
  525. { do search in a normal set which could have >32 elementsm
  526. but also used if the left side contains values > 32 or < 0 }
  527. else if left.location.loc=LOC_CONSTANT then
  528. begin
  529. if (left.location.value<setbase) or (((left.location.value-setbase) shr 3) >= right.resultdef.size) then
  530. {should be caught earlier }
  531. internalerror(2007020201);
  532. location.resflags:=F_NE;
  533. case right.location.loc of
  534. LOC_REFERENCE,LOC_CREFERENCE:
  535. begin
  536. inc(right.location.reference.offset,(left.location.value-setbase) shr 3);
  537. emit_const_ref(A_TEST,S_B,1 shl (left.location.value and 7),right.location.reference);
  538. end;
  539. LOC_REGISTER,LOC_CREGISTER:
  540. begin
  541. emit_const_reg(A_TEST,TCGSize2OpSize[right.location.size],1 shl (left.location.value-setbase),right.location.register);
  542. end;
  543. else
  544. internalerror(2007051901);
  545. end;
  546. end
  547. else
  548. begin
  549. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,opdef,false);
  550. register_maybe_adjust_setbase(current_asmdata.CurrAsmList,left.location,setbase);
  551. if (right.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  552. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,opdef,true);
  553. pleftreg:=left.location.register;
  554. if (opsize >= OS_S8) or { = if signed }
  555. ((left.resultdef.typ=orddef) and
  556. ((torddef(left.resultdef).low < int64(tsetdef(right.resultdef).setbase)) or
  557. (torddef(left.resultdef).high > int64(tsetdef(right.resultdef).setmax)))) or
  558. ((left.resultdef.typ=enumdef) and
  559. ((tenumdef(left.resultdef).min < aint(tsetdef(right.resultdef).setbase)) or
  560. (tenumdef(left.resultdef).max > aint(tsetdef(right.resultdef).setmax)))) then
  561. begin
  562. { we have to check if the value is < 0 or > setmax }
  563. current_asmdata.getjumplabel(l);
  564. current_asmdata.getjumplabel(l2);
  565. { BE will be false for negative values }
  566. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,OC_BE,tsetdef(right.resultdef).setmax-tsetdef(right.resultdef).setbase,pleftreg,l);
  567. { reset carry flag }
  568. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_CLC,S_NO));
  569. cg.a_jmp_always(current_asmdata.CurrAsmList,l2);
  570. cg.a_label(current_asmdata.CurrAsmList,l);
  571. pleftreg:=left.location.register;
  572. case right.location.loc of
  573. LOC_REGISTER, LOC_CREGISTER :
  574. emit_reg_reg(A_BT,S_L,pleftreg,right.location.register);
  575. LOC_CREFERENCE, LOC_REFERENCE :
  576. emit_reg_ref(A_BT,S_L,pleftreg,right.location.reference);
  577. else
  578. internalerror(2007020301);
  579. end;
  580. cg.a_label(current_asmdata.CurrAsmList,l2);
  581. location.resflags:=F_C;
  582. end
  583. else
  584. begin
  585. case right.location.loc of
  586. LOC_REGISTER, LOC_CREGISTER :
  587. emit_reg_reg(A_BT,S_L,pleftreg,right.location.register);
  588. LOC_CREFERENCE, LOC_REFERENCE :
  589. emit_reg_ref(A_BT,S_L,pleftreg,right.location.reference);
  590. else
  591. internalerror(2007020302);
  592. end;
  593. location.resflags:=F_C;
  594. end;
  595. end;
  596. end;
  597. end;
  598. if not genjumps then
  599. location_freetemp(current_asmdata.CurrAsmList,right.location);
  600. end;
  601. begin
  602. cinnode:=tx86innode;
  603. ccasenode:=tx86casenode;
  604. end.