nx86set.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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,
  37. cga,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. hreg,hreg2,
  69. pleftreg : tregister;
  70. opsize : tcgsize;
  71. setparts : array[1..8] of Tsetpart;
  72. i,numparts : byte;
  73. adjustment : longint;
  74. l,l2 : tasmlabel;
  75. {$ifdef CORRECT_SET_IN_FPC}
  76. AM : tasmop;
  77. {$endif CORRECT_SET_IN_FPC}
  78. function analizeset(Aset:pconstset;is_small:boolean):boolean;
  79. var
  80. compares,maxcompares:word;
  81. i:byte;
  82. begin
  83. if tnormalset(Aset^)=[] then
  84. {The expression...
  85. if expr in []
  86. ...is allways false. It should be optimized away in the
  87. resulttype pass, and thus never occur here. Since we
  88. do generate wrong code for it, do internalerror.}
  89. internalerror(2002072301);
  90. analizeset:=false;
  91. ranges:=false;
  92. numparts:=0;
  93. compares:=0;
  94. { Lots of comparisions take a lot of time, so do not allow
  95. too much comparisions. 8 comparisions are, however, still
  96. smalller than emitting the set }
  97. if cs_littlesize in aktglobalswitches then
  98. maxcompares:=8
  99. else
  100. maxcompares:=5;
  101. { when smallset is possible allow only 3 compares the smallset
  102. code is for littlesize also smaller when more compares are used }
  103. if is_small then
  104. maxcompares:=3;
  105. for i:=0 to 255 do
  106. if i in tnormalset(Aset^) then
  107. begin
  108. if (numparts=0) or (i<>setparts[numparts].stop+1) then
  109. begin
  110. {Set element is a separate element.}
  111. inc(compares);
  112. if compares>maxcompares then
  113. exit;
  114. inc(numparts);
  115. setparts[numparts].range:=false;
  116. setparts[numparts].stop:=i;
  117. end
  118. else
  119. {Set element is part of a range.}
  120. if not setparts[numparts].range then
  121. begin
  122. {Transform an element into a range.}
  123. setparts[numparts].range:=true;
  124. setparts[numparts].start:=setparts[numparts].stop;
  125. setparts[numparts].stop:=i;
  126. ranges := true;
  127. { there's only one compare per range anymore. Only a }
  128. { sub is added, but that's much faster than a }
  129. { cmp/jcc combo so neglect its effect }
  130. { inc(compares);
  131. if compares>maxcompares then
  132. exit; }
  133. end
  134. else
  135. begin
  136. {Extend a range.}
  137. setparts[numparts].stop:=i;
  138. end;
  139. end;
  140. analizeset:=true;
  141. end;
  142. begin
  143. { We check first if we can generate jumps, this can be done
  144. because the resulttype.def is already set in firstpass }
  145. { check if we can use smallset operation using btl which is limited
  146. to 32 bits, the left side may also not contain higher values !! }
  147. use_small:=(tsetdef(right.resulttype.def).settype=smallset) and
  148. ((left.resulttype.def.deftype=orddef) and (torddef(left.resulttype.def).high<=32) or
  149. (left.resulttype.def.deftype=enumdef) and (tenumdef(left.resulttype.def).max<=32));
  150. { Can we generate jumps? Possible for all types of sets }
  151. genjumps:=(right.nodetype=setconstn) and
  152. analizeset(tsetconstnode(right).value_set,use_small);
  153. { calculate both operators }
  154. { the complex one first }
  155. firstcomplex(self);
  156. secondpass(left);
  157. { Only process the right if we are not generating jumps }
  158. if not genjumps then
  159. begin
  160. secondpass(right);
  161. end;
  162. if codegenerror then
  163. exit;
  164. { ofcourse not commutative }
  165. if nf_swaped in flags then
  166. swapleftright;
  167. if genjumps then
  168. begin
  169. { It gives us advantage to check for the set elements
  170. separately instead of using the SET_IN_BYTE procedure.
  171. To do: Build in support for LOC_JUMP }
  172. opsize := def_cgsize(left.resulttype.def);
  173. { If register is used, use only lower 8 bits }
  174. if left.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  175. begin
  176. { for ranges we always need a 32bit register, because then we }
  177. { use the register as base in a reference (JM) }
  178. if ranges then
  179. begin
  180. pleftreg:=cg.makeregsize(exprasmlist,left.location.register,OS_INT);
  181. cg.a_load_reg_reg(exprasmlist,left.location.size,OS_INT,left.location.register,pleftreg);
  182. if opsize<>OS_INT then
  183. cg.a_op_const_reg(exprasmlist,OP_AND,OS_INT,255,pleftreg);
  184. opsize:=OS_INT;
  185. end
  186. else
  187. { otherwise simply use the lower 8 bits (no "and" }
  188. { necessary this way) (JM) }
  189. begin
  190. pleftreg:=cg.makeregsize(exprasmlist,left.location.register,OS_8);
  191. opsize := OS_8;
  192. end;
  193. end
  194. else
  195. begin
  196. { load the value in a register }
  197. pleftreg:=cg.getintregister(exprasmlist,OS_32);
  198. opsize:=OS_32;
  199. cg.a_load_ref_reg(exprasmlist,OS_8,OS_32,left.location.reference,pleftreg);
  200. end;
  201. { Get a label to jump to the end }
  202. location_reset(location,LOC_FLAGS,OS_NO);
  203. { It's better to use the zero flag when there are
  204. no ranges }
  205. if ranges then
  206. location.resflags:=F_C
  207. else
  208. location.resflags:=F_E;
  209. objectlibrary.getlabel(l);
  210. { how much have we already substracted from the x in the }
  211. { "x in [y..z]" expression }
  212. adjustment := 0;
  213. for i:=1 to numparts do
  214. if setparts[i].range then
  215. { use fact that a <= x <= b <=> cardinal(x-a) <= cardinal(b-a) }
  216. begin
  217. { is the range different from all legal values? }
  218. if (setparts[i].stop-setparts[i].start <> 255) then
  219. begin
  220. { yes, is the lower bound <> 0? }
  221. if (setparts[i].start <> 0) then
  222. begin
  223. if (left.location.loc = LOC_CREGISTER) then
  224. begin
  225. hreg:=cg.getintregister(exprasmlist,OS_INT);
  226. cg.a_load_reg_reg(exprasmlist,opsize,OS_INT,pleftreg,hreg);
  227. pleftreg:=hreg;
  228. opsize:=OS_INT;
  229. end;
  230. cg.a_op_const_reg(exprasmlist,OP_SUB,opsize,setparts[i].start-adjustment,pleftreg);
  231. end;
  232. { new total value substracted from x: }
  233. { adjustment + (setparts[i].start - adjustment) }
  234. adjustment := setparts[i].start;
  235. { check if result < b-a+1 (not "result <= b-a", since }
  236. { we need a carry in case the element is in the range }
  237. { (this will never overflow since we check at the }
  238. { beginning whether stop-start <> 255) }
  239. cg.a_cmp_const_reg_label(exprasmlist,opsize,OC_B,setparts[i].stop-setparts[i].start+1,pleftreg,l);
  240. end
  241. else
  242. { if setparts[i].start = 0 and setparts[i].stop = 255, }
  243. { it's always true since "in" is only allowed for bytes }
  244. begin
  245. exprasmlist.concat(taicpu.op_none(A_STC,S_NO));
  246. cg.a_jmp_always(exprasmlist,l);
  247. end;
  248. end
  249. else
  250. begin
  251. { Emit code to check if left is an element }
  252. exprasmlist.concat(taicpu.op_const_reg(A_CMP,TCGSize2OpSize[opsize],setparts[i].stop-adjustment,
  253. pleftreg));
  254. { Result should be in carry flag when ranges are used }
  255. if ranges then
  256. exprasmlist.concat(taicpu.op_none(A_STC,S_NO));
  257. { If found, jump to end }
  258. cg.a_jmp_flags(exprasmlist,F_E,l);
  259. end;
  260. if ranges and
  261. { if the last one was a range, the carry flag is already }
  262. { set appropriately }
  263. not(setparts[numparts].range) then
  264. exprasmlist.concat(taicpu.op_none(A_CLC,S_NO));
  265. { To compensate for not doing a second pass }
  266. right.location.reference.symbol:=nil;
  267. { Now place the end label }
  268. cg.a_label(exprasmlist,l);
  269. end
  270. else
  271. begin
  272. location_reset(location,LOC_FLAGS,OS_NO);
  273. { We will now generated code to check the set itself, no jmps,
  274. handle smallsets separate, because it allows faster checks }
  275. if use_small then
  276. begin
  277. if left.nodetype=ordconstn then
  278. begin
  279. location.resflags:=F_NE;
  280. case right.location.loc of
  281. LOC_REGISTER,
  282. LOC_CREGISTER:
  283. begin
  284. emit_const_reg(A_TEST,S_L,
  285. 1 shl (tordconstnode(left).value and 31),right.location.register);
  286. end;
  287. LOC_REFERENCE,
  288. LOC_CREFERENCE :
  289. begin
  290. emit_const_ref(A_TEST,S_L,1 shl (tordconstnode(left).value and 31),
  291. right.location.reference);
  292. end;
  293. else
  294. internalerror(200203312);
  295. end;
  296. end
  297. else
  298. begin
  299. case left.location.loc of
  300. LOC_REGISTER,
  301. LOC_CREGISTER:
  302. begin
  303. hreg:=cg.makeregsize(exprasmlist,left.location.register,OS_32);
  304. cg.a_load_reg_reg(exprasmlist,left.location.size,OS_32,left.location.register,hreg);
  305. end;
  306. else
  307. begin
  308. { the set element isn't never samller than a byte
  309. and because it's a small set we need only 5 bits
  310. but 8 bits are easier to load }
  311. hreg:=cg.getintregister(exprasmlist,OS_32);
  312. cg.a_load_ref_reg(exprasmlist,OS_8,OS_32,left.location.reference,hreg);
  313. end;
  314. end;
  315. case right.location.loc of
  316. LOC_REGISTER,
  317. LOC_CREGISTER :
  318. begin
  319. emit_reg_reg(A_BT,S_L,hreg,right.location.register);
  320. end;
  321. LOC_CONSTANT :
  322. begin
  323. { We have to load the value into a register because
  324. btl does not accept values only refs or regs (PFV) }
  325. hreg2:=cg.getintregister(exprasmlist,OS_32);
  326. cg.a_load_const_reg(exprasmlist,OS_32,right.location.value,hreg2);
  327. emit_reg_reg(A_BT,S_L,hreg,hreg2);
  328. end;
  329. LOC_CREFERENCE,
  330. LOC_REFERENCE :
  331. begin
  332. emit_reg_ref(A_BT,S_L,hreg,right.location.reference);
  333. end;
  334. else
  335. internalerror(2002032210);
  336. end;
  337. location.resflags:=F_C;
  338. end;
  339. end
  340. else
  341. begin
  342. if right.location.loc=LOC_CONSTANT then
  343. begin
  344. location.resflags:=F_C;
  345. objectlibrary.getlabel(l);
  346. objectlibrary.getlabel(l2);
  347. { load constants to a register }
  348. if left.nodetype=ordconstn then
  349. location_force_reg(exprasmlist,left.location,OS_INT,true);
  350. case left.location.loc of
  351. LOC_REGISTER,
  352. LOC_CREGISTER:
  353. begin
  354. hreg:=cg.makeregsize(exprasmlist,left.location.register,OS_32);
  355. cg.a_load_reg_reg(exprasmlist,left.location.size,OS_32,left.location.register,hreg);
  356. cg.a_cmp_const_reg_label(exprasmlist,OS_32,OC_BE,31,hreg,l);
  357. { reset carry flag }
  358. exprasmlist.concat(taicpu.op_none(A_CLC,S_NO));
  359. cg.a_jmp_always(exprasmlist,l2);
  360. cg.a_label(exprasmlist,l);
  361. { We have to load the value into a register because
  362. btl does not accept values only refs or regs (PFV) }
  363. hreg2:=cg.getintregister(exprasmlist,OS_32);
  364. cg.a_load_const_reg(exprasmlist,OS_32,right.location.value,hreg2);
  365. emit_reg_reg(A_BT,S_L,hreg,hreg2);
  366. end;
  367. else
  368. begin
  369. {$ifdef CORRECT_SET_IN_FPC}
  370. if m_tp in aktmodeswitches then
  371. begin
  372. {***WARNING only correct if
  373. reference is 32 bits (PM) *****}
  374. emit_const_ref(A_CMP,S_L,31,reference_copy(left.location.reference));
  375. end
  376. else
  377. {$endif CORRECT_SET_IN_FPC}
  378. begin
  379. emit_const_ref(A_CMP,S_B,31,left.location.reference);
  380. end;
  381. cg.a_jmp_flags(exprasmlist,F_BE,l);
  382. { reset carry flag }
  383. exprasmlist.concat(taicpu.op_none(A_CLC,S_NO));
  384. cg.a_jmp_always(exprasmlist,l2);
  385. cg.a_label(exprasmlist,l);
  386. hreg:=cg.getintregister(exprasmlist,OS_32);
  387. cg.a_load_ref_reg(exprasmlist,OS_32,OS_32,left.location.reference,hreg);
  388. { We have to load the value into a register because
  389. btl does not accept values only refs or regs (PFV) }
  390. hreg2:=cg.getintregister(exprasmlist,OS_32);
  391. cg.a_load_const_reg(exprasmlist,OS_32,right.location.value,hreg2);
  392. emit_reg_reg(A_BT,S_L,hreg,hreg2);
  393. end;
  394. end;
  395. cg.a_label(exprasmlist,l2);
  396. end { of right.location.loc=LOC_CONSTANT }
  397. { do search in a normal set which could have >32 elementsm
  398. but also used if the left side contains higher values > 32 }
  399. else if left.nodetype=ordconstn then
  400. begin
  401. location.resflags:=F_NE;
  402. inc(right.location.reference.offset,tordconstnode(left).value shr 3);
  403. emit_const_ref(A_TEST,S_B,1 shl (tordconstnode(left).value and 7),right.location.reference);
  404. end
  405. else
  406. begin
  407. if (left.location.loc=LOC_REGISTER) then
  408. pleftreg:=cg.makeregsize(exprasmlist,left.location.register,OS_32)
  409. else
  410. pleftreg:=cg.getintregister(exprasmlist,OS_32);
  411. cg.a_load_loc_reg(exprasmlist,OS_32,left.location,pleftreg);
  412. location_freetemp(exprasmlist,left.location);
  413. emit_reg_ref(A_BT,S_L,pleftreg,right.location.reference);
  414. { tg.ungetiftemp(exprasmlist,right.location.reference) happens below }
  415. location.resflags:=F_C;
  416. end;
  417. end;
  418. end;
  419. if not genjumps then
  420. location_freetemp(exprasmlist,right.location);
  421. end;
  422. begin
  423. cinnode:=tx86innode;
  424. end.
  425. {
  426. $Log$
  427. Revision 1.7 2004-10-24 20:10:08 peter
  428. * -Or fixes
  429. Revision 1.6 2004/10/01 17:32:16 peter
  430. * fix resizing of LOC_CREGISTER
  431. Revision 1.5 2004/09/25 14:23:55 peter
  432. * ungetregister is now only used for cpuregisters, renamed to
  433. ungetcpuregister
  434. * renamed (get|unget)explicitregister(s) to ..cpuregister
  435. * removed location-release/reference_release
  436. Revision 1.4 2004/06/16 20:07:11 florian
  437. * dwarf branch merged
  438. Revision 1.3 2004/05/22 23:34:28 peter
  439. tai_regalloc.allocation changed to ratype to notify rgobj of register size changes
  440. Revision 1.2.2.1 2004/04/28 18:35:42 peter
  441. * cardinal fixes for x86-64
  442. Revision 1.2 2004/02/27 10:21:06 florian
  443. * top_symbol killed
  444. + refaddr to treference added
  445. + refsymbol to treference added
  446. * top_local stuff moved to an extra record to save memory
  447. + aint introduced
  448. * tppufile.get/putint64/aint implemented
  449. Revision 1.1 2004/02/22 12:04:04 florian
  450. + nx86set added
  451. * some more x86-64 fixes
  452. }