n386set.pas 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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,aasmdata,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 current_settings.optimizecputype=cpu_386 then
  48. inc(max_linear_list,3)
  49. else if current_settings.optimizecputype=cpu_Pentium then
  50. inc(max_linear_list,6)
  51. else if current_settings.optimizecputype in [cpu_Pentium2,cpu_Pentium3] then
  52. inc(max_linear_list,9)
  53. else if current_settings.optimizecputype=cpu_Pentium4 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. procedure genitem(list:TAsmList;t : pcaselabel);
  67. var
  68. i : aint;
  69. begin
  70. if assigned(t^.less) then
  71. genitem(list,t^.less);
  72. { fill possible hole }
  73. for i:=last+1 to t^._low-1 do
  74. list.concat(Tai_const.Create_sym(elselabel));
  75. for i:=t^._low to t^._high do
  76. list.concat(Tai_const.Create_sym(blocklabel(t^.blockid)));
  77. last:=t^._high;
  78. if assigned(t^.greater) then
  79. genitem(list,t^.greater);
  80. end;
  81. begin
  82. if not(jumptable_no_range) then
  83. begin
  84. { case expr less than min_ => goto elselabel }
  85. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,jmp_lt,aint(min_),hregister,elselabel);
  86. { case expr greater than max_ => goto elselabel }
  87. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,jmp_gt,aint(max_),hregister,elselabel);
  88. end;
  89. current_asmdata.getjumplabel(table);
  90. { make it a 32bit register }
  91. indexreg:=cg.makeregsize(current_asmdata.CurrAsmList,hregister,OS_INT);
  92. cg.a_load_reg_reg(current_asmdata.CurrAsmList,opsize,OS_INT,hregister,indexreg);
  93. { create reference }
  94. reference_reset_symbol(href,table,0);
  95. href.offset:=(-aint(min_))*4;
  96. href.index:=indexreg;
  97. href.scalefactor:=4;
  98. emit_ref(A_JMP,S_NO,href);
  99. { generate jump table }
  100. new_section(current_procinfo.aktlocaldata,sec_data,current_procinfo.procdef.mangledname,sizeof(aint));
  101. current_procinfo.aktlocaldata.concat(Tai_label.Create(table));
  102. last:=min_;
  103. genitem(current_procinfo.aktlocaldata,hp);
  104. end;
  105. procedure ti386casenode.genlinearlist(hp : pcaselabel);
  106. var
  107. first : boolean;
  108. lastrange : boolean;
  109. last : TConstExprInt;
  110. cond_lt,cond_le : tresflags;
  111. procedure genitem(t : pcaselabel);
  112. begin
  113. if assigned(t^.less) then
  114. genitem(t^.less);
  115. { need we to test the first value }
  116. if first and (t^._low>get_min_value(left.resultdef)) then
  117. begin
  118. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,jmp_lt,aint(t^._low),hregister,elselabel);
  119. end;
  120. if t^._low=t^._high then
  121. begin
  122. if t^._low-last=0 then
  123. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, opsize, OC_EQ,0,hregister,blocklabel(t^.blockid))
  124. else
  125. begin
  126. cg.a_op_const_reg(current_asmdata.CurrAsmList, OP_SUB, opsize, aint(t^._low-last), hregister);
  127. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_E,blocklabel(t^.blockid));
  128. end;
  129. last:=t^._low;
  130. lastrange:=false;
  131. end
  132. else
  133. begin
  134. { it begins with the smallest label, if the value }
  135. { is even smaller then jump immediately to the }
  136. { ELSE-label }
  137. if first then
  138. begin
  139. { have we to ajust the first value ? }
  140. if (t^._low>get_min_value(left.resultdef)) then
  141. cg.a_op_const_reg(current_asmdata.CurrAsmList, OP_SUB, opsize, aint(t^._low), hregister);
  142. end
  143. else
  144. begin
  145. { if there is no unused label between the last and the }
  146. { present label then the lower limit can be checked }
  147. { immediately. else check the range in between: }
  148. cg.a_op_const_reg(current_asmdata.CurrAsmList, OP_SUB, opsize, aint(t^._low-last), hregister);
  149. { no jump necessary here if the new range starts at }
  150. { at the value following the previous one }
  151. if ((t^._low-last) <> 1) or
  152. (not lastrange) then
  153. cg.a_jmp_flags(current_asmdata.CurrAsmList,cond_lt,elselabel);
  154. end;
  155. {we need to use A_SUB, because A_DEC does not set the correct flags, therefor
  156. using a_op_const_reg(OP_SUB) is not possible }
  157. emit_const_reg(A_SUB,TCGSize2OpSize[opsize],aint(t^._high-t^._low),hregister);
  158. cg.a_jmp_flags(current_asmdata.CurrAsmList,cond_le,blocklabel(t^.blockid));
  159. last:=t^._high;
  160. lastrange:=true;
  161. end;
  162. first:=false;
  163. if assigned(t^.greater) then
  164. genitem(t^.greater);
  165. end;
  166. begin
  167. if with_sign then
  168. begin
  169. cond_lt:=F_L;
  170. cond_le:=F_LE;
  171. end
  172. else
  173. begin
  174. cond_lt:=F_B;
  175. cond_le:=F_BE;
  176. end;
  177. { do we need to generate cmps? }
  178. if (with_sign and (min_label<0)) then
  179. genlinearcmplist(hp)
  180. else
  181. begin
  182. last:=0;
  183. lastrange:=false;
  184. first:=true;
  185. genitem(hp);
  186. cg.a_jmp_always(current_asmdata.CurrAsmList,elselabel);
  187. end;
  188. end;
  189. begin
  190. ccasenode:=ti386casenode;
  191. end.