n386set.pas 8.4 KB

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