ncgset.pas 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl and Carl Eric Codere
  4. Generate generic assembler for in set/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 ncgset;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. node,nset,cpubase,cginfo,cgbase,cgobj,aasmbase,aasmtai;
  23. type
  24. tcgsetelementnode = class(tsetelementnode)
  25. procedure pass_2;override;
  26. end;
  27. tcginnode = class(tinnode)
  28. procedure pass_2;override;
  29. {# Routine to test bitnumber in bitnumber register on value
  30. in value register. The __result register should be set
  31. to one if the bit is set, otherwise __result register
  32. should be set to zero.
  33. Should be overriden on processors which have specific
  34. instructions to do bit tests.
  35. }
  36. procedure emit_bit_test_reg_reg(list : taasmoutput; bitnumber : tregister;
  37. value : tregister; __result :tregister);virtual;
  38. end;
  39. implementation
  40. uses
  41. globtype,systems,
  42. verbose,globals,
  43. symconst,symdef,defbase,
  44. paramgr,
  45. pass_2,
  46. ncon,
  47. cga,tgobj,ncgutil,regvars,rgobj;
  48. {*****************************************************************************
  49. TCGSETELEMENTNODE
  50. *****************************************************************************}
  51. procedure tcgsetelementnode.pass_2;
  52. var
  53. pushedregs : tmaybesave;
  54. begin
  55. { load first value in 32bit register }
  56. secondpass(left);
  57. if left.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  58. location_force_reg(exprasmlist,left.location,OS_32,false);
  59. { also a second value ? }
  60. if assigned(right) then
  61. begin
  62. maybe_save(exprasmlist,right.registers32,left.location,pushedregs);
  63. secondpass(right);
  64. if codegenerror then
  65. exit;
  66. maybe_restore(exprasmlist,left.location,pushedregs);
  67. if right.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  68. location_force_reg(exprasmlist,right.location,OS_32,false);
  69. end;
  70. { we doesn't modify the left side, we check only the type }
  71. location_copy(location,left.location);
  72. end;
  73. {*****************************************************************************
  74. *****************************************************************************}
  75. {**********************************************************************}
  76. { Description: Emit operation to do a bit test, where the bitnumber }
  77. { to test is in the bitnumber register. The value to test against is }
  78. { located in the value register. }
  79. { WARNING: Bitnumber register value is DESTROYED! }
  80. { __Result register is set to 1, if the bit is set otherwise, __Result}
  81. { is set to zero. __RESULT register is also used as scratch. }
  82. {**********************************************************************}
  83. procedure tcginnode.emit_bit_test_reg_reg(list : taasmoutput; bitnumber : tregister; value : tregister; __result :tregister);
  84. begin
  85. { first make sure that the bit number is modulo 32 }
  86. { not necessary, since if it's > 31, we have a range error -> will }
  87. { be caught when range checking is on! (JM) }
  88. { cg.a_op_const_reg(list,OP_AND,31,bitnumber); }
  89. { rotate value register "bitnumber" bits to the right }
  90. cg.a_op_reg_reg_reg(list,OP_SHR,OS_INT,bitnumber,value,__result);
  91. { extract the bit we want }
  92. cg.a_op_const_reg(list,OP_AND,1,__result);
  93. end;
  94. procedure tcginnode.pass_2;
  95. type
  96. Tsetpart=record
  97. range : boolean; {Part is a range.}
  98. start,stop : byte; {Start/stop when range; Stop=element when an element.}
  99. end;
  100. var
  101. genjumps,
  102. use_small,
  103. ranges : boolean;
  104. hr,hr2,hr3,
  105. pleftreg : tregister;
  106. href : treference;
  107. opsize : tcgsize;
  108. setparts : array[1..8] of Tsetpart;
  109. i,numparts : byte;
  110. adjustment : longint;
  111. pushedregs : tmaybesave;
  112. l,l2,l3 : tasmlabel;
  113. function analizeset(Aset:pconstset;is_small:boolean):boolean;
  114. type
  115. byteset=set of byte;
  116. var
  117. compares,maxcompares:word;
  118. i:byte;
  119. begin
  120. analizeset:=false;
  121. ranges:=false;
  122. numparts:=0;
  123. compares:=0;
  124. { Lots of comparisions take a lot of time, so do not allow
  125. too much comparisions. 8 comparisions are, however, still
  126. smalller than emitting the set }
  127. if cs_littlesize in aktglobalswitches then
  128. maxcompares:=8
  129. else
  130. maxcompares:=5;
  131. { when smallset is possible allow only 3 compares the smallset
  132. code is for littlesize also smaller when more compares are used }
  133. if is_small then
  134. maxcompares:=3;
  135. for i:=0 to 255 do
  136. if i in byteset(Aset^) then
  137. begin
  138. if (numparts=0) or (i<>setparts[numparts].stop+1) then
  139. begin
  140. {Set element is a separate element.}
  141. inc(compares);
  142. if compares>maxcompares then
  143. exit;
  144. inc(numparts);
  145. setparts[numparts].range:=false;
  146. setparts[numparts].stop:=i;
  147. end
  148. else
  149. {Set element is part of a range.}
  150. if not setparts[numparts].range then
  151. begin
  152. {Transform an element into a range.}
  153. setparts[numparts].range:=true;
  154. setparts[numparts].start:=setparts[numparts].stop;
  155. setparts[numparts].stop:=i;
  156. ranges := true;
  157. { there's only one compare per range anymore. Only a }
  158. { sub is added, but that's much faster than a }
  159. { cmp/jcc combo so neglect its effect }
  160. { inc(compares);
  161. if compares>maxcompares then
  162. exit; }
  163. end
  164. else
  165. begin
  166. {Extend a range.}
  167. setparts[numparts].stop:=i;
  168. end;
  169. end;
  170. analizeset:=true;
  171. end;
  172. begin
  173. { We check first if we can generate jumps, this can be done
  174. because the resulttype.def is already set in firstpass }
  175. { check if we can use smallset operation using btl which is limited
  176. to 32 bits, the left side may also not contain higher values !! }
  177. use_small:=(tsetdef(right.resulttype.def).settype=smallset) and
  178. ((left.resulttype.def.deftype=orddef) and (torddef(left.resulttype.def).high<=32) or
  179. (left.resulttype.def.deftype=enumdef) and (tenumdef(left.resulttype.def).max<=32));
  180. { Can we generate jumps? Possible for all types of sets }
  181. genjumps:=(right.nodetype=setconstn) and
  182. analizeset(tsetconstnode(right).value_set,use_small);
  183. { calculate both operators }
  184. { the complex one first }
  185. firstcomplex(self);
  186. secondpass(left);
  187. { Only process the right if we are not generating jumps }
  188. if not genjumps then
  189. begin
  190. maybe_save(exprasmlist,right.registers32,left.location,pushedregs);
  191. secondpass(right);
  192. maybe_restore(exprasmlist,left.location,pushedregs);
  193. end;
  194. if codegenerror then
  195. exit;
  196. { ofcourse not commutative }
  197. if nf_swaped in flags then
  198. swapleftright;
  199. { location is always LOC_JUMP }
  200. location_reset(location,LOC_REGISTER,OS_INT);
  201. { allocate a register for the result }
  202. location.register := rg.getregisterint(exprasmlist);
  203. { Get a label to jump to the end }
  204. getlabel(l);
  205. if genjumps then
  206. begin
  207. { clear the register value, indicating result is FALSE }
  208. cg.a_load_const_reg(exprasmlist,OS_INT,0,location.register);
  209. opsize := def_cgsize(left.resulttype.def);
  210. { If register is used, use only lower 8 bits }
  211. if left.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  212. begin
  213. { for ranges we always need a 32bit register, because then we }
  214. { use the register as base in a reference (JM) }
  215. if ranges then
  216. begin
  217. pleftreg:=rg.makeregsize(left.location.register,OS_INT);
  218. cg.a_load_reg_reg(exprasmlist,left.location.size,left.location.register,pleftreg);
  219. if opsize <> OS_INT then
  220. cg.a_op_const_reg(exprasmlist,OP_AND,255,pleftreg);
  221. opsize := OS_INT;
  222. end
  223. else
  224. { otherwise simply use the lower 8 bits (no "and" }
  225. { necessary this way) (JM) }
  226. begin
  227. pleftreg:=rg.makeregsize(left.location.register,OS_8);
  228. opsize := OS_8;
  229. end;
  230. end
  231. else
  232. begin
  233. { load the value in a register }
  234. pleftreg := cg.get_scratch_reg_int(exprasmlist);
  235. opsize := OS_INT;
  236. cg.a_load_ref_reg(exprasmlist,def_cgsize(left.resulttype.def),left.location.reference,pleftreg);
  237. end;
  238. { how much have we already substracted from the x in the }
  239. { "x in [y..z]" expression }
  240. adjustment := 0;
  241. hr := R_NO;
  242. for i:=1 to numparts do
  243. if setparts[i].range then
  244. { use fact that a <= x <= b <=> cardinal(x-a) <= cardinal(b-a) }
  245. begin
  246. { is the range different from all legal values? }
  247. if (setparts[i].stop-setparts[i].start <> 255) then
  248. begin
  249. { yes, is the lower bound <> 0? }
  250. if (setparts[i].start <> 0) then
  251. { we're going to substract from the left register, }
  252. { so in case of a LOC_CREGISTER first move the value }
  253. { to edi (not done before because now we can do the }
  254. { move and substract in one instruction with LEA) }
  255. if (left.location.loc = LOC_CREGISTER) and
  256. (hr <> pleftreg) then
  257. begin
  258. hr:=cg.get_scratch_reg_int(exprasmlist);
  259. cg.a_op_const_reg_reg(exprasmlist,OP_SUB,opsize,setparts[i].start,pleftreg,hr);
  260. pleftreg:=hr;
  261. opsize := OS_INT;
  262. end
  263. else
  264. begin
  265. { otherwise, the value is already in a register }
  266. { that can be modified }
  267. cg.a_op_const_reg(exprasmlist,OP_SUB,
  268. setparts[i].start-adjustment,pleftreg)
  269. end;
  270. { new total value substracted from x: }
  271. { adjustment + (setparts[i].start - adjustment) }
  272. adjustment := setparts[i].start;
  273. { check if result < b-a+1 (not "result <= b-a", since }
  274. { we need a carry in case the element is in the range }
  275. { (this will never overflow since we check at the }
  276. { beginning whether stop-start <> 255) }
  277. cg.a_cmp_const_reg_label(exprasmlist, opsize, OC_B,
  278. setparts[i].stop-setparts[i].start+1,pleftreg,l);
  279. end
  280. else
  281. { if setparts[i].start = 0 and setparts[i].stop = 255, }
  282. { it's always true since "in" is only allowed for bytes }
  283. begin
  284. cg.a_jmp_always(exprasmlist,l);
  285. end;
  286. end
  287. else
  288. begin
  289. { Emit code to check if left is an element }
  290. cg.a_cmp_const_reg_label(exprasmlist, opsize, OC_EQ,
  291. setparts[i].stop-adjustment,pleftreg,l);
  292. end;
  293. { To compensate for not doing a second pass }
  294. right.location.reference.symbol:=nil;
  295. getlabel(l3);
  296. cg.a_jmp_always(exprasmlist,l3);
  297. { Now place the end label if IN success }
  298. cg.a_label(exprasmlist,l);
  299. { result register is 1 }
  300. cg.a_load_const_reg(exprasmlist,OS_INT,1,location.register);
  301. { in case value is not found }
  302. cg.a_label(exprasmlist,l3);
  303. case left.location.loc of
  304. LOC_CREGISTER :
  305. cg.free_scratch_reg(exprasmlist,pleftreg);
  306. LOC_REGISTER :
  307. rg.ungetregister(exprasmlist,pleftreg);
  308. else
  309. begin
  310. reference_release(exprasmlist,left.location.reference);
  311. cg.free_scratch_reg(exprasmlist,pleftreg);
  312. end;
  313. end;
  314. end
  315. else
  316. {*****************************************************************}
  317. { NO JUMP TABLE GENERATION }
  318. {*****************************************************************}
  319. begin
  320. { We will now generated code to check the set itself, no jmps,
  321. handle smallsets separate, because it allows faster checks }
  322. if use_small then
  323. begin
  324. {**************************** SMALL SET **********************}
  325. if left.nodetype=ordconstn then
  326. begin
  327. { clear the register value, indicating result is FALSE }
  328. cg.a_load_const_reg(exprasmlist,OS_INT,0,location.register);
  329. hr:=cg.get_scratch_reg_int(exprasmlist);
  330. case right.location.loc of
  331. LOC_REGISTER,
  332. LOC_CREGISTER:
  333. begin
  334. { load set value into register }
  335. cg.a_load_reg_reg(exprasmlist,OS_INT,
  336. right.location.register,hr);
  337. end;
  338. LOC_REFERENCE,
  339. LOC_CREFERENCE :
  340. begin
  341. { load set value into register }
  342. cg.a_load_ref_reg(exprasmlist,OS_INT,
  343. right.location.reference,hr);
  344. end;
  345. else
  346. internalerror(200203312);
  347. end;
  348. { then do AND with constant and register }
  349. cg.a_op_const_reg(exprasmlist,OP_AND,1 shl
  350. (tordconstnode(left).value and 31),hr);
  351. { if the value in the AND register is <> 0 then the value is equal. }
  352. cg.a_cmp_const_reg_label(exprasmlist,OS_32,OC_EQ,1 shl
  353. (tordconstnode(left).value and 31),hr,l);
  354. cg.free_scratch_reg(exprasmlist,hr);
  355. getlabel(l3);
  356. cg.a_jmp_always(exprasmlist,l3);
  357. { Now place the end label if IN success }
  358. cg.a_label(exprasmlist,l);
  359. { result register is 1 : LOC_JUMP }
  360. cg.a_load_const_reg(exprasmlist,OS_INT,1,location.register);
  361. { in case value is not found }
  362. cg.a_label(exprasmlist,l3);
  363. location_release(exprasmlist,right.location);
  364. end
  365. else
  366. begin
  367. case left.location.loc of
  368. LOC_REGISTER,
  369. LOC_CREGISTER:
  370. begin
  371. hr3:=rg.makeregsize(left.location.register,OS_INT);
  372. cg.a_load_reg_reg(exprasmlist,left.location.size,left.location.register,hr3);
  373. hr:=cg.get_scratch_reg_int(exprasmlist);
  374. cg.a_load_reg_reg(exprasmlist,OS_INT,hr3,hr);
  375. end;
  376. else
  377. begin
  378. hr:=cg.get_scratch_reg_int(exprasmlist);
  379. cg.a_load_ref_reg(exprasmlist,def_cgsize(left.resulttype.def),
  380. left.location.reference,hr);
  381. location_release(exprasmlist,left.location);
  382. end;
  383. end;
  384. case right.location.loc of
  385. LOC_REGISTER,
  386. LOC_CREGISTER :
  387. begin
  388. hr2:=right.location.register;
  389. end;
  390. LOC_CONSTANT :
  391. begin
  392. hr2:=rg.getregisterint(exprasmlist);
  393. cg.a_load_const_reg(exprasmlist,OS_32,
  394. right.location.value,hr2);
  395. end;
  396. LOC_CREFERENCE,
  397. LOC_REFERENCE :
  398. begin
  399. location_release(exprasmlist,right.location);
  400. hr2:=rg.getregisterint(exprasmlist);
  401. cg.a_load_ref_reg(exprasmlist, OS_32,
  402. right.location.reference,hr2);
  403. end;
  404. else
  405. internalerror(2002032210);
  406. end;
  407. { emit bit test operation }
  408. emit_bit_test_reg_reg(exprasmlist,hr,hr2,location.register);
  409. { free the resources }
  410. case right.location.loc of
  411. LOC_REGISTER,
  412. LOC_CREGISTER :
  413. rg.ungetregisterint(exprasmlist,right.location.register);
  414. LOC_CONSTANT ,
  415. LOC_CREFERENCE,
  416. LOC_REFERENCE :
  417. rg.ungetregisterint(exprasmlist,hr2);
  418. else
  419. internalerror(2002032210);
  420. end;
  421. { free bitnumber register }
  422. cg.free_scratch_reg(exprasmlist,hr);
  423. end;
  424. end
  425. else
  426. {************************** NOT SMALL SET ********************}
  427. begin
  428. if right.location.loc=LOC_CONSTANT then
  429. begin
  430. { this section has not been tested! }
  431. { can it actually occur currently? CEC }
  432. internalerror(20020610);
  433. getlabel(l);
  434. getlabel(l2);
  435. { Is this treated in firstpass ?? }
  436. if left.nodetype=ordconstn then
  437. begin
  438. hr:=rg.getregisterint(exprasmlist);
  439. left.location.loc:=LOC_REGISTER;
  440. left.location.size:=OS_INT;
  441. left.location.register:=hr;
  442. cg.a_load_const_reg(exprasmlist,OS_INT,
  443. tordconstnode(left).value,hr);
  444. end;
  445. case left.location.loc of
  446. LOC_REGISTER,
  447. LOC_CREGISTER:
  448. begin
  449. hr:=rg.makeregsize(left.location.register,OS_INT);
  450. cg.a_load_reg_reg(exprasmlist,left.location.size,left.location.register,hr);
  451. cg.a_cmp_const_reg_label(exprasmlist,OS_INT,OC_BE,31,hr,l);
  452. { reset of result register is done in routine entry }
  453. cg.a_jmp_always(exprasmlist,l2);
  454. cg.a_label(exprasmlist,l);
  455. { We have to load the value into a register because
  456. btl does not accept values only refs or regs (PFV) }
  457. hr2:=rg.getregisterint(exprasmlist);
  458. cg.a_load_const_reg(exprasmlist,OS_INT,right.location.value,hr2);
  459. end;
  460. else
  461. begin
  462. cg.a_cmp_const_ref_label(exprasmlist,OS_8,OC_BE,31,left.location.reference,l);
  463. cg.a_jmp_always(exprasmlist,l2);
  464. cg.a_label(exprasmlist,l);
  465. location_release(exprasmlist,left.location);
  466. hr:=rg.getregisterint(exprasmlist);
  467. cg.a_load_ref_reg(exprasmlist,OS_32,left.location.reference,hr);
  468. { We have to load the value into a register because
  469. btl does not accept values only refs or regs (PFV) }
  470. hr2:=rg.getregisterint(exprasmlist);
  471. cg.a_load_const_reg(exprasmlist,OS_INT,
  472. right.location.value,hr2);
  473. end;
  474. end;
  475. { emit bit test operation }
  476. emit_bit_test_reg_reg(exprasmlist,hr,hr2,location.register);
  477. rg.ungetregisterint(exprasmlist,hr2);
  478. if not (left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  479. rg.ungetregisterint(exprasmlist,hr);
  480. cg.a_label(exprasmlist,l2);
  481. end { of right.location.loc=LOC_CONSTANT }
  482. { do search in a normal set which could have >32 elementsm
  483. but also used if the left side contains higher values > 32 }
  484. else if left.nodetype=ordconstn then
  485. begin
  486. getlabel(l2);
  487. getlabel(l);
  488. { use location.register as scratch register here }
  489. inc(right.location.reference.offset,tordconstnode(left).value shr 3);
  490. cg.a_load_ref_reg(exprasmlist, OS_8, right.location.reference, location.register);
  491. cg.a_op_const_reg(exprasmlist, OP_AND,1 shl (tordconstnode(left).value and 7),
  492. location.register);
  493. cg.a_cmp_const_reg_label(exprasmlist,OS_8, OC_NE,0,location.register,l2);
  494. cg.a_load_const_reg(exprasmlist, OS_INT,0, location.register);
  495. cg.a_jmp_always(exprasmlist,l);
  496. cg.a_label(exprasmlist,l2);
  497. cg.a_load_const_reg(exprasmlist, OS_INT,1, location.register);
  498. {emit_const_ref(A_TEST,S_B,1 shl (tordconstnode(left).value and 7),right.location.reference);}
  499. cg.a_label(exprasmlist,l);
  500. location_release(exprasmlist,right.location);
  501. end
  502. else
  503. begin
  504. if (left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  505. pleftreg:=rg.makeregsize(left.location.register,OS_INT)
  506. else
  507. pleftreg:=rg.getregisterint(exprasmlist);
  508. cg.a_load_loc_reg(exprasmlist,left.location,pleftreg);
  509. location_freetemp(exprasmlist,left.location);
  510. location_release(exprasmlist,left.location);
  511. cg.a_param_reg(exprasmlist,OS_8,pleftreg,paramanager.getintparaloc(2));
  512. cg.a_param_ref(exprasmlist,OS_ADDR,right.location.reference,paramanager.getintparaloc(1));
  513. cg.a_call_name(exprasmlist,'FPC_SET_IN_BYTE');
  514. { result of value is always one full register }
  515. cg.a_load_reg_reg(exprasmlist,OS_INT,ACCUMULATOR,location.register);
  516. { release the allocated register }
  517. if not (left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  518. rg.ungetregisterint(exprasmlist,pleftreg);
  519. location_release(exprasmlist,right.location);
  520. end;
  521. end;
  522. end;
  523. location_freetemp(exprasmlist,right.location);
  524. end;
  525. begin
  526. csetelementnode:=tcgsetelementnode;
  527. cinnode:=tcginnode;
  528. end.
  529. {
  530. $Log$
  531. Revision 1.7 2002-07-21 16:58:20 jonas
  532. * fixed some bugs in tcginnode.pass_2() and optimized the bit test
  533. Revision 1.6 2002/07/20 11:57:54 florian
  534. * types.pas renamed to defbase.pas because D6 contains a types
  535. unit so this would conflicts if D6 programms are compiled
  536. + Willamette/SSE2 instructions to assembler added
  537. Revision 1.5 2002/07/11 14:41:28 florian
  538. * start of the new generic parameter handling
  539. Revision 1.4 2002/07/07 10:16:29 florian
  540. * problems with last commit fixed
  541. Revision 1.3 2002/07/06 20:19:25 carl
  542. + generic set handling
  543. Revision 1.2 2002/07/01 16:23:53 peter
  544. * cg64 patch
  545. * basics for currency
  546. * asnode updates for class and interface (not finished)
  547. Revision 1.1 2002/06/16 08:14:56 carl
  548. + generic sets
  549. }