nx86set.pas 23 KB

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