2
0

nx86set.pas 23 KB

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