nx86set.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. node,nset,pass_1,ncgset;
  22. type
  23. tx86innode = class(tinnode)
  24. procedure pass_generate_code;override;
  25. function pass_1 : tnode;override;
  26. end;
  27. implementation
  28. uses
  29. globtype,systems,constexp,
  30. verbose,globals,
  31. symconst,symdef,defutil,
  32. aasmbase,aasmtai,aasmdata,aasmcpu,
  33. cgbase,pass_2,tgobj,
  34. ncon,
  35. cpubase,
  36. cga,cgobj,cgutils,ncgutil,
  37. cgx86;
  38. {*****************************************************************************
  39. TX86INNODE
  40. *****************************************************************************}
  41. function tx86innode.pass_1 : tnode;
  42. begin
  43. result:=nil;
  44. { this is the only difference from the generic version }
  45. expectloc:=LOC_FLAGS;
  46. firstpass(right);
  47. firstpass(left);
  48. if codegenerror then
  49. exit;
  50. left_right_max;
  51. { a smallset needs maybe an misc. register }
  52. if (left.nodetype<>ordconstn) and
  53. not(right.expectloc in [LOC_CREGISTER,LOC_REGISTER]) and
  54. (right.registersint<1) then
  55. inc(registersint);
  56. end;
  57. procedure tx86innode.pass_generate_code;
  58. type
  59. Tsetpart=record
  60. range : boolean; {Part is a range.}
  61. start,stop : byte; {Start/stop when range; Stop=element when an element.}
  62. end;
  63. var
  64. hreg,hreg2,
  65. pleftreg : tregister;
  66. opsize : tcgsize;
  67. orgopsize : tcgsize;
  68. setparts : array[1..8] of Tsetpart;
  69. setbase : aint;
  70. adjustment : longint;
  71. l,l2 : tasmlabel;
  72. i,numparts : byte;
  73. genjumps,
  74. use_small,
  75. ranges : boolean;
  76. {$ifdef CORRECT_SET_IN_FPC}
  77. AM : tasmop;
  78. {$endif CORRECT_SET_IN_FPC}
  79. function analizeset(Aset:pconstset;is_small:boolean):boolean;
  80. var
  81. compares,maxcompares:word;
  82. i:byte;
  83. begin
  84. if tnormalset(Aset^)=[] then
  85. {The expression...
  86. if expr in []
  87. ...is allways false. It should be optimized away in the
  88. resultdef pass, and thus never occur here. Since we
  89. do generate wrong code for it, do internalerror.}
  90. internalerror(2002072301);
  91. analizeset:=false;
  92. ranges:=false;
  93. numparts:=0;
  94. compares:=0;
  95. { Lots of comparisions take a lot of time, so do not allow
  96. too much comparisions. 8 comparisions are, however, still
  97. smalller than emitting the set }
  98. if cs_opt_size in current_settings.optimizerswitches then
  99. maxcompares:=8
  100. else
  101. maxcompares:=5;
  102. { when smallset is possible allow only 3 compares the smallset
  103. code is for littlesize also smaller when more compares are used }
  104. if is_small then
  105. maxcompares:=3;
  106. for i:=0 to 255 do
  107. if i in tnormalset(Aset^) then
  108. begin
  109. if (numparts=0) or (i<>setparts[numparts].stop+1) then
  110. begin
  111. {Set element is a separate element.}
  112. inc(compares);
  113. if compares>maxcompares then
  114. exit;
  115. inc(numparts);
  116. setparts[numparts].range:=false;
  117. setparts[numparts].stop:=i;
  118. end
  119. else
  120. {Set element is part of a range.}
  121. if not setparts[numparts].range then
  122. begin
  123. {Transform an element into a range.}
  124. setparts[numparts].range:=true;
  125. setparts[numparts].start:=setparts[numparts].stop;
  126. setparts[numparts].stop:=i;
  127. ranges := true;
  128. end
  129. else
  130. begin
  131. {Extend a range.}
  132. setparts[numparts].stop:=i;
  133. end;
  134. end;
  135. analizeset:=true;
  136. end;
  137. begin
  138. { We check first if we can generate jumps, this can be done
  139. because the resultdef is already set in firstpass }
  140. { check if we can use smallset operation using btl which is limited
  141. to 32 bits, the left side may also not contain higher values or be signed !! }
  142. use_small:=(tsetdef(right.resultdef).settype=smallset) and not is_signed(left.resultdef) and
  143. ((left.resultdef.typ=orddef) and (torddef(left.resultdef).high.svalue<32) or
  144. (left.resultdef.typ=enumdef) and (tenumdef(left.resultdef).max<32));
  145. { Can we generate jumps? Possible for all types of sets }
  146. genjumps:=(right.nodetype=setconstn) and
  147. analizeset(tsetconstnode(right).value_set,use_small);
  148. { calculate both operators }
  149. { the complex one first }
  150. { not in case of genjumps, because then we don't secondpass }
  151. { right at all (so we have to make sure that "right" really is }
  152. { "right" and not "swapped left" in that case) }
  153. if not(genjumps) then
  154. firstcomplex(self);
  155. secondpass(left);
  156. { Only process the right if we are not generating jumps }
  157. if not genjumps then
  158. begin
  159. secondpass(right);
  160. end;
  161. if codegenerror then
  162. exit;
  163. { ofcourse not commutative }
  164. if nf_swapped in flags then
  165. swapleftright;
  166. orgopsize := def_cgsize(left.resultdef);
  167. opsize := OS_32;
  168. if is_signed(left.resultdef) then
  169. opsize := tcgsize(ord(opsize)+(ord(OS_S8)-ord(OS_8)));
  170. if not(left.location.loc in [LOC_REGISTER,LOC_CREGISTER,LOC_REFERENCE,LOC_CREFERENCE,LOC_CONSTANT]) then
  171. location_force_reg(current_asmdata.CurrAsmList,left.location,opsize,true);
  172. if (right.location.loc in [LOC_SUBSETREG,LOC_CSUBSETREG]) then
  173. location_force_reg(current_asmdata.CurrAsmList,right.location,opsize,true);
  174. if genjumps then
  175. begin
  176. { It gives us advantage to check for the set elements
  177. separately instead of using the SET_IN_BYTE procedure.
  178. To do: Build in support for LOC_JUMP }
  179. { load and zero or sign extend as necessary }
  180. location_force_reg(current_asmdata.CurrAsmList,left.location,opsize,false);
  181. pleftreg:=left.location.register;
  182. { Get a label to jump to the end }
  183. location_reset(location,LOC_FLAGS,OS_NO);
  184. { It's better to use the zero flag when there are
  185. no ranges }
  186. if ranges then
  187. location.resflags:=F_C
  188. else
  189. location.resflags:=F_E;
  190. current_asmdata.getjumplabel(l);
  191. { how much have we already substracted from the x in the }
  192. { "x in [y..z]" expression }
  193. adjustment := 0;
  194. for i:=1 to numparts do
  195. if setparts[i].range then
  196. { use fact that a <= x <= b <=> cardinal(x-a) <= cardinal(b-a) }
  197. begin
  198. { is the range different from all legal values? }
  199. if (setparts[i].stop-setparts[i].start <> 255) or not (orgopsize = OS_8) then
  200. begin
  201. { yes, is the lower bound <> 0? }
  202. if (setparts[i].start <> 0) then
  203. begin
  204. location_force_reg(current_asmdata.CurrAsmList,left.location,opsize,false);
  205. hreg:=left.location.register;
  206. pleftreg:=hreg;
  207. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SUB,opsize,setparts[i].start-adjustment,pleftreg);
  208. end;
  209. { new total value substracted from x: }
  210. { adjustment + (setparts[i].start - adjustment) }
  211. adjustment := setparts[i].start;
  212. { check if result < b-a+1 (not "result <= b-a", since }
  213. { we need a carry in case the element is in the range }
  214. { (this will never overflow since we check at the }
  215. { beginning whether stop-start <> 255) }
  216. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,OC_B,setparts[i].stop-setparts[i].start+1,pleftreg,l);
  217. end
  218. else
  219. { if setparts[i].start = 0 and setparts[i].stop = 255, }
  220. { it's always true since "in" is only allowed for bytes }
  221. begin
  222. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_STC,S_NO));
  223. cg.a_jmp_always(current_asmdata.CurrAsmList,l);
  224. end;
  225. end
  226. else
  227. begin
  228. { Emit code to check if left is an element }
  229. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,TCGSize2OpSize[opsize],setparts[i].stop-adjustment,
  230. pleftreg));
  231. { Result should be in carry flag when ranges are used }
  232. if ranges then
  233. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_STC,S_NO));
  234. { If found, jump to end }
  235. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_E,l);
  236. end;
  237. if ranges and
  238. { if the last one was a range, the carry flag is already }
  239. { set appropriately }
  240. not(setparts[numparts].range) then
  241. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_CLC,S_NO));
  242. { To compensate for not doing a second pass }
  243. right.location.reference.symbol:=nil;
  244. { Now place the end label }
  245. cg.a_label(current_asmdata.CurrAsmList,l);
  246. end
  247. else
  248. begin
  249. location_reset(location,LOC_FLAGS,OS_NO);
  250. setbase:=tsetdef(right.resultdef).setbase;
  251. { We will now generated code to check the set itself, no jmps,
  252. handle smallsets separate, because it allows faster checks }
  253. if use_small then
  254. begin
  255. if left.location.loc=LOC_CONSTANT then
  256. begin
  257. location.resflags:=F_NE;
  258. case right.location.loc of
  259. LOC_REGISTER,
  260. LOC_CREGISTER:
  261. begin
  262. emit_const_reg(A_TEST,TCGSize2OpSize[right.location.size],
  263. 1 shl ((left.location.value-setbase) and 31),right.location.register);
  264. end;
  265. LOC_REFERENCE,
  266. LOC_CREFERENCE :
  267. begin
  268. emit_const_ref(A_TEST,TCGSize2OpSize[right.location.size],1 shl ((left.location.value-setbase) and 31),
  269. right.location.reference);
  270. end;
  271. else
  272. internalerror(200203312);
  273. end;
  274. end
  275. else
  276. begin
  277. location_force_reg(current_asmdata.CurrAsmList,left.location,OS_32,true);
  278. register_maybe_adjust_setbase(current_asmdata.CurrAsmList,left.location,setbase);
  279. if (tcgsize2size[right.location.size] < 4) or
  280. (right.location.loc = LOC_CONSTANT) then
  281. location_force_reg(current_asmdata.CurrAsmList,right.location,OS_32,true);
  282. hreg:=left.location.register;
  283. case right.location.loc of
  284. LOC_REGISTER,
  285. LOC_CREGISTER :
  286. begin
  287. emit_reg_reg(A_BT,S_L,hreg,right.location.register);
  288. end;
  289. LOC_CREFERENCE,
  290. LOC_REFERENCE :
  291. begin
  292. emit_reg_ref(A_BT,S_L,hreg,right.location.reference);
  293. end;
  294. else
  295. internalerror(2002032210);
  296. end;
  297. location.resflags:=F_C;
  298. end;
  299. end
  300. else
  301. begin
  302. if right.location.loc=LOC_CONSTANT then
  303. begin
  304. location.resflags:=F_C;
  305. current_asmdata.getjumplabel(l);
  306. current_asmdata.getjumplabel(l2);
  307. { load constants to a register }
  308. if (left.location.loc=LOC_CONSTANT) or
  309. (setbase<>0) then
  310. begin
  311. location_force_reg(current_asmdata.CurrAsmList,left.location,opsize,true);
  312. register_maybe_adjust_setbase(current_asmdata.CurrAsmList,left.location,setbase);
  313. end;
  314. case left.location.loc of
  315. LOC_REGISTER,
  316. LOC_CREGISTER:
  317. begin
  318. hreg:=cg.makeregsize(current_asmdata.CurrAsmList,left.location.register,opsize);
  319. cg.a_load_reg_reg(current_asmdata.CurrAsmList,left.location.size,opsize,left.location.register,hreg);
  320. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,OC_BE,31,hreg,l);
  321. { reset carry flag }
  322. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_CLC,S_NO));
  323. cg.a_jmp_always(current_asmdata.CurrAsmList,l2);
  324. cg.a_label(current_asmdata.CurrAsmList,l);
  325. { We have to load the value into a register because
  326. btl does not accept values only refs or regs (PFV) }
  327. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  328. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_32,right.location.value,hreg2);
  329. emit_reg_reg(A_BT,S_L,hreg,hreg2);
  330. end;
  331. else
  332. begin
  333. emit_const_ref(A_CMP,TCGSize2OpSize[orgopsize],31,left.location.reference);
  334. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_BE,l);
  335. { reset carry flag }
  336. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_CLC,S_NO));
  337. cg.a_jmp_always(current_asmdata.CurrAsmList,l2);
  338. cg.a_label(current_asmdata.CurrAsmList,l);
  339. hreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  340. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_32,OS_32,left.location.reference,hreg);
  341. { We have to load the value into a register because
  342. btl does not accept values only refs or regs (PFV) }
  343. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  344. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_32,right.location.value,hreg2);
  345. emit_reg_reg(A_BT,S_L,hreg,hreg2);
  346. end;
  347. end;
  348. cg.a_label(current_asmdata.CurrAsmList,l2);
  349. end { of right.location.loc=LOC_CONSTANT }
  350. { do search in a normal set which could have >32 elementsm
  351. but also used if the left side contains values > 32 or < 0 }
  352. else if left.location.loc=LOC_CONSTANT then
  353. begin
  354. if (left.location.value<setbase) or (((left.location.value-setbase) shr 3) >= right.resultdef.size) then
  355. {should be caught earlier }
  356. internalerror(2007020201);
  357. location.resflags:=F_NE;
  358. case right.location.loc of
  359. LOC_REFERENCE,LOC_CREFERENCE:
  360. begin
  361. inc(right.location.reference.offset,(left.location.value-setbase) shr 3);
  362. emit_const_ref(A_TEST,S_B,1 shl (left.location.value and 7),right.location.reference);
  363. end;
  364. LOC_REGISTER,LOC_CREGISTER:
  365. begin
  366. emit_const_reg(A_TEST,TCGSize2OpSize[right.location.size],1 shl (left.location.value-setbase),right.location.register);
  367. end;
  368. else
  369. internalerror(2007051901);
  370. end;
  371. end
  372. else
  373. begin
  374. location_force_reg(current_asmdata.CurrAsmList,left.location,opsize,false);
  375. register_maybe_adjust_setbase(current_asmdata.CurrAsmList,left.location,setbase);
  376. pleftreg:=left.location.register;
  377. if (opsize >= OS_S8) or { = if signed }
  378. ((left.resultdef.typ=orddef) and
  379. ((torddef(left.resultdef).low < int64(tsetdef(right.resultdef).setbase)) or
  380. (torddef(left.resultdef).high > int64(tsetdef(right.resultdef).setmax)))) or
  381. ((left.resultdef.typ=enumdef) and
  382. ((tenumdef(left.resultdef).min < tsetdef(right.resultdef).setbase) or
  383. (tenumdef(left.resultdef).max > tsetdef(right.resultdef).setmax))) then
  384. begin
  385. { we have to check if the value is < 0 or > setmax }
  386. current_asmdata.getjumplabel(l);
  387. current_asmdata.getjumplabel(l2);
  388. { BE will be false for negative values }
  389. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,OC_BE,tsetdef(right.resultdef).setmax-tsetdef(right.resultdef).setbase,pleftreg,l);
  390. { reset carry flag }
  391. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_CLC,S_NO));
  392. cg.a_jmp_always(current_asmdata.CurrAsmList,l2);
  393. cg.a_label(current_asmdata.CurrAsmList,l);
  394. pleftreg:=left.location.register;
  395. case right.location.loc of
  396. LOC_REGISTER, LOC_CREGISTER :
  397. emit_reg_reg(A_BT,S_L,pleftreg,right.location.register);
  398. LOC_CREFERENCE, LOC_REFERENCE :
  399. emit_reg_ref(A_BT,S_L,pleftreg,right.location.reference);
  400. else
  401. internalerror(2007020301);
  402. end;
  403. cg.a_label(current_asmdata.CurrAsmList,l2);
  404. location.resflags:=F_C;
  405. end
  406. else
  407. begin
  408. case right.location.loc of
  409. LOC_REGISTER, LOC_CREGISTER :
  410. emit_reg_reg(A_BT,S_L,pleftreg,right.location.register);
  411. LOC_CREFERENCE, LOC_REFERENCE :
  412. emit_reg_ref(A_BT,S_L,pleftreg,right.location.reference);
  413. else
  414. internalerror(2007020302);
  415. end;
  416. location.resflags:=F_C;
  417. end;
  418. end;
  419. end;
  420. end;
  421. if not genjumps then
  422. location_freetemp(current_asmdata.CurrAsmList,right.location);
  423. end;
  424. begin
  425. cinnode:=tx86innode;
  426. end.