nx86set.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Generate x86 assembler for in/case nodes
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit nx86set;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. node,nset,pass_1,ncgset;
  23. type
  24. tx86innode = class(tinnode)
  25. procedure pass_2;override;
  26. function pass_1 : tnode;override;
  27. end;
  28. implementation
  29. uses
  30. globtype,systems,
  31. verbose,globals,
  32. symconst,symdef,defutil,
  33. aasmbase,aasmtai,aasmcpu,
  34. cgbase,pass_2,
  35. ncon,
  36. cpubase,cpuinfo,procinfo,
  37. cga,cgutils,cgobj,ncgutil,
  38. cgx86;
  39. {*****************************************************************************
  40. TX86INNODE
  41. *****************************************************************************}
  42. function tx86innode.pass_1 : tnode;
  43. begin
  44. result:=nil;
  45. { this is the only difference from the generic version }
  46. expectloc:=LOC_FLAGS;
  47. firstpass(right);
  48. firstpass(left);
  49. if codegenerror then
  50. exit;
  51. left_right_max;
  52. { a smallset needs maybe an misc. register }
  53. if (left.nodetype<>ordconstn) and
  54. not(right.location.loc in [LOC_CREGISTER,LOC_REGISTER]) and
  55. (right.registersint<1) then
  56. inc(registersint);
  57. end;
  58. procedure tx86innode.pass_2;
  59. type
  60. Tsetpart=record
  61. range : boolean; {Part is a range.}
  62. start,stop : byte; {Start/stop when range; Stop=element when an element.}
  63. end;
  64. var
  65. genjumps,
  66. use_small,
  67. ranges : boolean;
  68. hr,hr2,
  69. pleftreg : tregister;
  70. href : treference;
  71. opsize : tcgsize;
  72. setparts : array[1..8] of Tsetpart;
  73. i,numparts : byte;
  74. adjustment : longint;
  75. l,l2 : tasmlabel;
  76. r : Tregister;
  77. {$ifdef CORRECT_SET_IN_FPC}
  78. AM : tasmop;
  79. {$endif CORRECT_SET_IN_FPC}
  80. function analizeset(Aset:pconstset;is_small:boolean):boolean;
  81. var
  82. compares,maxcompares:word;
  83. i:byte;
  84. begin
  85. if tnormalset(Aset^)=[] then
  86. {The expression...
  87. if expr in []
  88. ...is allways false. It should be optimized away in the
  89. resulttype pass, and thus never occur here. Since we
  90. do generate wrong code for it, do internalerror.}
  91. internalerror(2002072301);
  92. analizeset:=false;
  93. ranges:=false;
  94. numparts:=0;
  95. compares:=0;
  96. { Lots of comparisions take a lot of time, so do not allow
  97. too much comparisions. 8 comparisions are, however, still
  98. smalller than emitting the set }
  99. if cs_littlesize in aktglobalswitches then
  100. maxcompares:=8
  101. else
  102. maxcompares:=5;
  103. { when smallset is possible allow only 3 compares the smallset
  104. code is for littlesize also smaller when more compares are used }
  105. if is_small then
  106. maxcompares:=3;
  107. for i:=0 to 255 do
  108. if i in tnormalset(Aset^) then
  109. begin
  110. if (numparts=0) or (i<>setparts[numparts].stop+1) then
  111. begin
  112. {Set element is a separate element.}
  113. inc(compares);
  114. if compares>maxcompares then
  115. exit;
  116. inc(numparts);
  117. setparts[numparts].range:=false;
  118. setparts[numparts].stop:=i;
  119. end
  120. else
  121. {Set element is part of a range.}
  122. if not setparts[numparts].range then
  123. begin
  124. {Transform an element into a range.}
  125. setparts[numparts].range:=true;
  126. setparts[numparts].start:=setparts[numparts].stop;
  127. setparts[numparts].stop:=i;
  128. ranges := true;
  129. { there's only one compare per range anymore. Only a }
  130. { sub is added, but that's much faster than a }
  131. { cmp/jcc combo so neglect its effect }
  132. { inc(compares);
  133. if compares>maxcompares then
  134. exit; }
  135. end
  136. else
  137. begin
  138. {Extend a range.}
  139. setparts[numparts].stop:=i;
  140. end;
  141. end;
  142. analizeset:=true;
  143. end;
  144. begin
  145. { We check first if we can generate jumps, this can be done
  146. because the resulttype.def is already set in firstpass }
  147. { check if we can use smallset operation using btl which is limited
  148. to 32 bits, the left side may also not contain higher values !! }
  149. use_small:=(tsetdef(right.resulttype.def).settype=smallset) and
  150. ((left.resulttype.def.deftype=orddef) and (torddef(left.resulttype.def).high<=32) or
  151. (left.resulttype.def.deftype=enumdef) and (tenumdef(left.resulttype.def).max<=32));
  152. { Can we generate jumps? Possible for all types of sets }
  153. genjumps:=(right.nodetype=setconstn) and
  154. analizeset(tsetconstnode(right).value_set,use_small);
  155. { calculate both operators }
  156. { the complex one first }
  157. firstcomplex(self);
  158. secondpass(left);
  159. { Only process the right if we are not generating jumps }
  160. if not genjumps then
  161. begin
  162. secondpass(right);
  163. end;
  164. if codegenerror then
  165. exit;
  166. { ofcourse not commutative }
  167. if nf_swaped in flags then
  168. swapleftright;
  169. if genjumps then
  170. begin
  171. { It gives us advantage to check for the set elements
  172. separately instead of using the SET_IN_BYTE procedure.
  173. To do: Build in support for LOC_JUMP }
  174. opsize := def_cgsize(left.resulttype.def);
  175. { If register is used, use only lower 8 bits }
  176. if left.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  177. begin
  178. { for ranges we always need a 32bit register, because then we }
  179. { use the register as base in a reference (JM) }
  180. if ranges then
  181. begin
  182. pleftreg:=cg.makeregsize(exprasmlist,left.location.register,OS_INT);
  183. cg.a_load_reg_reg(exprasmlist,left.location.size,OS_INT,left.location.register,pleftreg);
  184. if opsize<>OS_INT then
  185. cg.a_op_const_reg(exprasmlist,OP_AND,OS_INT,255,pleftreg);
  186. opsize:=OS_INT;
  187. end
  188. else
  189. { otherwise simply use the lower 8 bits (no "and" }
  190. { necessary this way) (JM) }
  191. begin
  192. pleftreg:=cg.makeregsize(exprasmlist,left.location.register,OS_8);
  193. opsize := OS_8;
  194. end;
  195. end
  196. else
  197. begin
  198. { load the value in a register }
  199. pleftreg:=cg.getintregister(exprasmlist,OS_32);
  200. opsize:=OS_32;
  201. cg.a_load_ref_reg(exprasmlist,OS_8,OS_32,left.location.reference,pleftreg);
  202. location_release(exprasmlist,left.location);
  203. end;
  204. { Get a label to jump to the end }
  205. location_reset(location,LOC_FLAGS,OS_NO);
  206. { It's better to use the zero flag when there are
  207. no ranges }
  208. if ranges then
  209. location.resflags:=F_C
  210. else
  211. location.resflags:=F_E;
  212. objectlibrary.getlabel(l);
  213. { how much have we already substracted from the x in the }
  214. { "x in [y..z]" expression }
  215. adjustment := 0;
  216. r:=NR_NO;
  217. for i:=1 to numparts do
  218. if setparts[i].range then
  219. { use fact that a <= x <= b <=> cardinal(x-a) <= cardinal(b-a) }
  220. begin
  221. { is the range different from all legal values? }
  222. if (setparts[i].stop-setparts[i].start <> 255) then
  223. begin
  224. { yes, is the lower bound <> 0? }
  225. if (setparts[i].start <> 0) then
  226. { we're going to substract from the left register, }
  227. { so in case of a LOC_CREGISTER first move the value }
  228. { to edi (not done before because now we can do the }
  229. { move and substract in one instruction with LEA) }
  230. if (left.location.loc = LOC_CREGISTER) then
  231. begin
  232. cg.ungetregister(exprasmlist,pleftreg);
  233. r:=cg.getintregister(exprasmlist,OS_32);
  234. reference_reset_base(href,pleftreg,-setparts[i].start);
  235. cg.a_loadaddr_ref_reg(exprasmlist,href,r);
  236. { only now change pleftreg since previous value is }
  237. { still used in previous instruction }
  238. pleftreg := r;
  239. opsize := OS_32;
  240. end
  241. else
  242. begin
  243. { otherwise, the value is already in a register }
  244. { that can be modified }
  245. cg.a_op_const_reg(exprasmlist,OP_SUB,opsize,setparts[i].start-adjustment,pleftreg);
  246. end;
  247. { new total value substracted from x: }
  248. { adjustment + (setparts[i].start - adjustment) }
  249. adjustment := setparts[i].start;
  250. { check if result < b-a+1 (not "result <= b-a", since }
  251. { we need a carry in case the element is in the range }
  252. { (this will never overflow since we check at the }
  253. { beginning whether stop-start <> 255) }
  254. cg.a_cmp_const_reg_label(exprasmlist,opsize,OC_B,setparts[i].stop-setparts[i].start+1,pleftreg,l);
  255. end
  256. else
  257. { if setparts[i].start = 0 and setparts[i].stop = 255, }
  258. { it's always true since "in" is only allowed for bytes }
  259. begin
  260. exprasmlist.concat(taicpu.op_none(A_STC,S_NO));
  261. cg.a_jmp_always(exprasmlist,l);
  262. end;
  263. end
  264. else
  265. begin
  266. { Emit code to check if left is an element }
  267. exprasmlist.concat(taicpu.op_const_reg(A_CMP,TCGSize2OpSize[opsize],setparts[i].stop-adjustment,
  268. pleftreg));
  269. { Result should be in carry flag when ranges are used }
  270. if ranges then
  271. exprasmlist.concat(taicpu.op_none(A_STC,S_NO));
  272. { If found, jump to end }
  273. cg.a_jmp_flags(exprasmlist,F_E,l);
  274. end;
  275. if ranges and
  276. { if the last one was a range, the carry flag is already }
  277. { set appropriately }
  278. not(setparts[numparts].range) then
  279. exprasmlist.concat(taicpu.op_none(A_CLC,S_NO));
  280. { To compensate for not doing a second pass }
  281. right.location.reference.symbol:=nil;
  282. { Now place the end label }
  283. cg.a_label(exprasmlist,l);
  284. cg.ungetregister(exprasmlist,pleftreg);
  285. if r<>NR_NO then
  286. cg.ungetregister(exprasmlist,r);
  287. end
  288. else
  289. begin
  290. location_reset(location,LOC_FLAGS,OS_NO);
  291. { We will now generated code to check the set itself, no jmps,
  292. handle smallsets separate, because it allows faster checks }
  293. if use_small then
  294. begin
  295. if left.nodetype=ordconstn then
  296. begin
  297. location.resflags:=F_NE;
  298. case right.location.loc of
  299. LOC_REGISTER,
  300. LOC_CREGISTER:
  301. begin
  302. emit_const_reg(A_TEST,S_L,
  303. 1 shl (tordconstnode(left).value and 31),right.location.register);
  304. end;
  305. LOC_REFERENCE,
  306. LOC_CREFERENCE :
  307. begin
  308. emit_const_ref(A_TEST,S_L,1 shl (tordconstnode(left).value and 31),
  309. right.location.reference);
  310. end;
  311. else
  312. internalerror(200203312);
  313. end;
  314. location_release(exprasmlist,right.location);
  315. end
  316. else
  317. begin
  318. case left.location.loc of
  319. LOC_REGISTER,
  320. LOC_CREGISTER:
  321. begin
  322. hr:=cg.makeregsize(exprasmlist,left.location.register,OS_32);
  323. cg.a_load_reg_reg(exprasmlist,left.location.size,OS_32,left.location.register,hr);
  324. end;
  325. else
  326. begin
  327. { the set element isn't never samller than a byte
  328. and because it's a small set we need only 5 bits
  329. but 8 bits are easier to load }
  330. hr:=cg.getintregister(exprasmlist,OS_32);
  331. cg.a_load_ref_reg(exprasmlist,OS_8,OS_32,left.location.reference,hr);
  332. location_release(exprasmlist,left.location);
  333. end;
  334. end;
  335. case right.location.loc of
  336. LOC_REGISTER,
  337. LOC_CREGISTER :
  338. begin
  339. emit_reg_reg(A_BT,S_L,hr,
  340. right.location.register);
  341. cg.ungetregister(exprasmlist,right.location.register);
  342. end;
  343. LOC_CONSTANT :
  344. begin
  345. { We have to load the value into a register because
  346. btl does not accept values only refs or regs (PFV) }
  347. hr2:=cg.getintregister(exprasmlist,OS_32);
  348. cg.a_load_const_reg(exprasmlist,OS_32,right.location.value,hr2);
  349. emit_reg_reg(A_BT,S_L,hr,hr2);
  350. cg.ungetregister(exprasmlist,hr2);
  351. end;
  352. LOC_CREFERENCE,
  353. LOC_REFERENCE :
  354. begin
  355. location_release(exprasmlist,right.location);
  356. emit_reg_ref(A_BT,S_L,hr,right.location.reference);
  357. end;
  358. else
  359. internalerror(2002032210);
  360. end;
  361. { simply to indicate EDI is deallocated here too (JM) }
  362. cg.ungetregister(exprasmlist,hr);
  363. location.resflags:=F_C;
  364. end;
  365. end
  366. else
  367. begin
  368. if right.location.loc=LOC_CONSTANT then
  369. begin
  370. location.resflags:=F_C;
  371. objectlibrary.getlabel(l);
  372. objectlibrary.getlabel(l2);
  373. { load constants to a register }
  374. if left.nodetype=ordconstn then
  375. location_force_reg(exprasmlist,left.location,OS_INT,true);
  376. case left.location.loc of
  377. LOC_REGISTER,
  378. LOC_CREGISTER:
  379. begin
  380. hr:=cg.makeregsize(exprasmlist,left.location.register,OS_32);
  381. cg.a_load_reg_reg(exprasmlist,left.location.size,OS_32,left.location.register,hr);
  382. cg.a_cmp_const_reg_label(exprasmlist,OS_32,OC_BE,31,hr,l);
  383. { reset carry flag }
  384. exprasmlist.concat(taicpu.op_none(A_CLC,S_NO));
  385. cg.a_jmp_always(exprasmlist,l2);
  386. cg.a_label(exprasmlist,l);
  387. { We have to load the value into a register because
  388. btl does not accept values only refs or regs (PFV) }
  389. hr2:=cg.getintregister(exprasmlist,OS_32);
  390. cg.a_load_const_reg(exprasmlist,OS_32,right.location.value,hr2);
  391. emit_reg_reg(A_BT,S_L,hr,hr2);
  392. cg.ungetregister(exprasmlist,hr2);
  393. end;
  394. else
  395. begin
  396. {$ifdef CORRECT_SET_IN_FPC}
  397. if m_tp in aktmodeswitches then
  398. begin
  399. {***WARNING only correct if
  400. reference is 32 bits (PM) *****}
  401. emit_const_ref(A_CMP,S_L,31,reference_copy(left.location.reference));
  402. end
  403. else
  404. {$endif CORRECT_SET_IN_FPC}
  405. begin
  406. emit_const_ref(A_CMP,S_B,31,left.location.reference);
  407. end;
  408. cg.a_jmp_flags(exprasmlist,F_BE,l);
  409. { reset carry flag }
  410. exprasmlist.concat(taicpu.op_none(A_CLC,S_NO));
  411. cg.a_jmp_always(exprasmlist,l2);
  412. cg.a_label(exprasmlist,l);
  413. location_release(exprasmlist,left.location);
  414. hr:=cg.getintregister(exprasmlist,OS_32);
  415. cg.a_load_ref_reg(exprasmlist,OS_32,OS_32,left.location.reference,hr);
  416. { We have to load the value into a register because
  417. btl does not accept values only refs or regs (PFV) }
  418. hr2:=cg.getintregister(exprasmlist,OS_32);
  419. cg.a_load_const_reg(exprasmlist,OS_32,right.location.value,hr2);
  420. emit_reg_reg(A_BT,S_L,hr,hr2);
  421. cg.ungetregister(exprasmlist,hr2);
  422. end;
  423. end;
  424. cg.a_label(exprasmlist,l2);
  425. end { of right.location.loc=LOC_CONSTANT }
  426. { do search in a normal set which could have >32 elementsm
  427. but also used if the left side contains higher values > 32 }
  428. else if left.nodetype=ordconstn then
  429. begin
  430. location.resflags:=F_NE;
  431. inc(right.location.reference.offset,tordconstnode(left).value shr 3);
  432. emit_const_ref(A_TEST,S_B,1 shl (tordconstnode(left).value and 7),right.location.reference);
  433. location_release(exprasmlist,right.location);
  434. end
  435. else
  436. begin
  437. if (left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  438. pleftreg:=cg.makeregsize(exprasmlist,left.location.register,OS_32)
  439. else
  440. pleftreg:=cg.getintregister(exprasmlist,OS_32);
  441. cg.a_load_loc_reg(exprasmlist,OS_32,left.location,pleftreg);
  442. location_freetemp(exprasmlist,left.location);
  443. location_release(exprasmlist,left.location);
  444. emit_reg_ref(A_BT,S_L,pleftreg,right.location.reference);
  445. cg.ungetregister(exprasmlist,pleftreg);
  446. location_release(exprasmlist,right.location);
  447. { tg.ungetiftemp(exprasmlist,right.location.reference) happens below }
  448. location.resflags:=F_C;
  449. end;
  450. end;
  451. end;
  452. if not genjumps then
  453. location_freetemp(exprasmlist,right.location);
  454. end;
  455. begin
  456. cinnode:=tx86innode;
  457. end.
  458. {
  459. $Log$
  460. Revision 1.4 2004-06-16 20:07:11 florian
  461. * dwarf branch merged
  462. Revision 1.3 2004/05/22 23:34:28 peter
  463. tai_regalloc.allocation changed to ratype to notify rgobj of register size changes
  464. Revision 1.2.2.1 2004/04/28 18:35:42 peter
  465. * cardinal fixes for x86-64
  466. Revision 1.2 2004/02/27 10:21:06 florian
  467. * top_symbol killed
  468. + refaddr to treference added
  469. + refsymbol to treference added
  470. * top_local stuff moved to an extra record to save memory
  471. + aint introduced
  472. * tppufile.get/putint64/aint implemented
  473. Revision 1.1 2004/02/22 12:04:04 florian
  474. + nx86set added
  475. * some more x86-64 fixes
  476. }