n386set.pas 8.4 KB

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