n386set.pas 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Generate i386 assembler for in set/case nodes
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit n386set;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. globtype,
  23. node,nset,pass_1,ncgset;
  24. type
  25. ti386casenode = class(tcgcasenode)
  26. procedure optimizevalues(var max_linear_list:aint;var max_dist:aword);override;
  27. function has_jumptable : boolean;override;
  28. procedure genjumptable(hp : pcaselabel;min_,max_ : aint);override;
  29. procedure genlinearlist(hp : pcaselabel);override;
  30. end;
  31. implementation
  32. uses
  33. systems,
  34. verbose,globals,
  35. symconst,symdef,defutil,
  36. aasmbase,aasmtai,aasmcpu,
  37. cgbase,pass_2,
  38. ncon,
  39. cpubase,cpuinfo,procinfo,
  40. cga,cgutils,cgobj,ncgutil,
  41. cgx86;
  42. {*****************************************************************************
  43. TI386CASENODE
  44. *****************************************************************************}
  45. procedure ti386casenode.optimizevalues(var max_linear_list:aint;var max_dist:aword);
  46. begin
  47. { a jump table crashes the pipeline! }
  48. if aktoptprocessor=Class386 then
  49. inc(max_linear_list,3)
  50. else if aktoptprocessor=ClassPentium then
  51. inc(max_linear_list,6)
  52. else if aktoptprocessor in [ClassPentium2,ClassPentium3] then
  53. inc(max_linear_list,9)
  54. else if aktoptprocessor=ClassPentium4 then
  55. inc(max_linear_list,14);
  56. end;
  57. function ti386casenode.has_jumptable : boolean;
  58. begin
  59. has_jumptable:=true;
  60. end;
  61. procedure ti386casenode.genjumptable(hp : pcaselabel;min_,max_ : aint);
  62. var
  63. table : tasmlabel;
  64. last : TConstExprInt;
  65. indexreg : tregister;
  66. href : treference;
  67. jumpsegment : TAAsmOutput;
  68. procedure genitem(t : pcaselabel);
  69. var
  70. i : aint;
  71. begin
  72. if assigned(t^.less) then
  73. genitem(t^.less);
  74. { fill possible hole }
  75. for i:=last+1 to t^._low-1 do
  76. jumpSegment.concat(Tai_const.Create_sym(elselabel));
  77. for i:=t^._low to t^._high do
  78. jumpSegment.concat(Tai_const.Create_sym(blocklabel(t^.blockid)));
  79. last:=t^._high;
  80. if assigned(t^.greater) then
  81. genitem(t^.greater);
  82. end;
  83. begin
  84. if (cs_create_smart in aktmoduleswitches) or
  85. (af_smartlink_sections in target_asm.flags) then
  86. jumpsegment:=current_procinfo.aktlocaldata
  87. else
  88. jumpsegment:=datasegment;
  89. if not(jumptable_no_range) then
  90. begin
  91. { case expr less than min_ => goto elselabel }
  92. cg.a_cmp_const_reg_label(exprasmlist,opsize,jmp_lt,aint(min_),hregister,elselabel);
  93. { case expr greater than max_ => goto elselabel }
  94. cg.a_cmp_const_reg_label(exprasmlist,opsize,jmp_gt,aint(max_),hregister,elselabel);
  95. end;
  96. objectlibrary.getlabel(table);
  97. { make it a 32bit register }
  98. indexreg:=cg.makeregsize(exprasmlist,hregister,OS_INT);
  99. cg.a_load_reg_reg(exprasmlist,opsize,OS_INT,hregister,indexreg);
  100. { create reference }
  101. reference_reset_symbol(href,table,0);
  102. href.offset:=(-aint(min_))*4;
  103. href.index:=indexreg;
  104. href.scalefactor:=4;
  105. emit_ref(A_JMP,S_NO,href);
  106. { generate jump table }
  107. if not(cs_littlesize in aktglobalswitches) then
  108. jumpSegment.concat(Tai_Align.Create_Op(4,0));
  109. jumpSegment.concat(Tai_label.Create(table));
  110. last:=min_;
  111. genitem(hp);
  112. end;
  113. procedure ti386casenode.genlinearlist(hp : pcaselabel);
  114. var
  115. first : boolean;
  116. lastrange : boolean;
  117. last : TConstExprInt;
  118. cond_lt,cond_le : tresflags;
  119. procedure genitem(t : pcaselabel);
  120. begin
  121. if assigned(t^.less) then
  122. genitem(t^.less);
  123. { need we to test the first value }
  124. if first and (t^._low>get_min_value(left.resulttype.def)) then
  125. begin
  126. cg.a_cmp_const_reg_label(exprasmlist,opsize,jmp_lt,aint(t^._low),hregister,elselabel);
  127. end;
  128. if t^._low=t^._high then
  129. begin
  130. if t^._low-last=0 then
  131. cg.a_cmp_const_reg_label(exprasmlist, opsize, OC_EQ,0,hregister,blocklabel(t^.blockid))
  132. else
  133. begin
  134. cg.a_op_const_reg(exprasmlist, OP_SUB, opsize, aint(t^._low-last), hregister);
  135. cg.a_jmp_flags(exprasmlist,F_E,blocklabel(t^.blockid));
  136. end;
  137. last:=t^._low;
  138. lastrange:=false;
  139. end
  140. else
  141. begin
  142. { it begins with the smallest label, if the value }
  143. { is even smaller then jump immediately to the }
  144. { ELSE-label }
  145. if first then
  146. begin
  147. { have we to ajust the first value ? }
  148. if (t^._low>get_min_value(left.resulttype.def)) then
  149. cg.a_op_const_reg(exprasmlist, OP_SUB, opsize, aint(t^._low), hregister);
  150. end
  151. else
  152. begin
  153. { if there is no unused label between the last and the }
  154. { present label then the lower limit can be checked }
  155. { immediately. else check the range in between: }
  156. cg.a_op_const_reg(exprasmlist, OP_SUB, opsize, aint(t^._low-last), hregister);
  157. { no jump necessary here if the new range starts at }
  158. { at the value following the previous one }
  159. if ((t^._low-last) <> 1) or
  160. (not lastrange) then
  161. cg.a_jmp_flags(exprasmlist,cond_lt,elselabel);
  162. end;
  163. {we need to use A_SUB, because A_DEC does not set the correct flags, therefor
  164. using a_op_const_reg(OP_SUB) is not possible }
  165. emit_const_reg(A_SUB,TCGSize2OpSize[opsize],aint(t^._high-t^._low),hregister);
  166. cg.a_jmp_flags(exprasmlist,cond_le,blocklabel(t^.blockid));
  167. last:=t^._high;
  168. lastrange:=true;
  169. end;
  170. first:=false;
  171. if assigned(t^.greater) then
  172. genitem(t^.greater);
  173. end;
  174. begin
  175. if with_sign then
  176. begin
  177. cond_lt:=F_L;
  178. cond_le:=F_LE;
  179. end
  180. else
  181. begin
  182. cond_lt:=F_B;
  183. cond_le:=F_BE;
  184. end;
  185. { do we need to generate cmps? }
  186. if (with_sign and (min_label<0)) then
  187. genlinearcmplist(hp)
  188. else
  189. begin
  190. last:=0;
  191. lastrange:=false;
  192. first:=true;
  193. genitem(hp);
  194. cg.a_jmp_always(exprasmlist,elselabel);
  195. end;
  196. end;
  197. begin
  198. ccasenode:=ti386casenode;
  199. end.
  200. {
  201. $Log$
  202. Revision 1.77 2004-11-30 18:13:39 jonas
  203. * patch from Peter to fix inlining of case statements
  204. Revision 1.76 2004/06/20 08:55:31 florian
  205. * logs truncated
  206. Revision 1.75 2004/06/16 20:07:10 florian
  207. * dwarf branch merged
  208. Revision 1.74 2004/05/22 23:34:28 peter
  209. tai_regalloc.allocation changed to ratype to notify rgobj of register size changes
  210. Revision 1.73.2.5 2004/05/02 20:54:41 peter
  211. * compile fix
  212. Revision 1.73.2.4 2004/05/02 14:09:54 peter
  213. * fix case 64bit issues
  214. Revision 1.73.2.3 2004/04/29 23:30:28 peter
  215. * fix i386 compiler
  216. }