n386set.pas 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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,nx86set;
  23. type
  24. ti386casenode = class(tx86casenode)
  25. procedure optimizevalues(var max_linear_list:aint;var max_dist:aword);override;
  26. procedure genlinearlist(hp : pcaselabel);override;
  27. end;
  28. implementation
  29. uses
  30. systems,
  31. verbose,globals,constexp,
  32. symconst,symdef,defutil,
  33. aasmbase,aasmtai,aasmdata,aasmcpu,
  34. cgbase,pass_2,
  35. ncon,
  36. cpubase,cpuinfo,procinfo,
  37. cga,cgutils,cgobj,ncgutil,
  38. cgx86;
  39. {*****************************************************************************
  40. TI386CASENODE
  41. *****************************************************************************}
  42. procedure ti386casenode.optimizevalues(var max_linear_list:aint;var max_dist:aword);
  43. begin
  44. { a jump table crashes the pipeline! }
  45. if current_settings.optimizecputype=cpu_386 then
  46. inc(max_linear_list,3)
  47. else if current_settings.optimizecputype=cpu_Pentium then
  48. inc(max_linear_list,6)
  49. else if current_settings.optimizecputype in [cpu_Pentium2,cpu_Pentium3] then
  50. inc(max_linear_list,9)
  51. else if current_settings.optimizecputype=cpu_Pentium4 then
  52. inc(max_linear_list,14);
  53. end;
  54. procedure ti386casenode.genlinearlist(hp : pcaselabel);
  55. var
  56. first : boolean;
  57. lastrange : boolean;
  58. last : TConstExprInt;
  59. cond_lt,cond_le : tresflags;
  60. procedure genitem(t : pcaselabel);
  61. begin
  62. if assigned(t^.less) then
  63. genitem(t^.less);
  64. { need we to test the first value }
  65. if first and (t^._low>get_min_value(left.resultdef)) then
  66. begin
  67. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,opsize,jmp_lt,aint(t^._low.svalue),hregister,elselabel);
  68. end;
  69. if t^._low=t^._high then
  70. begin
  71. if t^._low-last=0 then
  72. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList, opsize, OC_EQ,0,hregister,blocklabel(t^.blockid))
  73. else
  74. begin
  75. cg.a_op_const_reg(current_asmdata.CurrAsmList, OP_SUB, opsize, aint(t^._low.svalue-last.svalue), hregister);
  76. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_E,blocklabel(t^.blockid));
  77. end;
  78. last:=t^._low;
  79. lastrange:=false;
  80. end
  81. else
  82. begin
  83. { it begins with the smallest label, if the value }
  84. { is even smaller then jump immediately to the }
  85. { ELSE-label }
  86. if first then
  87. begin
  88. { have we to ajust the first value ? }
  89. if (t^._low>get_min_value(left.resultdef)) or (get_min_value(left.resultdef)<>0) then
  90. cg.a_op_const_reg(current_asmdata.CurrAsmList, OP_SUB, opsize, aint(t^._low.svalue), hregister);
  91. end
  92. else
  93. begin
  94. { if there is no unused label between the last and the }
  95. { present label then the lower limit can be checked }
  96. { immediately. else check the range in between: }
  97. cg.a_op_const_reg(current_asmdata.CurrAsmList, OP_SUB, opsize, aint(t^._low.svalue-last.svalue), hregister);
  98. { no jump necessary here if the new range starts at }
  99. { at the value following the previous one }
  100. if ((t^._low-last) <> 1) or
  101. (not lastrange) then
  102. cg.a_jmp_flags(current_asmdata.CurrAsmList,cond_lt,elselabel);
  103. end;
  104. {we need to use A_SUB, because A_DEC does not set the correct flags, therefor
  105. using a_op_const_reg(OP_SUB) is not possible }
  106. emit_const_reg(A_SUB,TCGSize2OpSize[opsize],aint(t^._high.svalue-t^._low.svalue),hregister);
  107. cg.a_jmp_flags(current_asmdata.CurrAsmList,cond_le,blocklabel(t^.blockid));
  108. last:=t^._high;
  109. lastrange:=true;
  110. end;
  111. first:=false;
  112. if assigned(t^.greater) then
  113. genitem(t^.greater);
  114. end;
  115. begin
  116. if with_sign then
  117. begin
  118. cond_lt:=F_L;
  119. cond_le:=F_LE;
  120. end
  121. else
  122. begin
  123. cond_lt:=F_B;
  124. cond_le:=F_BE;
  125. end;
  126. { do we need to generate cmps? }
  127. if (with_sign and (min_label<0)) then
  128. genlinearcmplist(hp)
  129. else
  130. begin
  131. last:=0;
  132. lastrange:=false;
  133. first:=true;
  134. genitem(hp);
  135. cg.a_jmp_always(current_asmdata.CurrAsmList,elselabel);
  136. end;
  137. end;
  138. begin
  139. ccasenode:=ti386casenode;
  140. end.