nx86set.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. end;
  203. { Get a label to jump to the end }
  204. location_reset(location,LOC_FLAGS,OS_NO);
  205. { It's better to use the zero flag when there are
  206. no ranges }
  207. if ranges then
  208. location.resflags:=F_C
  209. else
  210. location.resflags:=F_E;
  211. objectlibrary.getlabel(l);
  212. { how much have we already substracted from the x in the }
  213. { "x in [y..z]" expression }
  214. adjustment := 0;
  215. r:=NR_NO;
  216. for i:=1 to numparts do
  217. if setparts[i].range then
  218. { use fact that a <= x <= b <=> cardinal(x-a) <= cardinal(b-a) }
  219. begin
  220. { is the range different from all legal values? }
  221. if (setparts[i].stop-setparts[i].start <> 255) then
  222. begin
  223. { yes, is the lower bound <> 0? }
  224. if (setparts[i].start <> 0) then
  225. { we're going to substract from the left register, }
  226. { so in case of a LOC_CREGISTER first move the value }
  227. { to edi (not done before because now we can do the }
  228. { move and substract in one instruction with LEA) }
  229. if (left.location.loc = LOC_CREGISTER) then
  230. begin
  231. r:=cg.getintregister(exprasmlist,OS_32);
  232. reference_reset_base(href,pleftreg,-setparts[i].start);
  233. cg.a_loadaddr_ref_reg(exprasmlist,href,r);
  234. { only now change pleftreg since previous value is }
  235. { still used in previous instruction }
  236. pleftreg := r;
  237. opsize := OS_32;
  238. end
  239. else
  240. begin
  241. { otherwise, the value is already in a register }
  242. { that can be modified }
  243. cg.a_op_const_reg(exprasmlist,OP_SUB,opsize,setparts[i].start-adjustment,pleftreg);
  244. end;
  245. { new total value substracted from x: }
  246. { adjustment + (setparts[i].start - adjustment) }
  247. adjustment := setparts[i].start;
  248. { check if result < b-a+1 (not "result <= b-a", since }
  249. { we need a carry in case the element is in the range }
  250. { (this will never overflow since we check at the }
  251. { beginning whether stop-start <> 255) }
  252. cg.a_cmp_const_reg_label(exprasmlist,opsize,OC_B,setparts[i].stop-setparts[i].start+1,pleftreg,l);
  253. end
  254. else
  255. { if setparts[i].start = 0 and setparts[i].stop = 255, }
  256. { it's always true since "in" is only allowed for bytes }
  257. begin
  258. exprasmlist.concat(taicpu.op_none(A_STC,S_NO));
  259. cg.a_jmp_always(exprasmlist,l);
  260. end;
  261. end
  262. else
  263. begin
  264. { Emit code to check if left is an element }
  265. exprasmlist.concat(taicpu.op_const_reg(A_CMP,TCGSize2OpSize[opsize],setparts[i].stop-adjustment,
  266. pleftreg));
  267. { Result should be in carry flag when ranges are used }
  268. if ranges then
  269. exprasmlist.concat(taicpu.op_none(A_STC,S_NO));
  270. { If found, jump to end }
  271. cg.a_jmp_flags(exprasmlist,F_E,l);
  272. end;
  273. if ranges and
  274. { if the last one was a range, the carry flag is already }
  275. { set appropriately }
  276. not(setparts[numparts].range) then
  277. exprasmlist.concat(taicpu.op_none(A_CLC,S_NO));
  278. { To compensate for not doing a second pass }
  279. right.location.reference.symbol:=nil;
  280. { Now place the end label }
  281. cg.a_label(exprasmlist,l);
  282. end
  283. else
  284. begin
  285. location_reset(location,LOC_FLAGS,OS_NO);
  286. { We will now generated code to check the set itself, no jmps,
  287. handle smallsets separate, because it allows faster checks }
  288. if use_small then
  289. begin
  290. if left.nodetype=ordconstn then
  291. begin
  292. location.resflags:=F_NE;
  293. case right.location.loc of
  294. LOC_REGISTER,
  295. LOC_CREGISTER:
  296. begin
  297. emit_const_reg(A_TEST,S_L,
  298. 1 shl (tordconstnode(left).value and 31),right.location.register);
  299. end;
  300. LOC_REFERENCE,
  301. LOC_CREFERENCE :
  302. begin
  303. emit_const_ref(A_TEST,S_L,1 shl (tordconstnode(left).value and 31),
  304. right.location.reference);
  305. end;
  306. else
  307. internalerror(200203312);
  308. end;
  309. end
  310. else
  311. begin
  312. case left.location.loc of
  313. LOC_REGISTER,
  314. LOC_CREGISTER:
  315. begin
  316. hr:=cg.makeregsize(exprasmlist,left.location.register,OS_32);
  317. cg.a_load_reg_reg(exprasmlist,left.location.size,OS_32,left.location.register,hr);
  318. end;
  319. else
  320. begin
  321. { the set element isn't never samller than a byte
  322. and because it's a small set we need only 5 bits
  323. but 8 bits are easier to load }
  324. hr:=cg.getintregister(exprasmlist,OS_32);
  325. cg.a_load_ref_reg(exprasmlist,OS_8,OS_32,left.location.reference,hr);
  326. end;
  327. end;
  328. case right.location.loc of
  329. LOC_REGISTER,
  330. LOC_CREGISTER :
  331. begin
  332. emit_reg_reg(A_BT,S_L,hr,right.location.register);
  333. end;
  334. LOC_CONSTANT :
  335. begin
  336. { We have to load the value into a register because
  337. btl does not accept values only refs or regs (PFV) }
  338. hr2:=cg.getintregister(exprasmlist,OS_32);
  339. cg.a_load_const_reg(exprasmlist,OS_32,right.location.value,hr2);
  340. emit_reg_reg(A_BT,S_L,hr,hr2);
  341. end;
  342. LOC_CREFERENCE,
  343. LOC_REFERENCE :
  344. begin
  345. emit_reg_ref(A_BT,S_L,hr,right.location.reference);
  346. end;
  347. else
  348. internalerror(2002032210);
  349. end;
  350. location.resflags:=F_C;
  351. end;
  352. end
  353. else
  354. begin
  355. if right.location.loc=LOC_CONSTANT then
  356. begin
  357. location.resflags:=F_C;
  358. objectlibrary.getlabel(l);
  359. objectlibrary.getlabel(l2);
  360. { load constants to a register }
  361. if left.nodetype=ordconstn then
  362. location_force_reg(exprasmlist,left.location,OS_INT,true);
  363. case left.location.loc of
  364. LOC_REGISTER,
  365. LOC_CREGISTER:
  366. begin
  367. hr:=cg.makeregsize(exprasmlist,left.location.register,OS_32);
  368. cg.a_load_reg_reg(exprasmlist,left.location.size,OS_32,left.location.register,hr);
  369. cg.a_cmp_const_reg_label(exprasmlist,OS_32,OC_BE,31,hr,l);
  370. { reset carry flag }
  371. exprasmlist.concat(taicpu.op_none(A_CLC,S_NO));
  372. cg.a_jmp_always(exprasmlist,l2);
  373. cg.a_label(exprasmlist,l);
  374. { We have to load the value into a register because
  375. btl does not accept values only refs or regs (PFV) }
  376. hr2:=cg.getintregister(exprasmlist,OS_32);
  377. cg.a_load_const_reg(exprasmlist,OS_32,right.location.value,hr2);
  378. emit_reg_reg(A_BT,S_L,hr,hr2);
  379. end;
  380. else
  381. begin
  382. {$ifdef CORRECT_SET_IN_FPC}
  383. if m_tp in aktmodeswitches then
  384. begin
  385. {***WARNING only correct if
  386. reference is 32 bits (PM) *****}
  387. emit_const_ref(A_CMP,S_L,31,reference_copy(left.location.reference));
  388. end
  389. else
  390. {$endif CORRECT_SET_IN_FPC}
  391. begin
  392. emit_const_ref(A_CMP,S_B,31,left.location.reference);
  393. end;
  394. cg.a_jmp_flags(exprasmlist,F_BE,l);
  395. { reset carry flag }
  396. exprasmlist.concat(taicpu.op_none(A_CLC,S_NO));
  397. cg.a_jmp_always(exprasmlist,l2);
  398. cg.a_label(exprasmlist,l);
  399. hr:=cg.getintregister(exprasmlist,OS_32);
  400. cg.a_load_ref_reg(exprasmlist,OS_32,OS_32,left.location.reference,hr);
  401. { We have to load the value into a register because
  402. btl does not accept values only refs or regs (PFV) }
  403. hr2:=cg.getintregister(exprasmlist,OS_32);
  404. cg.a_load_const_reg(exprasmlist,OS_32,right.location.value,hr2);
  405. emit_reg_reg(A_BT,S_L,hr,hr2);
  406. end;
  407. end;
  408. cg.a_label(exprasmlist,l2);
  409. end { of right.location.loc=LOC_CONSTANT }
  410. { do search in a normal set which could have >32 elementsm
  411. but also used if the left side contains higher values > 32 }
  412. else if left.nodetype=ordconstn then
  413. begin
  414. location.resflags:=F_NE;
  415. inc(right.location.reference.offset,tordconstnode(left).value shr 3);
  416. emit_const_ref(A_TEST,S_B,1 shl (tordconstnode(left).value and 7),right.location.reference);
  417. end
  418. else
  419. begin
  420. if (left.location.loc=LOC_REGISTER) then
  421. pleftreg:=cg.makeregsize(exprasmlist,left.location.register,OS_32)
  422. else
  423. pleftreg:=cg.getintregister(exprasmlist,OS_32);
  424. cg.a_load_loc_reg(exprasmlist,OS_32,left.location,pleftreg);
  425. location_freetemp(exprasmlist,left.location);
  426. emit_reg_ref(A_BT,S_L,pleftreg,right.location.reference);
  427. { tg.ungetiftemp(exprasmlist,right.location.reference) happens below }
  428. location.resflags:=F_C;
  429. end;
  430. end;
  431. end;
  432. if not genjumps then
  433. location_freetemp(exprasmlist,right.location);
  434. end;
  435. begin
  436. cinnode:=tx86innode;
  437. end.
  438. {
  439. $Log$
  440. Revision 1.6 2004-10-01 17:32:16 peter
  441. * fix resizing of LOC_CREGISTER
  442. Revision 1.5 2004/09/25 14:23:55 peter
  443. * ungetregister is now only used for cpuregisters, renamed to
  444. ungetcpuregister
  445. * renamed (get|unget)explicitregister(s) to ..cpuregister
  446. * removed location-release/reference_release
  447. Revision 1.4 2004/06/16 20:07:11 florian
  448. * dwarf branch merged
  449. Revision 1.3 2004/05/22 23:34:28 peter
  450. tai_regalloc.allocation changed to ratype to notify rgobj of register size changes
  451. Revision 1.2.2.1 2004/04/28 18:35:42 peter
  452. * cardinal fixes for x86-64
  453. Revision 1.2 2004/02/27 10:21:06 florian
  454. * top_symbol killed
  455. + refaddr to treference added
  456. + refsymbol to treference added
  457. * top_local stuff moved to an extra record to save memory
  458. + aint introduced
  459. * tppufile.get/putint64/aint implemented
  460. Revision 1.1 2004/02/22 12:04:04 florian
  461. + nx86set added
  462. * some more x86-64 fixes
  463. }