nx86set.pas 23 KB

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