n386set.pas 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Generate i386 assembler for in set/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 n386set;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,
  22. node,nset,pass_1,ncgset;
  23. type
  24. ti386casenode = class(tcgcasenode)
  25. procedure optimizevalues(var max_linear_list:aint;var max_dist:aword);override;
  26. function has_jumptable : boolean;override;
  27. procedure genjumptable(hp : pcaselabel;min_,max_ : aint);override;
  28. procedure genlinearlist(hp : pcaselabel);override;
  29. end;
  30. implementation
  31. uses
  32. systems,
  33. verbose,globals,
  34. symconst,symdef,defutil,
  35. aasmbase,aasmtai,aasmcpu,
  36. cgbase,pass_2,
  37. ncon,
  38. cpubase,cpuinfo,procinfo,
  39. cga,cgutils,cgobj,ncgutil,
  40. cgx86;
  41. {*****************************************************************************
  42. TI386CASENODE
  43. *****************************************************************************}
  44. procedure ti386casenode.optimizevalues(var max_linear_list:aint;var max_dist:aword);
  45. begin
  46. { a jump table crashes the pipeline! }
  47. if aktoptprocessor=Class386 then
  48. inc(max_linear_list,3)
  49. else if aktoptprocessor=ClassPentium then
  50. inc(max_linear_list,6)
  51. else if aktoptprocessor in [ClassPentium2,ClassPentium3] then
  52. inc(max_linear_list,9)
  53. else if aktoptprocessor=ClassPentium4 then
  54. inc(max_linear_list,14);
  55. end;
  56. function ti386casenode.has_jumptable : boolean;
  57. begin
  58. has_jumptable:=true;
  59. end;
  60. procedure ti386casenode.genjumptable(hp : pcaselabel;min_,max_ : aint);
  61. var
  62. table : tasmlabel;
  63. last : TConstExprInt;
  64. indexreg : tregister;
  65. href : treference;
  66. jumpsegment : TAAsmOutput;
  67. procedure genitem(t : pcaselabel);
  68. var
  69. i : aint;
  70. begin
  71. if assigned(t^.less) then
  72. genitem(t^.less);
  73. { fill possible hole }
  74. for i:=last+1 to t^._low-1 do
  75. jumpSegment.concat(Tai_const.Create_sym(elselabel));
  76. for i:=t^._low to t^._high do
  77. jumpSegment.concat(Tai_const.Create_sym(blocklabel(t^.blockid)));
  78. last:=t^._high;
  79. if assigned(t^.greater) then
  80. genitem(t^.greater);
  81. end;
  82. begin
  83. if (cs_create_smart in aktmoduleswitches) or
  84. (af_smartlink_sections in target_asm.flags) then
  85. jumpsegment:=current_procinfo.aktlocaldata
  86. else
  87. jumpsegment:=datasegment;
  88. if not(jumptable_no_range) then
  89. begin
  90. { case expr less than min_ => goto elselabel }
  91. cg.a_cmp_const_reg_label(exprasmlist,opsize,jmp_lt,aint(min_),hregister,elselabel);
  92. { case expr greater than max_ => goto elselabel }
  93. cg.a_cmp_const_reg_label(exprasmlist,opsize,jmp_gt,aint(max_),hregister,elselabel);
  94. end;
  95. objectlibrary.getlabel(table);
  96. { make it a 32bit register }
  97. indexreg:=cg.makeregsize(exprasmlist,hregister,OS_INT);
  98. cg.a_load_reg_reg(exprasmlist,opsize,OS_INT,hregister,indexreg);
  99. { create reference }
  100. reference_reset_symbol(href,table,0);
  101. href.offset:=(-aint(min_))*4;
  102. href.index:=indexreg;
  103. href.scalefactor:=4;
  104. emit_ref(A_JMP,S_NO,href);
  105. { generate jump table }
  106. if not(cs_littlesize in aktglobalswitches) then
  107. jumpSegment.concat(Tai_Align.Create_Op(4,0));
  108. jumpSegment.concat(Tai_label.Create(table));
  109. last:=min_;
  110. genitem(hp);
  111. end;
  112. procedure ti386casenode.genlinearlist(hp : pcaselabel);
  113. var
  114. first : boolean;
  115. lastrange : boolean;
  116. last : TConstExprInt;
  117. cond_lt,cond_le : tresflags;
  118. procedure genitem(t : pcaselabel);
  119. begin
  120. if assigned(t^.less) then
  121. genitem(t^.less);
  122. { need we to test the first value }
  123. if first and (t^._low>get_min_value(left.resulttype.def)) then
  124. begin
  125. cg.a_cmp_const_reg_label(exprasmlist,opsize,jmp_lt,aint(t^._low),hregister,elselabel);
  126. end;
  127. if t^._low=t^._high then
  128. begin
  129. if t^._low-last=0 then
  130. cg.a_cmp_const_reg_label(exprasmlist, opsize, OC_EQ,0,hregister,blocklabel(t^.blockid))
  131. else
  132. begin
  133. cg.a_op_const_reg(exprasmlist, OP_SUB, opsize, aint(t^._low-last), hregister);
  134. cg.a_jmp_flags(exprasmlist,F_E,blocklabel(t^.blockid));
  135. end;
  136. last:=t^._low;
  137. lastrange:=false;
  138. end
  139. else
  140. begin
  141. { it begins with the smallest label, if the value }
  142. { is even smaller then jump immediately to the }
  143. { ELSE-label }
  144. if first then
  145. begin
  146. { have we to ajust the first value ? }
  147. if (t^._low>get_min_value(left.resulttype.def)) then
  148. cg.a_op_const_reg(exprasmlist, OP_SUB, opsize, aint(t^._low), hregister);
  149. end
  150. else
  151. begin
  152. { if there is no unused label between the last and the }
  153. { present label then the lower limit can be checked }
  154. { immediately. else check the range in between: }
  155. cg.a_op_const_reg(exprasmlist, OP_SUB, opsize, aint(t^._low-last), hregister);
  156. { no jump necessary here if the new range starts at }
  157. { at the value following the previous one }
  158. if ((t^._low-last) <> 1) or
  159. (not lastrange) then
  160. cg.a_jmp_flags(exprasmlist,cond_lt,elselabel);
  161. end;
  162. {we need to use A_SUB, because A_DEC does not set the correct flags, therefor
  163. using a_op_const_reg(OP_SUB) is not possible }
  164. emit_const_reg(A_SUB,TCGSize2OpSize[opsize],aint(t^._high-t^._low),hregister);
  165. cg.a_jmp_flags(exprasmlist,cond_le,blocklabel(t^.blockid));
  166. last:=t^._high;
  167. lastrange:=true;
  168. end;
  169. first:=false;
  170. if assigned(t^.greater) then
  171. genitem(t^.greater);
  172. end;
  173. begin
  174. if with_sign then
  175. begin
  176. cond_lt:=F_L;
  177. cond_le:=F_LE;
  178. end
  179. else
  180. begin
  181. cond_lt:=F_B;
  182. cond_le:=F_BE;
  183. end;
  184. { do we need to generate cmps? }
  185. if (with_sign and (min_label<0)) then
  186. genlinearcmplist(hp)
  187. else
  188. begin
  189. last:=0;
  190. lastrange:=false;
  191. first:=true;
  192. genitem(hp);
  193. cg.a_jmp_always(exprasmlist,elselabel);
  194. end;
  195. end;
  196. begin
  197. ccasenode:=ti386casenode;
  198. end.