nx86set.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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,
  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. genjumps,
  65. use_small,
  66. ranges : boolean;
  67. hreg,hreg2,
  68. pleftreg : tregister;
  69. opsize : tcgsize;
  70. orgopsize : 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. resultdef 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_opt_size in current_settings.optimizerswitches 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. end
  128. else
  129. begin
  130. {Extend a range.}
  131. setparts[numparts].stop:=i;
  132. end;
  133. end;
  134. analizeset:=true;
  135. end;
  136. begin
  137. { We check first if we can generate jumps, this can be done
  138. because the resultdef is already set in firstpass }
  139. { check if we can use smallset operation using btl which is limited
  140. to 32 bits, the left side may also not contain higher values or be signed !! }
  141. use_small:=(tsetdef(right.resultdef).settype=smallset) and not is_signed(left.resultdef) and
  142. ((left.resultdef.typ=orddef) and (torddef(left.resultdef).high<=32) or
  143. (left.resultdef.typ=enumdef) and (tenumdef(left.resultdef).max<=32));
  144. { Can we generate jumps? Possible for all types of sets }
  145. genjumps:=(right.nodetype=setconstn) and
  146. analizeset(tsetconstnode(right).value_set,use_small);
  147. { calculate both operators }
  148. { the complex one first }
  149. firstcomplex(self);
  150. secondpass(left);
  151. { Only process the right if we are not generating jumps }
  152. if not genjumps then
  153. begin
  154. secondpass(right);
  155. end;
  156. if codegenerror then
  157. exit;
  158. { ofcourse not commutative }
  159. if nf_swapped in flags then
  160. swapleftright;
  161. orgopsize := def_cgsize(left.resultdef);
  162. opsize := OS_32;
  163. if is_signed(left.resultdef) then
  164. opsize := tcgsize(ord(opsize)+(ord(OS_S8)-ord(OS_8)));
  165. if not(left.location.loc in [LOC_REGISTER,LOC_CREGISTER,LOC_REFERENCE,LOC_CREFERENCE]) then
  166. location_force_reg(current_asmdata.CurrAsmList,left.location,opsize,true);
  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. { load and zero or sign extend as necessary }
  173. location_force_reg(current_asmdata.CurrAsmList,left.location,opsize,false);
  174. pleftreg:=left.location.register;
  175. { Get a label to jump to the end }
  176. location_reset(location,LOC_FLAGS,OS_NO);
  177. { It's better to use the zero flag when there are
  178. no ranges }
  179. if ranges then
  180. location.resflags:=F_C
  181. else
  182. location.resflags:=F_E;
  183. current_asmdata.getjumplabel(l);
  184. { how much have we already substracted from the x in the }
  185. { "x in [y..z]" expression }
  186. adjustment := 0;
  187. for i:=1 to numparts do
  188. if setparts[i].range then
  189. { use fact that a <= x <= b <=> cardinal(x-a) <= cardinal(b-a) }
  190. begin
  191. { is the range different from all legal values? }
  192. if (setparts[i].stop-setparts[i].start <> 255) or not (orgopsize = OS_8) then
  193. begin
  194. { yes, is the lower bound <> 0? }
  195. if (setparts[i].start <> 0) then
  196. begin
  197. location_force_reg(current_asmdata.CurrAsmList,left.location,opsize,false);
  198. hreg:=left.location.register;
  199. pleftreg:=hreg;
  200. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SUB,opsize,setparts[i].start-adjustment,pleftreg);
  201. end;
  202. { new total value substracted from x: }
  203. { adjustment + (setparts[i].start - adjustment) }
  204. adjustment := setparts[i].start;
  205. { check if result < b-a+1 (not "result <= b-a", since }
  206. { we need a carry in case the element is in the range }
  207. { (this will never overflow since we check at the }
  208. { beginning whether stop-start <> 255) }
  209. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,OC_B,setparts[i].stop-setparts[i].start+1,pleftreg,l);
  210. end
  211. else
  212. { if setparts[i].start = 0 and setparts[i].stop = 255, }
  213. { it's always true since "in" is only allowed for bytes }
  214. begin
  215. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_STC,S_NO));
  216. cg.a_jmp_always(current_asmdata.CurrAsmList,l);
  217. end;
  218. end
  219. else
  220. begin
  221. { Emit code to check if left is an element }
  222. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,TCGSize2OpSize[opsize],setparts[i].stop-adjustment,
  223. pleftreg));
  224. { Result should be in carry flag when ranges are used }
  225. if ranges then
  226. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_STC,S_NO));
  227. { If found, jump to end }
  228. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_E,l);
  229. end;
  230. if ranges and
  231. { if the last one was a range, the carry flag is already }
  232. { set appropriately }
  233. not(setparts[numparts].range) then
  234. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_CLC,S_NO));
  235. { To compensate for not doing a second pass }
  236. right.location.reference.symbol:=nil;
  237. { Now place the end label }
  238. cg.a_label(current_asmdata.CurrAsmList,l);
  239. end
  240. else
  241. begin
  242. location_reset(location,LOC_FLAGS,OS_NO);
  243. { We will now generated code to check the set itself, no jmps,
  244. handle smallsets separate, because it allows faster checks }
  245. if use_small then
  246. begin
  247. if left.nodetype=ordconstn then
  248. begin
  249. location.resflags:=F_NE;
  250. case right.location.loc of
  251. LOC_REGISTER,
  252. LOC_CREGISTER:
  253. begin
  254. emit_const_reg(A_TEST,S_L,
  255. 1 shl (tordconstnode(left).value and 31),right.location.register);
  256. end;
  257. LOC_REFERENCE,
  258. LOC_CREFERENCE :
  259. begin
  260. emit_const_ref(A_TEST,S_L,1 shl (tordconstnode(left).value and 31),
  261. right.location.reference);
  262. end;
  263. else
  264. internalerror(200203312);
  265. end;
  266. end
  267. else
  268. begin
  269. location_force_reg(current_asmdata.CurrAsmList,left.location,OS_32,true);
  270. hreg:=left.location.register;
  271. case right.location.loc of
  272. LOC_REGISTER,
  273. LOC_CREGISTER :
  274. begin
  275. emit_reg_reg(A_BT,S_L,hreg,right.location.register);
  276. end;
  277. LOC_CONSTANT :
  278. begin
  279. { We have to load the value into a register because
  280. btl does not accept values only refs or regs (PFV) }
  281. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  282. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_32,right.location.value,hreg2);
  283. emit_reg_reg(A_BT,S_L,hreg,hreg2);
  284. end;
  285. LOC_CREFERENCE,
  286. LOC_REFERENCE :
  287. begin
  288. emit_reg_ref(A_BT,S_L,hreg,right.location.reference);
  289. end;
  290. else
  291. internalerror(2002032210);
  292. end;
  293. location.resflags:=F_C;
  294. end;
  295. end
  296. else
  297. begin
  298. if right.location.loc=LOC_CONSTANT then
  299. begin
  300. location.resflags:=F_C;
  301. current_asmdata.getjumplabel(l);
  302. current_asmdata.getjumplabel(l2);
  303. { load constants to a register }
  304. if left.nodetype=ordconstn then
  305. location_force_reg(current_asmdata.CurrAsmList,left.location,opsize,true);
  306. case left.location.loc of
  307. LOC_REGISTER,
  308. LOC_CREGISTER:
  309. begin
  310. hreg:=cg.makeregsize(current_asmdata.CurrAsmList,left.location.register,opsize);
  311. cg.a_load_reg_reg(current_asmdata.CurrAsmList,left.location.size,opsize,left.location.register,hreg);
  312. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,OC_BE,31,hreg,l);
  313. { reset carry flag }
  314. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_CLC,S_NO));
  315. cg.a_jmp_always(current_asmdata.CurrAsmList,l2);
  316. cg.a_label(current_asmdata.CurrAsmList,l);
  317. { We have to load the value into a register because
  318. btl does not accept values only refs or regs (PFV) }
  319. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  320. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_32,right.location.value,hreg2);
  321. emit_reg_reg(A_BT,S_L,hreg,hreg2);
  322. end;
  323. else
  324. begin
  325. emit_const_ref(A_CMP,TCGSize2OpSize[orgopsize],31,left.location.reference);
  326. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_BE,l);
  327. { reset carry flag }
  328. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_CLC,S_NO));
  329. cg.a_jmp_always(current_asmdata.CurrAsmList,l2);
  330. cg.a_label(current_asmdata.CurrAsmList,l);
  331. hreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  332. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_32,OS_32,left.location.reference,hreg);
  333. { We have to load the value into a register because
  334. btl does not accept values only refs or regs (PFV) }
  335. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  336. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_32,right.location.value,hreg2);
  337. emit_reg_reg(A_BT,S_L,hreg,hreg2);
  338. end;
  339. end;
  340. cg.a_label(current_asmdata.CurrAsmList,l2);
  341. end { of right.location.loc=LOC_CONSTANT }
  342. { do search in a normal set which could have >32 elementsm
  343. but also used if the left side contains values > 32 or < 0 }
  344. else if left.nodetype=ordconstn then
  345. begin
  346. if (tordconstnode(left).value < 0) or ((tordconstnode(left).value shr 3) >= right.resultdef.size) then
  347. {should be caught earlier }
  348. internalerror(2007020201);
  349. location.resflags:=F_NE;
  350. inc(right.location.reference.offset,tordconstnode(left).value shr 3);
  351. emit_const_ref(A_TEST,S_B,1 shl (tordconstnode(left).value and 7),right.location.reference);
  352. end
  353. else
  354. begin
  355. location_force_reg(current_asmdata.CurrAsmList,left.location,opsize,false);
  356. pleftreg:=left.location.register;
  357. if (opsize >= OS_S8) or { = if signed }
  358. ((left.resultdef.typ=orddef) and (torddef(left.resultdef).high > tsetdef(right.resultdef).setmax)) or
  359. ((left.resultdef.typ=enumdef) and (tenumdef(left.resultdef).max > tsetdef(right.resultdef).setmax)) then
  360. begin
  361. { we have to check if the value is < 0 or > setmax }
  362. current_asmdata.getjumplabel(l);
  363. current_asmdata.getjumplabel(l2);
  364. { BE will be false for negative values }
  365. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,OC_BE,tsetdef(right.resultdef).setmax,pleftreg,l);
  366. { reset carry flag }
  367. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_CLC,S_NO));
  368. cg.a_jmp_always(current_asmdata.CurrAsmList,l2);
  369. cg.a_label(current_asmdata.CurrAsmList,l);
  370. case right.location.loc of
  371. LOC_REGISTER, LOC_CREGISTER :
  372. emit_reg_reg(A_BT,S_L,pleftreg,right.location.register);
  373. LOC_CREFERENCE, LOC_REFERENCE :
  374. emit_reg_ref(A_BT,S_L,pleftreg,right.location.reference);
  375. else
  376. internalerror(2007020301);
  377. end;
  378. cg.a_label(current_asmdata.CurrAsmList,l2);
  379. location.resflags:=F_C;
  380. end
  381. else
  382. begin
  383. case right.location.loc of
  384. LOC_REGISTER, LOC_CREGISTER :
  385. emit_reg_reg(A_BT,S_L,pleftreg,right.location.register);
  386. LOC_CREFERENCE, LOC_REFERENCE :
  387. emit_reg_ref(A_BT,S_L,pleftreg,right.location.reference);
  388. else
  389. internalerror(2007020302);
  390. end;
  391. location.resflags:=F_C;
  392. end;
  393. end;
  394. end;
  395. end;
  396. if not genjumps then
  397. location_freetemp(current_asmdata.CurrAsmList,right.location);
  398. end;
  399. begin
  400. cinnode:=tx86innode;
  401. end.